This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk_payload_data.go
212 lines (180 loc) · 6.5 KB
/
chunk_payload_data.go
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package sctp
import (
"encoding/binary"
"errors"
"fmt"
"time"
)
/*
chunkPayloadData represents an SCTP Chunk of type DATA
0 1 2 3
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
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type = 0 | Reserved|U|B|E| Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| TSN |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Stream Identifier S | Stream Sequence Number n |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Payload Protocol Identifier |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| User Data (seq n of Stream S) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
An unfragmented user message shall have both the B and E bits set to
'1'. Setting both B and E bits to '0' indicates a middle fragment of
a multi-fragment user message, as summarized in the following table:
B E Description
============================================================
| 1 0 | First piece of a fragmented user message |
+----------------------------------------------------------+
| 0 0 | Middle piece of a fragmented user message |
+----------------------------------------------------------+
| 0 1 | Last piece of a fragmented user message |
+----------------------------------------------------------+
| 1 1 | Unfragmented message |
============================================================
| Table 1: Fragment Description Flags |
============================================================
*/
type chunkPayloadData struct {
chunkHeader
unordered bool
beginningFragment bool
endingFragment bool
immediateSack bool
tsn uint32
streamIdentifier uint16
streamSequenceNumber uint16
payloadType PayloadProtocolIdentifier
userData []byte
// Whether this data chunk was acknowledged (received by peer)
acked bool
missIndicator uint32
// Partial-reliability parameters used only by sender
since time.Time
nSent uint32 // number of transmission made for this chunk
_abandoned bool
_allInflight bool // valid only with the first fragment
// Retransmission flag set when T1-RTX timeout occurred and this
// chunk is still in the inflight queue
retransmit bool
head *chunkPayloadData // link to the head of the fragment
}
const (
payloadDataEndingFragmentBitmask = 1
payloadDataBeginingFragmentBitmask = 2
payloadDataUnorderedBitmask = 4
payloadDataImmediateSACK = 8
payloadDataHeaderSize = 12
)
// PayloadProtocolIdentifier is an enum for DataChannel payload types
type PayloadProtocolIdentifier uint32
// PayloadProtocolIdentifier enums
// https://www.iana.org/assignments/sctp-parameters/sctp-parameters.xhtml#sctp-parameters-25
const (
PayloadTypeUnknown PayloadProtocolIdentifier = 0
PayloadTypeWebRTCDCEP PayloadProtocolIdentifier = 50
PayloadTypeWebRTCString PayloadProtocolIdentifier = 51
PayloadTypeWebRTCBinary PayloadProtocolIdentifier = 53
PayloadTypeWebRTCStringEmpty PayloadProtocolIdentifier = 56
PayloadTypeWebRTCBinaryEmpty PayloadProtocolIdentifier = 57
)
// Data chunk errors
var (
ErrChunkPayloadSmall = errors.New("packet is smaller than the header size")
)
func (p PayloadProtocolIdentifier) String() string {
switch p {
case PayloadTypeWebRTCDCEP:
return "WebRTC DCEP"
case PayloadTypeWebRTCString:
return "WebRTC String"
case PayloadTypeWebRTCBinary:
return "WebRTC Binary"
case PayloadTypeWebRTCStringEmpty:
return "WebRTC String (Empty)"
case PayloadTypeWebRTCBinaryEmpty:
return "WebRTC Binary (Empty)"
default:
return fmt.Sprintf("Unknown Payload Protocol Identifier: %d", p)
}
}
func (p *chunkPayloadData) unmarshal(raw []byte) error {
if err := p.chunkHeader.unmarshal(raw); err != nil {
return err
}
p.immediateSack = p.flags&payloadDataImmediateSACK != 0
p.unordered = p.flags&payloadDataUnorderedBitmask != 0
p.beginningFragment = p.flags&payloadDataBeginingFragmentBitmask != 0
p.endingFragment = p.flags&payloadDataEndingFragmentBitmask != 0
if len(p.raw) < payloadDataHeaderSize {
return ErrChunkPayloadSmall
}
p.tsn = binary.BigEndian.Uint32(p.raw[0:])
p.streamIdentifier = binary.BigEndian.Uint16(p.raw[4:])
p.streamSequenceNumber = binary.BigEndian.Uint16(p.raw[6:])
p.payloadType = PayloadProtocolIdentifier(binary.BigEndian.Uint32(p.raw[8:]))
p.userData = p.raw[payloadDataHeaderSize:]
return nil
}
func (p *chunkPayloadData) marshal() ([]byte, error) {
payRaw := make([]byte, payloadDataHeaderSize+len(p.userData))
binary.BigEndian.PutUint32(payRaw[0:], p.tsn)
binary.BigEndian.PutUint16(payRaw[4:], p.streamIdentifier)
binary.BigEndian.PutUint16(payRaw[6:], p.streamSequenceNumber)
binary.BigEndian.PutUint32(payRaw[8:], uint32(p.payloadType))
copy(payRaw[payloadDataHeaderSize:], p.userData)
flags := uint8(0)
if p.endingFragment {
flags = 1
}
if p.beginningFragment {
flags |= 1 << 1
}
if p.unordered {
flags |= 1 << 2
}
if p.immediateSack {
flags |= 1 << 3
}
p.chunkHeader.flags = flags
p.chunkHeader.typ = ctPayloadData
p.chunkHeader.raw = payRaw
return p.chunkHeader.marshal()
}
func (p *chunkPayloadData) check() (abort bool, err error) {
return false, nil
}
// String makes chunkPayloadData printable
func (p *chunkPayloadData) String() string {
return fmt.Sprintf("%s\n%d", p.chunkHeader, p.tsn)
}
func (p *chunkPayloadData) abandoned() bool {
if p.head != nil {
return p.head._abandoned && p.head._allInflight
}
return p._abandoned && p._allInflight
}
func (p *chunkPayloadData) setAbandoned(abandoned bool) {
if p.head != nil {
p.head._abandoned = abandoned
return
}
p._abandoned = abandoned
}
func (p *chunkPayloadData) setAllInflight() {
if p.endingFragment {
if p.head != nil {
p.head._allInflight = true
} else {
p._allInflight = true
}
}
}
func (p *chunkPayloadData) isFragmented() bool {
return !(p.head == nil && p.beginningFragment && p.endingFragment)
}