Debian Codegraph @

chunk_init_ack.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 )
10
11 /*
12 chunkInitAck represents an SCTP Chunk of type INIT ACK
13
14 See chunkInitCommon for the fixed headers
15
16 Variable Parameters Status Type Value
17 -------------------------------------------------------------
18 State Cookie Mandatory 7
19 IPv4 IP (Note 1) Optional 5
20 IPv6 IP (Note 1) Optional 6
21 Unrecognized Parameter Optional 8
22 Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
23 Host Name IP (Note 3) Optional 11<Paste>
24 */
25 type chunkInitAck struct {
26 chunkHeader
27 chunkInitCommon
28 }
29
30 // Init ack chunk errors.
31 var (
32 ErrChunkTypeNotInitAck = errors.New("ChunkType is not of type INIT ACK")
33 ErrChunkNotLongEnoughForParams = errors.New("chunk Value isn't long enough for mandatory parameters exp")
34 ErrChunkTypeInitAckFlagZero = errors.New("ChunkType of type INIT ACK flags must be all 0")
35 ErrInitAckUnmarshalFailed = errors.New("failed to unmarshal INIT body")
36 ErrInitCommonDataMarshalFailed = errors.New("failed marshaling INIT common data")
37 ErrChunkTypeInitAckInitateTagZero = errors.New("ChunkType of type INIT ACK InitiateTag must not be 0")
38 ErrInitAckInboundStreamRequestZero = errors.New("INIT ACK inbound stream request must be > 0")
39 ErrInitAckOutboundStreamRequestZero = errors.New("INIT ACK outbound stream request must be > 0")
40 ErrInitAckAdvertisedReceiver1500 = errors.New("INIT ACK Advertised Receiver Window Credit (a_rwnd) must be >= 1500")
41 )
42
43 func (i *chunkInitAck) unmarshal(raw []byte) error {
44 if err := i.chunkHeader.unmarshal(raw); err != nil {
45 return err
46 }
47
48 if i.typ != ctInitAck {
49 return fmt.Errorf("%w: actually is %s", ErrChunkTypeNotInitAck, i.typ.String())
50 } else if len(i.raw) < initChunkMinLength {
51 return fmt.Errorf("%w: %d actual: %d", ErrChunkNotLongEnoughForParams, initChunkMinLength, len(i.raw))
52 }
53
54 // The Chunk Flags field in INIT is reserved, and all bits in it should
55 // be set to 0 by the sender and ignored by the receiver. The sequence
56 // of parameters within an INIT can be processed in any order.
57 if i.flags != 0 {
58 return ErrChunkTypeInitAckFlagZero
59 }
60
61 if err := i.chunkInitCommon.unmarshal(i.raw); err != nil {
62 return fmt.Errorf("%w: %v", ErrInitAckUnmarshalFailed, err) //nolint:errorlint
63 }
64
65 return nil
66 }
67
68 func (i *chunkInitAck) marshal() ([]byte, error) {
69 initShared, err := i.chunkInitCommon.marshal()
70 if err != nil {
71 return nil, fmt.Errorf("%w: %v", ErrInitCommonDataMarshalFailed, err) //nolint:errorlint
72 }
73
74 i.chunkHeader.typ = ctInitAck
75 i.chunkHeader.raw = initShared
76
77 return i.chunkHeader.marshal()
78 }
79
80 func (i *chunkInitAck) check() (abort bool, err error) {
81 // The receiver of the INIT ACK records the value of the Initiate Tag
82 // parameter. This value MUST be placed into the Verification Tag
83 // field of every SCTP packet that the INIT ACK receiver transmits
84 // within this association.
85 //
86 // The Initiate Tag MUST NOT take the value 0. See Section 5.3.1 for
87 // more on the selection of the Initiate Tag value.
88 //
89 // If the value of the Initiate Tag in a received INIT ACK chunk is
90 // found to be 0, the receiver MUST destroy the association
91 // discarding its TCB. The receiver MAY send an ABORT for debugging
92 // purpose.
93 if i.initiateTag == 0 {
94 abort = true
95
96 return abort, ErrChunkTypeInitAckInitateTagZero
97 }
98
99 // Defines the maximum number of streams the sender of this INIT ACK
100 // chunk allows the peer end to create in this association. The
101 // value 0 MUST NOT be used.
102 //
103 // Note: There is no negotiation of the actual number of streams but
104 // instead the two endpoints will use the min(requested, offered).
105 // See Section 5.1.1 for details.
106 //
107 // Note: A receiver of an INIT ACK with the MIS value set to 0 SHOULD
108 // destroy the association discarding its TCB.
109 if i.numInboundStreams == 0 {
110 abort = true
111
112 return abort, ErrInitAckInboundStreamRequestZero
113 }
114
115 // Defines the number of outbound streams the sender of this INIT ACK
116 // chunk wishes to create in this association. The value of 0 MUST
117 // NOT be used, and the value MUST NOT be greater than the MIS value
118 // sent in the INIT chunk.
119 //
120 // Note: A receiver of an INIT ACK with the OS value set to 0 SHOULD
121 // destroy the association discarding its TCB.
122
123 if i.numOutboundStreams == 0 {
124 abort = true
125
126 return abort, ErrInitAckOutboundStreamRequestZero
127 }
128
129 // An SCTP receiver MUST be able to receive a minimum of 1500 bytes in
130 // one SCTP packet. This means that an SCTP endpoint MUST NOT indicate
131 // less than 1500 bytes in its initial a_rwnd sent in the INIT or INIT
132 // ACK.
133 if i.advertisedReceiverWindowCredit < 1500 {
134 abort = true
135
136 return abort, ErrInitAckAdvertisedReceiver1500
137 }
138
139 return false, nil
140 }
141
142 // String makes chunkInitAck printable.
143 func (i *chunkInitAck) String() string {
144 return fmt.Sprintf("%s\n%s", i.chunkHeader, i.chunkInitCommon)
145 }