Debian Codegraph @

chunk_abort.go

raw
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 Abort represents an SCTP Chunk of type ABORT
14
15 The ABORT chunk is sent to the peer of an association to close the
16 association. The ABORT chunk may contain Cause Parameters to inform
17 the receiver about the reason of the abort. DATA chunks MUST NOT be
18 bundled with ABORT. Control chunks (except for INIT, INIT ACK, and
19 SHUTDOWN COMPLETE) MAY be bundled with an ABORT, but they MUST be
20 placed before the ABORT in the SCTP packet or they will be ignored by
21 the receiver.
22
23 0 1 2 3
24 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
25 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26 | Type = 6 |Reserved |T| Length |
27 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28 | |
29 | zero or more Error Causes |
30 | |
31 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32 */
33 type chunkAbort struct {
34 chunkHeader
35 errorCauses []errorCause
36 }
37
38 // Abort chunk errors.
39 var (
40 ErrChunkTypeNotAbort = errors.New("ChunkType is not of type ABORT")
41 ErrBuildAbortChunkFailed = errors.New("failed build Abort Chunk")
42 )
43
44 func (a *chunkAbort) unmarshal(raw []byte) error {
45 if err := a.chunkHeader.unmarshal(raw); err != nil {
46 return err
47 }
48
49 if a.typ != ctAbort {
50 return fmt.Errorf("%w: actually is %s", ErrChunkTypeNotAbort, a.typ.String())
51 }
52
53 offset := chunkHeaderSize
54 for len(raw)-offset >= 4 {
55 e, err := buildErrorCause(raw[offset:])
56 if err != nil {
57 return fmt.Errorf("%w: %v", ErrBuildAbortChunkFailed, err) //nolint:errorlint
58 }
59
60 offset += int(e.length())
61 a.errorCauses = append(a.errorCauses, e)
62 }
63
64 return nil
65 }
66
67 func (a *chunkAbort) marshal() ([]byte, error) {
68 a.chunkHeader.typ = ctAbort
69 a.flags = 0x00
70 a.raw = []byte{}
71 for _, ec := range a.errorCauses {
72 raw, err := ec.marshal()
73 if err != nil {
74 return nil, err
75 }
76 a.raw = append(a.raw, raw...)
77 }
78
79 return a.chunkHeader.marshal()
80 }
81
82 func (a *chunkAbort) check() (abort bool, err error) {
83 return false, nil
84 }
85
86 // String makes chunkAbort printable.
87 func (a *chunkAbort) String() string {
88 var res strings.Builder
89 res.WriteString(a.chunkHeader.String())
90
91 for _, cause := range a.errorCauses {
92 fmt.Fprintf(&res, "\n - %s", cause)
93 }
94
95 return res.String()
96 }