| 1 |
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package sctp // nolint:dupl |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"strings" |
| 10 |
) |
| 11 |
|
| 12 |
/* |
| 13 |
Operation Error (ERROR) (9) |
| 14 |
|
| 15 |
An endpoint sends this chunk to its peer endpoint to notify it of |
| 16 |
certain error conditions. It contains one or more error causes. An |
| 17 |
Operation Error is not considered fatal in and of itself, but may be |
| 18 |
used with an ERROR chunk to report a fatal condition. It has the |
| 19 |
following parameters: |
| 20 |
|
| 21 |
0 1 2 3 |
| 22 |
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 |
| 23 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 24 |
| Type = 9 | Chunk Flags | Length | |
| 25 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 26 |
\ \ |
| 27 |
/ one or more Error Causes / |
| 28 |
\ \ |
| 29 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 30 |
|
| 31 |
Chunk Flags: 8 bits |
| 32 |
|
| 33 |
Set to 0 on transmit and ignored on receipt. |
| 34 |
|
| 35 |
Length: 16 bits (unsigned integer) |
| 36 |
|
| 37 |
Set to the size of the chunk in bytes, including the chunk header |
| 38 |
and all the Error Cause fields present. |
| 39 |
*/ |
| 40 |
type chunkError struct { |
| 41 |
chunkHeader |
| 42 |
errorCauses []errorCause |
| 43 |
} |
| 44 |
|
| 45 |
// Error chunk errors. |
| 46 |
var ( |
| 47 |
ErrChunkTypeNotCtError = errors.New("ChunkType is not of type ctError") |
| 48 |
ErrBuildErrorChunkFailed = errors.New("failed build Error Chunk") |
| 49 |
) |
| 50 |
|
| 51 |
func (a *chunkError) unmarshal(raw []byte) error { |
| 52 |
if err := a.chunkHeader.unmarshal(raw); err != nil { |
| 53 |
return err |
| 54 |
} |
| 55 |
|
| 56 |
if a.typ != ctError { |
| 57 |
return fmt.Errorf("%w, actually is %s", ErrChunkTypeNotCtError, a.typ.String()) |
| 58 |
} |
| 59 |
|
| 60 |
offset := chunkHeaderSize |
| 61 |
for len(raw)-offset >= 4 { |
| 62 |
e, err := buildErrorCause(raw[offset:]) |
| 63 |
if err != nil { |
| 64 |
return fmt.Errorf("%w: %v", ErrBuildErrorChunkFailed, err) //nolint:errorlint |
| 65 |
} |
| 66 |
|
| 67 |
offset += int(e.length()) |
| 68 |
a.errorCauses = append(a.errorCauses, e) |
| 69 |
} |
| 70 |
|
| 71 |
return nil |
| 72 |
} |
| 73 |
|
| 74 |
func (a *chunkError) marshal() ([]byte, error) { |
| 75 |
a.chunkHeader.typ = ctError |
| 76 |
a.flags = 0x00 |
| 77 |
a.raw = []byte{} |
| 78 |
for _, ec := range a.errorCauses { |
| 79 |
raw, err := ec.marshal() |
| 80 |
if err != nil { |
| 81 |
return nil, err |
| 82 |
} |
| 83 |
a.raw = append(a.raw, raw...) |
| 84 |
} |
| 85 |
|
| 86 |
return a.chunkHeader.marshal() |
| 87 |
} |
| 88 |
|
| 89 |
func (a *chunkError) check() (abort bool, err error) { |
| 90 |
return false, nil |
| 91 |
} |
| 92 |
|
| 93 |
// String makes chunkError printable. |
| 94 |
func (a *chunkError) String() string { |
| 95 |
var res strings.Builder |
| 96 |
res.WriteString(a.chunkHeader.String()) |
| 97 |
|
| 98 |
for _, cause := range a.errorCauses { |
| 99 |
fmt.Fprintf(&res, "\n - %s", cause) |
| 100 |
} |
| 101 |
|
| 102 |
return res.String() |
| 103 |
} |