This repository has been archived by the owner on Apr 21, 2021. It is now read-only.
forked from ekr/minq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame.go
403 lines (340 loc) · 8.44 KB
/
frame.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package minq
import (
"fmt"
)
type frameType uint8
const (
kFrameTypePadding = frameType(0x0)
kFrameTypeRstStream = frameType(0x1)
kFrameTypeConnectionClose = frameType(0x2)
kFrameTypeGoaway = frameType(0x3)
kFrameTypeMaxData = frameType(0x4)
kFrameTypeMaxStreamData = frameType(0x5)
kFrameTypeMaxStreamId = frameType(0x6)
kFrameTypePing = frameType(0x7)
kFrameTypeBlocked = frameType(0x8)
kFrameTypeStreamBlocked = frameType(0x9)
kFrameTypeStreamIdNeeded = frameType(0xa)
kFrameTypeNewConnectionId = frameType(0xb)
kFrameTypeAck = frameType(0xa0)
kFrameTypeStream = frameType(0xc0)
)
const (
kFrameTypeFlagF = frameType(0x40)
kFrameTypeFlagD = frameType(0x01)
)
type innerFrame interface {
getType() frameType
}
type frame struct {
stream uint32
f innerFrame
encoded []byte
}
// Encode internally if not already encoded.
func (f *frame) encode() error {
if f.encoded != nil {
return nil
}
var err error
f.encoded, err = encode(f.f)
logf(logTypeFrame, "Frame encoded, total length=%v", len(f.encoded))
return err
}
func (f *frame) length() (int, error) {
err := f.encode()
if err != nil {
return 0, err
}
return len(f.encoded), nil
}
// Decode an arbitrary frame.
func decodeFrame(data []byte) (uintptr, *frame, error) {
var inner innerFrame
t := data[0]
logf(logTypeFrame, "Frame type byte %v", t)
switch {
case t == uint8(kFrameTypePadding):
inner = &paddingFrame{}
case t == uint8(kFrameTypeRstStream):
inner = &rstStreamFrame{}
case t == uint8(kFrameTypeConnectionClose):
inner = &connectionCloseFrame{}
case t == uint8(kFrameTypeGoaway):
inner = &goawayFrame{}
case t == uint8(kFrameTypeMaxData):
inner = &maxDataFrame{}
case t == uint8(kFrameTypeMaxStreamData):
inner = &maxStreamDataFrame{}
case t == uint8(kFrameTypePing):
inner = &pingFrame{}
case t == uint8(kFrameTypeBlocked):
inner = &blockedFrame{}
case t == uint8(kFrameTypeStreamBlocked):
inner = &streamBlockedFrame{}
case t == uint8(kFrameTypeStreamIdNeeded):
inner = &streamIdNeededFrame{}
case t == uint8(kFrameTypeNewConnectionId):
inner = &newConnectionIdFrame{}
case t >= uint8(kFrameTypeAck) && t <= 0xbf:
inner = &ackFrame{}
case t >= uint8(kFrameTypeStream):
inner = &streamFrame{}
default:
logf(logTypeConnection, "Unknown frame type %v", t)
return 0, nil, fmt.Errorf("Received unknwon frame type: %v", t)
}
n, err := decode(inner, data)
if err != nil {
return 0, nil, err
}
return n, &frame{0, inner, data[:n]}, nil
}
// Frame definitions below this point.
// PADDING
type paddingFrame struct {
Typ frameType
}
func (f paddingFrame) getType() frameType {
return kFrameTypePadding
}
func newPaddingFrame(stream uint32) frame {
return frame{stream, &paddingFrame{0}, nil}
}
// RST_STREAM
type rstStreamFrame struct {
Type frameType
StreamId uint32
ErrorCode uint32
FinalOffset uint64
}
func (f rstStreamFrame) getType() frameType {
return kFrameTypeRstStream
}
// CONNECTION_CLOSE
type connectionCloseFrame struct {
Type frameType
ErrorCode uint32
ReasonPhraseLength uint16
ReasonPhrase []byte
}
func (f connectionCloseFrame) getType() frameType {
return kFrameTypeConnectionClose
}
func (f connectionCloseFrame) ReasonPhrase__length() uintptr {
return uintptr(f.ReasonPhraseLength)
}
func newConnectionCloseFrame(errcode ErrorCode, reason string) frame {
str := []byte(reason)
return frame{0, &connectionCloseFrame{
kFrameTypeConnectionClose,
uint32(errcode),
uint16(len(str)),
str}, nil}
}
// GOAWAY
type goawayFrame struct {
Type frameType
LargestClientStreamId uint32
LargestServerStreamId uint32
}
func (f goawayFrame) getType() frameType {
return kFrameTypeGoaway
}
func newGoawayFrame(client uint32, server uint32) frame {
return frame{0,
&goawayFrame{kFrameTypeGoaway, client, server},
nil,
}
}
// MAX_DATA
type maxDataFrame struct {
Type frameType
MaximumData uint64
}
func (f maxDataFrame) getType() frameType {
return kFrameTypeMaxData
}
// MAX_STREAM_DATA
type maxStreamDataFrame struct {
Type frameType
StreamId uint32
MaximumStreamData uint64
}
func (f maxStreamDataFrame) getType() frameType {
return kFrameTypeMaxStreamData
}
// MAX_STREAM_ID
type maxStreamIdFrame struct {
Type frameType
MaximumStreamId uint32
}
func (f maxStreamIdFrame) getType() frameType {
return kFrameTypeMaxStreamId
}
// PING
type pingFrame struct {
Type frameType
}
func (f pingFrame) getType() frameType {
return kFrameTypePing
}
// BLOCKED
type blockedFrame struct {
Type frameType
}
func (f blockedFrame) getType() frameType {
return kFrameTypeBlocked
}
// STREAM_BLOCKED
type streamBlockedFrame struct {
Type frameType
StreamId uint32
}
func (f streamBlockedFrame) getType() frameType {
return kFrameTypeStreamBlocked
}
// STREAM_ID_NEEDED
type streamIdNeededFrame struct {
Type frameType
}
func (f streamIdNeededFrame) getType() frameType {
return kFrameTypeStreamIdNeeded
}
// NEW_CONNECTION_ID
type newConnectionIdFrame struct {
Type frameType
Sequence uint16
ConnectionId uint64
}
func (f newConnectionIdFrame) getType() frameType {
return kFrameTypeNewConnectionId
}
// ACK
type ackBlock struct {
lengthLength uintptr
Gap uint8
Length uint64
}
func (f ackBlock) Length__length() uintptr {
return f.lengthLength
}
type ackFrame struct {
Type frameType
NumBlocks uint8
NumTS uint8
LargestAcknowledged uint64
AckDelay uint16
FirstAckBlockLength uint64
AckBlockSection []byte
TimestampSection []byte
}
func (f ackFrame) getType() frameType {
return kFrameTypeAck
}
func (f ackFrame) NumBlocks__length() uintptr {
if f.Type&0x10 == 0 {
return 0
}
return 1
}
func ackFieldsLength(b byte) uintptr {
return []uintptr{1, 2, 4, 8}[b]
}
func (f ackFrame) LargestAcknowledged__length() uintptr {
return ackFieldsLength((byte(f.Type) >> 2) & 0x3)
}
func (f ackFrame) FirstAckBlockLength__length() uintptr {
return ackFieldsLength(byte(f.Type) & 0x3)
}
func (f ackFrame) AckBlockSection__length() uintptr {
return uintptr(f.NumBlocks) * (1 + f.LargestAcknowledged__length())
}
func (f ackFrame) TimestampSection__length() uintptr {
return uintptr(f.NumTS * 5)
}
func newAckFrame(rs []ackRange) (*frame, error) {
logf(logTypeFrame, "Making ACK frame %v", rs)
var f ackFrame
f.Type = kFrameTypeAck | 0xa
if len(rs) > 1 {
f.Type |= 0x10
f.NumBlocks = uint8(len(rs) - 1)
}
f.LargestAcknowledged = rs[0].lastPacket
f.FirstAckBlockLength = rs[0].count - 1
last := f.LargestAcknowledged - f.FirstAckBlockLength
// TODO([email protected]): Fill in any of the timestamp stuff.
f.AckDelay = 0
f.NumTS = 0
f.TimestampSection = nil
for i := 1; i < len(rs); i++ {
gap := last - rs[i].lastPacket
assert(gap < 256) // TODO([email protected]): handle this.
b := &ackBlock{
4, // Fixed 32-bit width (see 0xb above)
uint8(last - rs[i].lastPacket),
rs[i].count,
}
last = rs[i].lastPacket - rs[i].count + 1
encoded, err := encode(b)
if err != nil {
return nil, err
}
f.AckBlockSection = append(f.AckBlockSection, encoded...)
}
return &frame{0, &f, nil}, nil
}
// STREAM
type streamFrame struct {
Typ frameType
StreamId uint32
Offset uint64
DataLength uint16
Data []byte
}
func (f streamFrame) getType() frameType {
return kFrameTypeStream
}
func (f streamFrame) DataLength__length() uintptr {
logf(logTypeFrame, "DataLength__length() called")
if (f.Typ & kFrameTypeFlagD) == 0 {
return 0
}
logf(logTypeFrame, "DataLength__length() returning 2")
return 2
}
func (f streamFrame) StreamId__length() uintptr {
lengths := []uintptr{1, 2, 3, 4}
val := (f.Typ >> 3) & 0x03
return lengths[val]
}
func (f streamFrame) Offset__length() uintptr {
lengths := []uintptr{0, 2, 4, 8}
val := (f.Typ >> 1) & 0x03
return lengths[val]
}
func (f streamFrame) Data__length() uintptr {
if f.DataLength__length() == 0 {
return codecDefaultSize
}
return uintptr(f.DataLength)
}
func newStreamFrame(stream uint32, offset uint64, data []byte) frame {
logf(logTypeFrame, "Creating stream frame with data length=%d", len(data))
assert(len(data) <= 65535)
return frame{
stream,
&streamFrame{
// TODO([email protected]): One might want to allow non
// D bit, but not for now.
// Set all of SSOO to 1
kFrameTypeStream | 0x1e | kFrameTypeFlagD,
uint32(stream),
offset,
uint16(len(data)),
dup(data),
},
nil,
}
}