-
Notifications
You must be signed in to change notification settings - Fork 16
/
frame.go
708 lines (591 loc) · 15.7 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
package minq
import (
"fmt"
"time"
"github.com/bifurcation/mint/syntax"
)
type frameType uint8
type frameNonSyntax interface {
unmarshal(b []byte) (int, error)
}
const (
kFrameTypePadding = frameType(0x0)
kFrameTypeRstStream = frameType(0x1)
kFrameTypeConnectionClose = frameType(0x2)
kFrameTypeApplicationClose = frameType(0x3)
kFrameTypeMaxData = frameType(0x4)
kFrameTypeMaxStreamData = frameType(0x5)
kFrameTypeMaxStreamId = frameType(0x6)
kFrameTypePing = frameType(0x7)
kFrameTypeBlocked = frameType(0x8)
kFrameTypeStreamBlocked = frameType(0x9)
kFrameTypeStreamIdBlocked = frameType(0xa)
kFrameTypeNewConnectionId = frameType(0xb)
kFrameTypeStopSending = frameType(0xc)
kFrameTypeAck = frameType(0x1a)
kFrameTypeAckECN = frameType(0x1b)
kFrameTypePathChallenge = frameType(0xe)
kFrameTypePathResponse = frameType(0xf)
kFrameTypeStream = frameType(0x10)
kFrameTypeStreamMax = frameType(0x17)
kFrameTypeCryptoHs = frameType(0x18)
)
const (
kFrameTypeStreamFlagFIN = frameType(0x01)
kFrameTypeStreamFlagLEN = frameType(0x02)
kFrameTypeStreamFlagOFF = frameType(0x04)
)
const (
// Assume maximal sizes for these.
kMaxAckHeaderLength = 33
kMaxAckBlockEntryLength = 16
kMaxAckGap = 255
kMaxAckBlocks = 255
)
type innerFrame interface {
getType() frameType
String() string
}
type frame struct {
stream uint64
f innerFrame
encoded []byte
pns []uint64
lostPns []uint64
time time.Time
needsTransmit bool
}
func (f frame) String() string {
return f.f.String()
}
func newFrame(stream uint64, inner innerFrame) *frame {
return &frame{stream, inner, nil, nil, nil, time.Unix(0, 0), true}
}
// Encode internally if not already encoded.
func (f *frame) encode() error {
if f.encoded != nil {
return nil
}
var err error
f.encoded, err = syntax.Marshal(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
var n int
var err error
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(kFrameTypeApplicationClose):
inner = &applicationCloseFrame{}
case t == uint8(kFrameTypeMaxData):
inner = &maxDataFrame{}
case t == uint8(kFrameTypeMaxStreamData):
inner = &maxStreamDataFrame{}
case t == uint8(kFrameTypeMaxStreamId):
inner = &maxStreamIdFrame{}
case t == uint8(kFrameTypePing):
inner = &pingFrame{}
case t == uint8(kFrameTypeBlocked):
inner = &blockedFrame{}
case t == uint8(kFrameTypeStreamBlocked):
inner = &streamBlockedFrame{}
case t == uint8(kFrameTypeStreamIdBlocked):
inner = &streamIdBlockedFrame{}
case t == uint8(kFrameTypeNewConnectionId):
inner = &newConnectionIdFrame{}
case t == uint8(kFrameTypeStopSending):
inner = &stopSendingFrame{}
case t == uint8(kFrameTypeAck):
inner = &ackFrame{}
case t == uint8(kFrameTypePathChallenge):
inner = &pathChallengeFrame{}
case t == uint8(kFrameTypePathResponse):
inner = &pathResponseFrame{}
case t >= uint8(kFrameTypeStream) && t <= uint8(kFrameTypeStreamMax):
inner = &streamFrame{}
case t == uint8(kFrameTypeCryptoHs):
inner = &cryptoHsFrame{}
default:
logf(logTypeConnection, "Unknown frame type %v", t)
return 0, nil, fmt.Errorf("Received unknown frame type: %v", t)
}
ns, ok := inner.(frameNonSyntax)
if ok {
n, err = ns.unmarshal(data)
} else {
n, err = syntax.Unmarshal(data, inner)
}
if err != nil {
return 0, nil, err
}
return uintptr(n), &frame{0, inner, data[:n], nil, nil, time.Now(), false}, nil
}
// Frame definitions below this point.
// PADDING
type paddingFrame struct {
Typ frameType
}
func (f paddingFrame) String() string {
return "P"
}
func (f paddingFrame) getType() frameType {
return kFrameTypePadding
}
func newPaddingFrame(stream uint64) *frame {
return newFrame(stream, &paddingFrame{0})
}
// RST_STREAM
type rstStreamFrame struct {
Type frameType
StreamId uint64 `tls:"varint"`
ErrorCode uint16
FinalOffset uint64 `tls:"varint"`
}
func (f rstStreamFrame) String() string {
return fmt.Sprintf("RST_STREAM stream=%x errorCode=%d finalOffset=%x", f.StreamId, f.ErrorCode, f.FinalOffset)
}
func (f rstStreamFrame) getType() frameType {
return kFrameTypeRstStream
}
func newRstStreamFrame(streamId uint64, errorCode uint16, finalOffset uint64) *frame {
return newFrame(streamId, &rstStreamFrame{
kFrameTypeRstStream,
uint64(streamId),
errorCode,
finalOffset})
}
// STOP_SENDING
type stopSendingFrame struct {
Type frameType
StreamId uint64 `tls:"varint"`
ErrorCode uint16
}
func (f stopSendingFrame) String() string {
return fmt.Sprintf("STOP_SENDING stream=%x errorCode=%d", f.StreamId, f.ErrorCode)
}
func (f stopSendingFrame) getType() frameType {
return kFrameTypeStopSending
}
func newStopSendingFrame(streamId uint64, errorCode uint16) *frame {
return newFrame(streamId, &stopSendingFrame{
kFrameTypeStopSending,
uint64(streamId),
errorCode})
}
// CONNECTION_CLOSE
type connectionCloseFrame struct {
Type frameType
ErrorCode uint16
ReasonPhrase []byte `tls:"head=varint"`
}
func (f connectionCloseFrame) String() string {
return fmt.Sprintf("CONNECTION_CLOSE errorCode=%x", f.ErrorCode)
}
func (f connectionCloseFrame) getType() frameType {
return kFrameTypeConnectionClose
}
func newConnectionCloseFrame(errcode ErrorCode, reason string) *frame {
return newFrame(0, &connectionCloseFrame{
kFrameTypeConnectionClose,
uint16(errcode),
[]byte(reason),
})
}
// APPLICATION_CLOSE
type applicationCloseFrame struct {
Type frameType
ErrorCode uint16
ReasonPhrase []byte `tls:"head=varint"`
}
func (f applicationCloseFrame) String() string {
return fmt.Sprintf("APPLICATION_CLOSE errorCode=%x", f.ErrorCode)
}
func (f applicationCloseFrame) getType() frameType {
return kFrameTypeApplicationClose
}
func newApplicationCloseFrame(errcode uint16, reason string) *frame {
return newFrame(0, &applicationCloseFrame{
kFrameTypeApplicationClose,
uint16(errcode),
[]byte(reason),
})
}
// MAX_DATA
type maxDataFrame struct {
Type frameType
MaximumData uint64 `tls:"varint"`
}
func (f maxDataFrame) String() string {
return fmt.Sprintf("MAX_DATA %d", f.MaximumData)
}
func (f maxDataFrame) getType() frameType {
return kFrameTypeMaxData
}
func newMaxData(m uint64) *frame {
return newFrame(0, &maxDataFrame{kFrameTypeMaxData, m})
}
// MAX_STREAM_DATA
type maxStreamDataFrame struct {
Type frameType
StreamId uint64 `tls:"varint"`
MaximumStreamData uint64 `tls:"varint"`
}
func newMaxStreamData(stream uint64, offset uint64) *frame {
return newFrame(stream,
&maxStreamDataFrame{
kFrameTypeMaxStreamData,
stream,
offset,
})
}
func (f maxStreamDataFrame) String() string {
return fmt.Sprintf("MAX_STREAM_DATA stream=%d %d", f.StreamId, f.MaximumStreamData)
}
func (f maxStreamDataFrame) getType() frameType {
return kFrameTypeMaxStreamData
}
// MAX_STREAM_ID
type maxStreamIdFrame struct {
Type frameType
MaximumStreamId uint64 `tls:"varint"`
}
func newMaxStreamId(id uint64) *frame {
return newFrame(0,
&maxStreamIdFrame{
kFrameTypeMaxStreamId,
id,
})
}
func (f maxStreamIdFrame) String() string {
return fmt.Sprintf("MAX_STREAM_ID %d", f.MaximumStreamId)
}
func (f maxStreamIdFrame) getType() frameType {
return kFrameTypeMaxStreamId
}
// PING
type pingFrame struct {
Type frameType
}
func (f pingFrame) String() string {
return "PING"
}
func (f pingFrame) getType() frameType {
return kFrameTypePing
}
// BLOCKED
type blockedFrame struct {
Type frameType
Offset uint64 `tls:"varint"`
}
func (f blockedFrame) String() string {
return "BLOCKED"
}
func (f blockedFrame) getType() frameType {
return kFrameTypeBlocked
}
func newBlockedFrame(offset uint64) *frame {
return newFrame(0, &blockedFrame{kFrameTypeBlocked, offset})
}
// STREAM_BLOCKED
type streamBlockedFrame struct {
Type frameType
StreamId uint64 `tls:"varint"`
Offset uint64 `tls:"varint"`
}
func (f streamBlockedFrame) String() string {
return "STREAM_BLOCKED"
}
func (f streamBlockedFrame) getType() frameType {
return kFrameTypeStreamBlocked
}
func newStreamBlockedFrame(id uint64, offset uint64) *frame {
return newFrame(0, &streamBlockedFrame{kFrameTypeStreamBlocked, id, offset})
}
// STREAM_ID_BLOCKED
type streamIdBlockedFrame struct {
Type frameType
StreamId uint64 `tls:"varint"`
}
func (f streamIdBlockedFrame) String() string {
return "STREAM_ID_BLOCKED"
}
func (f streamIdBlockedFrame) getType() frameType {
return kFrameTypeStreamIdBlocked
}
func newStreamIdBlockedFrame(id uint64) *frame {
return newFrame(0, &streamIdBlockedFrame{
kFrameTypeStreamIdBlocked,
id})
}
// NEW_CONNECTION_ID
type newConnectionIdFrame struct {
Type frameType
Sequence uint16 `tls:"varint"`
ConnectionId ConnectionId
ResetToken [16]byte
}
func (f newConnectionIdFrame) String() string {
return fmt.Sprintf("NEW_CONNECTION_ID %d=%x", f.Sequence, f.ConnectionId)
}
func (f newConnectionIdFrame) getType() frameType {
return kFrameTypeNewConnectionId
}
func newNewConnectionIdFrame(seq uint16, cid ConnectionId, resetToken []byte) *frame {
f := &newConnectionIdFrame{
Type: kFrameTypeNewConnectionId,
Sequence: seq,
ConnectionId: cid,
}
assert(len(resetToken) == len(f.ResetToken))
copy(f.ResetToken[:], resetToken)
return newFrame(0, f)
}
// ACK
type ackBlock struct {
Gap uint64 `tls:"varint"`
Length uint64 `tls:"varint"`
}
type ackFrameHeader struct {
Type frameType
LargestAcknowledged uint64 `tls:"varint"`
AckDelay uint64 `tls:"varint"`
AckBlockCount uint64 `tls:"varint"`
FirstAckBlock uint64 `tls:"varint"`
}
type ackFrame struct {
ackFrameHeader
AckBlockSection []*ackBlock `tls:"head=none"`
}
func (f ackFrame) String() string {
return fmt.Sprintf("ACK numBlocks=%d largestAck=%x", f.AckBlockCount, f.LargestAcknowledged)
}
func (f ackFrame) getType() frameType {
return kFrameTypeAck
}
// ACK frames can't presently be decoded with syntax, so we need
// a custom decoder.
func (f *ackFrame) unmarshal(buf []byte) (int, error) {
// First, decode the header
read := int(0)
n, err := syntax.Unmarshal(buf, &f.ackFrameHeader)
if err != nil {
return 0, err
}
buf = buf[n:]
read += n
// Now decode each block
for i := uint64(0); i < f.AckBlockCount; i++ {
blk := &ackBlock{}
n, err := syntax.Unmarshal(buf, blk)
if err != nil {
return 0, err
}
buf = buf[n:]
read += n
f.AckBlockSection = append(f.AckBlockSection, blk)
}
return read, nil
}
func newAckFrame(recvd *recvdPackets, rs ackRanges, left int) (*frame, int, error) {
if left < kMaxAckHeaderLength {
return nil, 0, nil
}
logf(logTypeFrame, "Making ACK frame %v", rs)
left -= kMaxAckHeaderLength
last := rs[0].lastPacket
largestAckData, ok := recvd.packets[last]
// Should always be there. Packets only get removed after being set to ack2,
// which means we should not be acking it again.
assert(ok)
// FIRST, fill in the basic info of the ACK frame
var f ackFrame
f.Type = kFrameTypeAck
f.LargestAcknowledged = last
delay := time.Since(largestAckData.t).Nanoseconds()
f.AckDelay = uint64(delay) / 1000 >> kTpDefaultAckDelayExponent
f.AckBlockCount = 0
f.FirstAckBlock = rs[0].count - 1
// ...and account for the first block.
last -= f.FirstAckBlock
addedRanges := 1
// SECOND, add the remaining ACK blocks that fit and that we have
for (left > 0) && (addedRanges < len(rs)) {
// calculate blocks needed for the next range
gap := last - rs[addedRanges].lastPacket - 1
gap = last - rs[addedRanges].lastPacket - 1
b := &ackBlock{
gap,
rs[addedRanges].count - 1,
}
last = rs[addedRanges].lastPacket - rs[addedRanges].count
f.AckBlockCount++
f.AckBlockSection = append(f.AckBlockSection, b)
addedRanges++
left -= kMaxAckBlockEntryLength // Assume worst-case.
}
return newFrame(0, &f), addedRanges, nil
}
// PATH_CHALLENGE
type pathChallengeFrame struct {
Type frameType
Data [8]byte
}
func (f pathChallengeFrame) String() string {
return "PATH_CHALLENGE"
}
func (f pathChallengeFrame) getType() frameType {
return kFrameTypePathChallenge
}
func newPathChallengeFrame(data []byte) *frame {
payload := &pathChallengeFrame{Type: kFrameTypePathChallenge}
assert(len(data) == len(payload.Data))
copy(payload.Data[:], data)
return newFrame(0, payload)
}
// PATH_RESPONSE
type pathResponseFrame struct {
Type frameType
Data [8]byte
}
func (f pathResponseFrame) String() string {
return "PATH_RESPONSE"
}
func (f pathResponseFrame) getType() frameType {
return kFrameTypePathResponse
}
func newPathResponseFrame(data []byte) *frame {
payload := &pathResponseFrame{Type: kFrameTypePathResponse}
assert(len(data) == len(payload.Data))
copy(payload.Data[:], data)
return newFrame(0, payload)
}
// STREAM
type streamFrame struct {
Typ frameType
StreamId uint64 `tls:"varint"`
Offset uint64 `tls:"varint"`
Data []byte `tls:"head=varint"`
}
func (f streamFrame) String() string {
return fmt.Sprintf("STREAM stream=%d offset=%d len=%d FIN=%v", f.StreamId, f.Offset, len(f.Data), f.hasFin())
}
func (f streamFrame) getType() frameType {
return kFrameTypeStream
}
func (f streamFrame) hasFin() bool {
if f.Typ&kFrameTypeStreamFlagFIN == 0 {
return false
}
return true
}
func newStreamFrame(stream uint64, offset uint64, data []byte, last bool) *frame {
logf(logTypeFrame, "Creating stream frame with data length=%d", len(data))
assert(len(data) <= 65535)
// TODO([email protected]): One might want to allow non
// D bit, but not for now.
// Set all of SSOO to 1
typ := kFrameTypeStream | kFrameTypeStreamFlagLEN | kFrameTypeStreamFlagOFF
if last {
typ |= kFrameTypeStreamFlagFIN
}
return newFrame(
stream,
&streamFrame{
typ,
stream,
offset,
dup(data),
})
}
func decodeVarint(buf []byte) (int, uint64, error) {
var vi struct {
Val uint64 `tls:"varint"`
}
n, err := syntax.Unmarshal(buf, &vi)
if err != nil {
return 0, 0, err
}
return n, vi.Val, nil
}
// Stream frames can't presently be decoded with syntax, so we need
// a custom decoder.
func (f *streamFrame) unmarshal(buf []byte) (int, error) {
f.Typ = frameType(buf[0])
buf = buf[1:]
var read = int(1)
var n int
var err error
n, f.StreamId, err = decodeVarint(buf)
if err != nil {
return 0, err
}
buf = buf[n:]
read += n
if f.Typ&kFrameTypeStreamFlagOFF != 0 {
n, f.Offset, err = decodeVarint(buf)
if err != nil {
return 0, err
}
buf = buf[n:]
read += n
}
if f.Typ&kFrameTypeStreamFlagLEN != 0 {
var l uint64
n, l, err = decodeVarint(buf)
if err != nil {
return 0, err
}
buf = buf[n:]
read += n
logf(logTypeFrame, "Expecting %v bytes", l)
if l > uint64(len(buf)) {
return 0, fmt.Errorf("Insufficient bytes left")
}
f.Data = dup(buf[:l])
read += int(l)
} else {
f.Data = dup(buf)
read += len(buf)
}
return read, nil
}
// CRYPTO_HS
type cryptoHsFrame struct {
Typ frameType
Offset uint64 `tls:"varint"`
Data []byte `tls:"head=varint"`
}
func (f cryptoHsFrame) getType() frameType {
return kFrameTypeCryptoHs
}
func (f cryptoHsFrame) String() string {
return fmt.Sprintf("CRYPTO_HS len=%d", len(f.Data))
}
func newCryptoHsFrame(offset uint64, data []byte) *frame {
logf(logTypeFrame, "Creating crypto_hs frame with data length=%d", len(data))
return newFrame(
0,
&cryptoHsFrame{
kFrameTypeCryptoHs,
offset,
dup(data),
},
)
}