| 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 |
) |
| 11 |
|
| 12 |
// errorCauseCode is a cause code that appears in either a ERROR or ABORT chunk. |
| 13 |
type errorCauseCode uint16 |
| 14 |
|
| 15 |
type errorCause interface { |
| 16 |
unmarshal([]byte) error |
| 17 |
marshal() ([]byte, error) |
| 18 |
length() uint16 |
| 19 |
String() string |
| 20 |
|
| 21 |
errorCauseCode() errorCauseCode |
| 22 |
} |
| 23 |
|
| 24 |
// Error and abort chunk errors. |
| 25 |
var ( |
| 26 |
ErrBuildErrorCaseHandle = errors.New("BuildErrorCause does not handle") |
| 27 |
) |
| 28 |
|
| 29 |
// buildErrorCause delegates the building of a error cause from raw bytes to the correct structure. |
| 30 |
func buildErrorCause(raw []byte) (errorCause, error) { |
| 31 |
var errCause errorCause |
| 32 |
|
| 33 |
c := errorCauseCode(binary.BigEndian.Uint16(raw[0:])) |
| 34 |
switch c { |
| 35 |
case invalidMandatoryParameter: |
| 36 |
errCause = &errorCauseInvalidMandatoryParameter{} |
| 37 |
case unrecognizedChunkType: |
| 38 |
errCause = &errorCauseUnrecognizedChunkType{} |
| 39 |
case protocolViolation: |
| 40 |
errCause = &errorCauseProtocolViolation{} |
| 41 |
case userInitiatedAbort: |
| 42 |
errCause = &errorCauseUserInitiatedAbort{} |
| 43 |
default: |
| 44 |
// Unknown cause — treat as opaque TLV per RFC 9260 §3.2.1 |
| 45 |
e := &errorCauseHeader{} |
| 46 |
if err := e.unmarshal(raw); err != nil { |
| 47 |
return nil, err |
| 48 |
} |
| 49 |
|
| 50 |
return e, nil |
| 51 |
} |
| 52 |
|
| 53 |
if err := errCause.unmarshal(raw); err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
|
| 57 |
return errCause, nil |
| 58 |
} |
| 59 |
|
| 60 |
const ( |
| 61 |
invalidStreamIdentifier errorCauseCode = 1 |
| 62 |
missingMandatoryParameter errorCauseCode = 2 |
| 63 |
staleCookieError errorCauseCode = 3 |
| 64 |
outOfResource errorCauseCode = 4 |
| 65 |
unresolvableAddress errorCauseCode = 5 |
| 66 |
unrecognizedChunkType errorCauseCode = 6 |
| 67 |
invalidMandatoryParameter errorCauseCode = 7 |
| 68 |
unrecognizedParameters errorCauseCode = 8 |
| 69 |
noUserData errorCauseCode = 9 |
| 70 |
cookieReceivedWhileShuttingDown errorCauseCode = 10 |
| 71 |
restartOfAnAssociationWithNewAddresses errorCauseCode = 11 |
| 72 |
userInitiatedAbort errorCauseCode = 12 |
| 73 |
protocolViolation errorCauseCode = 13 |
| 74 |
) |
| 75 |
|
| 76 |
func (e errorCauseCode) String() string { //nolint:cyclop |
| 77 |
switch e { |
| 78 |
case invalidStreamIdentifier: |
| 79 |
return "Invalid Stream Identifier" |
| 80 |
case missingMandatoryParameter: |
| 81 |
return "Missing Mandatory Parameter" |
| 82 |
case staleCookieError: |
| 83 |
return "Stale Cookie Error" |
| 84 |
case outOfResource: |
| 85 |
return "Out Of Resource" |
| 86 |
case unresolvableAddress: |
| 87 |
return "Unresolvable IP" |
| 88 |
case unrecognizedChunkType: |
| 89 |
return "Unrecognized Chunk Type" |
| 90 |
case invalidMandatoryParameter: |
| 91 |
return "Invalid Mandatory Parameter" |
| 92 |
case unrecognizedParameters: |
| 93 |
return "Unrecognized Parameters" |
| 94 |
case noUserData: |
| 95 |
return "No User Data" |
| 96 |
case cookieReceivedWhileShuttingDown: |
| 97 |
return "Cookie Received While Shutting Down" |
| 98 |
case restartOfAnAssociationWithNewAddresses: |
| 99 |
return "Restart Of An Association With New Addresses" |
| 100 |
case userInitiatedAbort: |
| 101 |
return "User Initiated Abort" |
| 102 |
case protocolViolation: |
| 103 |
return "Protocol Violation" |
| 104 |
default: |
| 105 |
return fmt.Sprintf("Unknown CauseCode: %d", e) |
| 106 |
} |
| 107 |
} |