| 1 |
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package sctp |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
) |
| 10 |
|
| 11 |
/* |
| 12 |
chunkHeartbeatAck represents an SCTP Chunk of type HEARTBEAT ACK |
| 13 |
|
| 14 |
An endpoint should send this chunk to its peer endpoint as a response |
| 15 |
to a HEARTBEAT chunk (see Section 8.3). A HEARTBEAT ACK is always |
| 16 |
sent to the source IP address of the IP datagram containing the |
| 17 |
HEARTBEAT chunk to which this ack is responding. |
| 18 |
|
| 19 |
The parameter field contains a variable-length opaque data structure. |
| 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 = 5 | Chunk Flags | Heartbeat Ack Length | |
| 25 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 26 |
| | |
| 27 |
| Heartbeat Information TLV (Variable-Length) | |
| 28 |
| | |
| 29 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 30 |
|
| 31 |
Defined as a variable-length parameter using the format described |
| 32 |
in Section 3.2.1, i.e.: |
| 33 |
|
| 34 |
Variable Parameters Status Type Value |
| 35 |
------------------------------------------------------------- |
| 36 |
Heartbeat Info Mandatory 1 |
| 37 |
. |
| 38 |
*/ |
| 39 |
type chunkHeartbeatAck struct { |
| 40 |
chunkHeader |
| 41 |
params []param |
| 42 |
} |
| 43 |
|
| 44 |
// Heartbeat ack chunk errors. |
| 45 |
var ( |
| 46 |
// Deprecated: this error is no longer used but is kept for compatibility. |
| 47 |
ErrUnimplemented = errors.New("unimplemented") |
| 48 |
ErrChunkTypeNotHeartbeatAck = errors.New("chunk type is not of type HEARTBEAT ACK") |
| 49 |
ErrHeartbeatAckParams = errors.New("heartbeat Ack must have one param") |
| 50 |
ErrHeartbeatAckNotHeartbeatInfo = errors.New("heartbeat Ack must have one param, and it should be a HeartbeatInfo") |
| 51 |
ErrHeartbeatAckMarshalParam = errors.New("unable to marshal parameter for Heartbeat Ack") |
| 52 |
) |
| 53 |
|
| 54 |
func (h *chunkHeartbeatAck) unmarshal(raw []byte) error { //nolint:cyclop |
| 55 |
if err := h.chunkHeader.unmarshal(raw); err != nil { |
| 56 |
return err |
| 57 |
} |
| 58 |
|
| 59 |
if h.typ != ctHeartbeatAck { |
| 60 |
return fmt.Errorf("%w %s", ErrChunkTypeNotHeartbeatAck, h.typ.String()) |
| 61 |
} |
| 62 |
|
| 63 |
// allow for an empty heartbeat: no RTT info -> ActiveHeartbeat just won't update SRTT. |
| 64 |
if len(h.raw) == 0 { |
| 65 |
h.params = nil |
| 66 |
|
| 67 |
return nil |
| 68 |
} |
| 69 |
|
| 70 |
if len(h.raw) < initOptionalVarHeaderLength { |
| 71 |
return fmt.Errorf("%w: %d", ErrHeartbeatAckParams, len(h.raw)) |
| 72 |
} |
| 73 |
|
| 74 |
pType, err := parseParamType(h.raw) |
| 75 |
if err != nil { |
| 76 |
return fmt.Errorf("%w: %v", ErrHeartbeatAckParams, err) //nolint:errorlint |
| 77 |
} |
| 78 |
if pType != heartbeatInfo { |
| 79 |
return fmt.Errorf("%w: instead have %s", ErrHeartbeatAckNotHeartbeatInfo, pType.String()) |
| 80 |
} |
| 81 |
|
| 82 |
var pHeader paramHeader |
| 83 |
if e := pHeader.unmarshal(h.raw); e != nil { |
| 84 |
return fmt.Errorf("%w: %v", ErrHeartbeatAckParams, e) //nolint:errorlint |
| 85 |
} |
| 86 |
plen := pHeader.length() |
| 87 |
if plen < initOptionalVarHeaderLength || plen > len(h.raw) { |
| 88 |
return fmt.Errorf("%w: %d", ErrHeartbeatAckParams, plen) |
| 89 |
} |
| 90 |
|
| 91 |
p, err := buildParam(pType, h.raw[:plen]) |
| 92 |
if err != nil { |
| 93 |
return fmt.Errorf("%w: %v", ErrHeartbeatAckMarshalParam, err) //nolint:errorlint |
| 94 |
} |
| 95 |
h.params = []param{p} |
| 96 |
|
| 97 |
// Any trailing bytes beyond the single param must be zero. |
| 98 |
if rem := h.raw[plen:]; len(rem) > 0 && !allZero(rem) { |
| 99 |
return ErrHeartbeatExtraNonZero |
| 100 |
} |
| 101 |
|
| 102 |
return nil |
| 103 |
} |
| 104 |
|
| 105 |
func (h *chunkHeartbeatAck) marshal() ([]byte, error) { |
| 106 |
if len(h.params) != 1 { |
| 107 |
return nil, ErrHeartbeatAckParams |
| 108 |
} |
| 109 |
|
| 110 |
switch h.params[0].(type) { |
| 111 |
case *paramHeartbeatInfo: |
| 112 |
// ParamHeartbeatInfo is valid |
| 113 |
default: |
| 114 |
return nil, ErrHeartbeatAckNotHeartbeatInfo |
| 115 |
} |
| 116 |
|
| 117 |
out := make([]byte, 0) |
| 118 |
for idx, p := range h.params { |
| 119 |
pp, err := p.marshal() |
| 120 |
if err != nil { |
| 121 |
return nil, fmt.Errorf("%w: %v", ErrHeartbeatAckMarshalParam, err) //nolint:errorlint |
| 122 |
} |
| 123 |
|
| 124 |
out = append(out, pp...) |
| 125 |
|
| 126 |
// Chunks (including Type, Length, and Value fields) are padded out |
| 127 |
// by the sender with all zero bytes to be a multiple of 4 bytes |
| 128 |
// long. This padding MUST NOT be more than 3 bytes in total. The |
| 129 |
// Chunk Length value does not include terminating padding of the |
| 130 |
// chunk. *However, it does include padding of any variable-length |
| 131 |
// parameter except the last parameter in the chunk.* The receiver |
| 132 |
// MUST ignore the padding. |
| 133 |
if idx != len(h.params)-1 { |
| 134 |
out = padByte(out, getPadding(len(pp))) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
h.chunkHeader.typ = ctHeartbeatAck |
| 139 |
h.chunkHeader.raw = out |
| 140 |
|
| 141 |
return h.chunkHeader.marshal() |
| 142 |
} |
| 143 |
|
| 144 |
func (h *chunkHeartbeatAck) check() (abort bool, err error) { |
| 145 |
return false, nil |
| 146 |
} |