Debian Codegraph @

chunk_heartbeat.go

raw
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 chunkHeartbeat represents an SCTP Chunk of type HEARTBEAT (RFC 9260 section 3.3.6)
13
14 An endpoint sends this chunk to probe reachability of a destination address.
15 The chunk MUST contain exactly one variable-length parameter:
16
17 Variable Parameters Status Type Value
18 -------------------------------------------------------------
19 Heartbeat Info Mandatory 1
20
21 nolint:godot
22 */
23 type chunkHeartbeat struct {
24 chunkHeader
25 params []param
26 }
27
28 // Heartbeat chunk errors.
29 var (
30 ErrChunkTypeNotHeartbeat = errors.New("ChunkType is not of type HEARTBEAT")
31 ErrHeartbeatNotLongEnoughInfo = errors.New("heartbeat is not long enough to contain Heartbeat Info")
32 ErrParseParamTypeFailed = errors.New("failed to parse param type")
33 ErrHeartbeatParam = errors.New("heartbeat should only have HEARTBEAT param")
34 ErrHeartbeatChunkUnmarshal = errors.New("failed unmarshalling param in Heartbeat Chunk")
35 ErrHeartbeatExtraNonZero = errors.New("heartbeat has non-zero trailing bytes after last parameter")
36 ErrHeartbeatMarshalNoInfo = errors.New("heartbeat marshal requires exactly one Heartbeat Info parameter")
37 )
38
39 func (h *chunkHeartbeat) unmarshal(raw []byte) error { //nolint:cyclop
40 if err := h.chunkHeader.unmarshal(raw); err != nil {
41 return err
42 }
43
44 if h.typ != ctHeartbeat {
45 return fmt.Errorf("%w: actually is %s", ErrChunkTypeNotHeartbeat, h.typ.String())
46 }
47
48 // if the body is completely empty, accept it but don't populate params.
49 if len(h.raw) == 0 {
50 return nil
51 }
52
53 // need at least a parameter header present (TLV: 4 bytes minimum).
54 if len(h.raw) < initOptionalVarHeaderLength {
55 return fmt.Errorf("%w: %d", ErrHeartbeatNotLongEnoughInfo, len(h.raw))
56 }
57
58 pType, err := parseParamType(h.raw)
59 if err != nil {
60 return fmt.Errorf("%w: %v", ErrParseParamTypeFailed, err) //nolint:errorlint
61 }
62 if pType != heartbeatInfo {
63 return fmt.Errorf("%w: instead have %s", ErrHeartbeatParam, pType.String())
64 }
65
66 var pHeader paramHeader
67 if e := pHeader.unmarshal(h.raw); e != nil {
68 return fmt.Errorf("%w: %v", ErrParseParamTypeFailed, e) //nolint:errorlint
69 }
70
71 plen := pHeader.length()
72 if plen < initOptionalVarHeaderLength || plen > len(h.raw) {
73 return ErrHeartbeatNotLongEnoughInfo
74 }
75
76 p, err := buildParam(pType, h.raw[:plen])
77 if err != nil {
78 return fmt.Errorf("%w: %v", ErrHeartbeatChunkUnmarshal, err) //nolint:errorlint
79 }
80 h.params = append(h.params, p)
81
82 // any trailing bytes beyond the single param must be all zeros.
83 if rem := h.raw[plen:]; len(rem) > 0 && !allZero(rem) {
84 return ErrHeartbeatExtraNonZero
85 }
86
87 return nil
88 }
89
90 func (h *chunkHeartbeat) Marshal() ([]byte, error) {
91 // exactly one Heartbeat Info param is required.
92 if len(h.params) != 1 {
93 return nil, ErrHeartbeatMarshalNoInfo
94 }
95
96 // enforce correct concrete type via type assertion (param interface has no type getter).
97 if _, ok := h.params[0].(*paramHeartbeatInfo); !ok {
98 return nil, ErrHeartbeatParam
99 }
100
101 pp, err := h.params[0].marshal()
102 if err != nil {
103 return nil, fmt.Errorf("%w: %v", ErrHeartbeatChunkUnmarshal, err) //nolint:errorlint
104 }
105
106 // single TLV, no inter-parameter padding within the chunk body.
107 h.chunkHeader.typ = ctHeartbeat
108 h.chunkHeader.flags = 0 // sender MUST set to 0
109 h.chunkHeader.raw = append([]byte(nil), pp...)
110
111 return h.chunkHeader.marshal()
112 }
113
114 func (h *chunkHeartbeat) check() (abort bool, err error) {
115 return false, nil
116 }