Repository navigation
Expand file tree
/
Copy patherror_cause_header.go
More file actions
68 lines (52 loc) · 1.67 KB
/
Copy patherror_cause_header.go
File metadata and controls
68 lines (52 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package sctp
import (
"encoding/binary"
"errors"
)
// errorCauseHeader represents the shared header that is shared by all error causes.
type errorCauseHeader struct {
code errorCauseCode
len uint16 // Cause Length: header(4) + value, no padding. RFC 9260 sec 3.3.10
raw []byte // value (cause-specific info), no padding
}
const (
errorCauseHeaderLength = 4
maxErrorCauseValueLen = 0xFFFF - errorCauseHeaderLength
)
// ErrInvalidSCTPChunk is returned when an SCTP chunk is invalid.
var ErrInvalidSCTPChunk = errors.New("invalid SCTP chunk")
func (e *errorCauseHeader) marshal() ([]byte, error) {
if len(e.raw) > maxErrorCauseValueLen {
return nil, ErrCauseLengthInvalid
}
e.len = uint16(len(e.raw)) + uint16(errorCauseHeaderLength) //nolint:gosec // G115
raw := make([]byte, e.len)
binary.BigEndian.PutUint16(raw[0:], uint16(e.code))
binary.BigEndian.PutUint16(raw[2:], e.len)
copy(raw[errorCauseHeaderLength:], e.raw)
return raw, nil
}
func (e *errorCauseHeader) unmarshal(raw []byte) error {
if len(raw) < errorCauseHeaderLength {
return ErrInvalidSCTPChunk
}
e.code = errorCauseCode(binary.BigEndian.Uint16(raw[0:]))
e.len = binary.BigEndian.Uint16(raw[2:])
if e.len < errorCauseHeaderLength || int(e.len) > len(raw) {
return ErrInvalidSCTPChunk
}
e.raw = raw[errorCauseHeaderLength:e.len]
return nil
}
func (e *errorCauseHeader) length() uint16 {
return e.len
}
func (e *errorCauseHeader) errorCauseCode() errorCauseCode {
return e.code
}
// String makes errorCauseHeader printable.
func (e errorCauseHeader) String() string {
return e.code.String()
}