Debian Codegraph @

chunkheader.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 "encoding/binary"
8 "errors"
9 "fmt"
10 )
11
12 /*
13 chunkHeader represents a SCTP Chunk header, defined in https://tools.ietf.org/html/rfc4960#section-3.2
14 The figure below illustrates the field format for the chunks to be
15 transmitted in the SCTP packet. Each chunk is formatted with a Chunk
16 Type field, a chunk-specific Flag field, a Chunk Length field, and a
17 Value field.
18
19 0 1 2 3
20 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
21 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
22 | Chunk Type | Chunk Flags | Chunk Length |
23 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
24 | |
25 | Chunk Value |
26 | |
27 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28 */
29 type chunkHeader struct {
30 typ chunkType
31 flags byte
32 raw []byte
33 }
34
35 const (
36 chunkHeaderSize = 4
37 )
38
39 // SCTP chunk header errors.
40 var (
41 ErrChunkHeaderTooSmall = errors.New("raw is too small for a SCTP chunk")
42 ErrChunkHeaderNotEnoughSpace = errors.New("not enough data left in SCTP packet to satisfy requested length")
43 ErrChunkHeaderPaddingNonZero = errors.New("chunk padding is non-zero at offset")
44 )
45
46 func (c *chunkHeader) unmarshal(raw []byte) error {
47 if len(raw) < chunkHeaderSize {
48 return fmt.Errorf(
49 "%w: raw only %d bytes, %d is the minimum length",
50 ErrChunkHeaderTooSmall, len(raw), chunkHeaderSize,
51 )
52 }
53
54 c.typ = chunkType(raw[0])
55 c.flags = raw[1]
56 length := binary.BigEndian.Uint16(raw[2:])
57
58 // Length includes Chunk header
59 valueLength := int(length - chunkHeaderSize)
60 lengthAfterValue := len(raw) - (chunkHeaderSize + valueLength)
61
62 if lengthAfterValue < 0 {
63 return fmt.Errorf("%w: remain %d req %d ", ErrChunkHeaderNotEnoughSpace, valueLength, len(raw)-chunkHeaderSize)
64 } else if lengthAfterValue < 4 {
65 // https://tools.ietf.org/html/rfc4960#section-3.2
66 // The Chunk Length field does not count any chunk padding.
67 // Chunks (including Type, Length, and Value fields) are padded out
68 // by the sender with all zero bytes to be a multiple of 4 bytes
69 // long. This padding MUST NOT be more than 3 bytes in total. The
70 // Chunk Length value does not include terminating padding of the
71 // chunk. However, it does include padding of any variable-length
72 // parameter except the last parameter in the chunk. The receiver
73 // MUST ignore the padding.
74 for i := lengthAfterValue; i > 0; i-- {
75 paddingOffset := chunkHeaderSize + valueLength + (i - 1)
76 if raw[paddingOffset] != 0 {
77 return fmt.Errorf("%w: %d ", ErrChunkHeaderPaddingNonZero, paddingOffset)
78 }
79 }
80 }
81
82 c.raw = raw[chunkHeaderSize : chunkHeaderSize+valueLength]
83
84 return nil
85 }
86
87 func (c *chunkHeader) marshal() ([]byte, error) {
88 raw := make([]byte, 4+len(c.raw))
89
90 raw[0] = uint8(c.typ)
91 raw[1] = c.flags
92 binary.BigEndian.PutUint16(raw[2:], uint16(len(c.raw)+chunkHeaderSize)) //nolint:gosec // G115
93 copy(raw[4:], c.raw)
94
95 return raw, nil
96 }
97
98 func (c *chunkHeader) valueLength() int {
99 return len(c.raw)
100 }
101
102 // String makes chunkHeader printable.
103 func (c chunkHeader) String() string {
104 return c.typ.String()
105 }