This repository was archived by the owner on Aug 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathframes.go
More file actions
685 lines (643 loc) · 23.5 KB
/
frames.go
File metadata and controls
685 lines (643 loc) · 23.5 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
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
/*
Maxime Piraux's master's thesis
Copyright (C) 2017-2018 Maxime Piraux
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License version 3
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package masterthesis
import (
"bytes"
"encoding/binary"
"io"
"fmt"
"errors"
. "github.com/mpiraux/master-thesis/lib"
)
type Frame interface {
FrameType() FrameType
writeTo(buffer *bytes.Buffer)
shouldBeRetransmitted() bool
FrameLength() uint16
}
func NewFrame(buffer *bytes.Reader, conn *Connection) (Frame, error) {
typeByte, err := buffer.ReadByte()
if err == io.EOF {
return nil, nil
} else if err != nil {
return nil, err
}
buffer.UnreadByte()
frameType := FrameType(typeByte)
switch {
case frameType == PaddingFrameType:
return Frame(NewPaddingFrame(buffer)), nil
case frameType == ResetStreamType:
return Frame(NewResetStream(buffer)), nil
case frameType == ConnectionCloseType:
return Frame(NewConnectionCloseFrame(buffer)), nil
case frameType == ApplicationCloseType:
return Frame(NewApplicationCloseFrame(buffer)), nil
case frameType == MaxDataType:
return Frame(NewMaxDataFrame(buffer)), nil
case frameType == MaxStreamDataType:
return Frame(NewMaxStreamDataFrame(buffer)), nil
case frameType == MaxStreamIdType:
return Frame(NewMaxStreamIdFrame(buffer)), nil
case frameType == PingType:
return Frame(NewPingFrame(buffer)), nil
case frameType == BlockedType:
return Frame(NewBlockedFrame(buffer)), nil
case frameType == StreamBlockedType:
return Frame(NewStreamBlockedFrame(buffer)), nil
case frameType == StreamIdBlockedType:
return Frame(NewStreamIdNeededFrame(buffer)), nil
case frameType == NewConnectionIdType:
return Frame(NewNewConnectionIdFrame(buffer)), nil
case frameType == StopSendingType:
return Frame(NewStopSendingFrame(buffer)), nil
case frameType == AckType:
return Frame(ReadAckFrame(buffer)), nil
case frameType == PathChallengeType:
return Frame(ReadPathChallenge(buffer)), nil
case frameType == PathResponseType:
return Frame(ReadPathResponse(buffer)), nil
case (frameType & StreamType) == StreamType && frameType <= 0x17:
return Frame(ReadStreamFrame(buffer, conn)), nil
case frameType == CryptoType:
return Frame(ReadCryptoFrame(buffer, conn)), nil
case frameType == NewTokenType:
return Frame(ReadNewTokenFrame(buffer, conn)), nil
case frameType == AckECNType:
return Frame(ReadAckECNFrame(buffer, conn)), nil
default:
return nil, errors.New(fmt.Sprintf("Unknown frame type %d", typeByte))
}
}
type FrameType uint64
const (
PaddingFrameType FrameType = 0x00
ResetStreamType = 0x01
ConnectionCloseType = 0x02
ApplicationCloseType = 0x03
MaxDataType = 0x04
MaxStreamDataType = 0x05
MaxStreamIdType = 0x06
PingType = 0x07
BlockedType = 0x08
StreamBlockedType = 0x09
StreamIdBlockedType = 0x0a
NewConnectionIdType = 0x0b
StopSendingType = 0x0c
AckType = 0x0d
PathChallengeType = 0x0e
PathResponseType = 0x0f
StreamType = 0x10
CryptoType = 0x18
NewTokenType = 0x19
AckECNType = 0x20
)
type PaddingFrame byte
func (frame PaddingFrame) FrameType() FrameType { return PaddingFrameType }
func (frame PaddingFrame) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
}
func (frame PaddingFrame) shouldBeRetransmitted() bool { return false }
func (frame PaddingFrame) FrameLength() uint16 { return 1 }
func NewPaddingFrame(buffer *bytes.Reader) *PaddingFrame {
buffer.ReadByte() // Discard frame payload
return new(PaddingFrame)
}
type ResetStream struct {
StreamId uint64
ErrorCode uint16
FinalOffset uint64
}
func (frame ResetStream) FrameType() FrameType { return ResetStreamType }
func (frame ResetStream) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.StreamId)
binary.Write(buffer, binary.BigEndian, frame.ErrorCode)
WriteVarInt(buffer, frame.FinalOffset)
}
func (frame ResetStream) shouldBeRetransmitted() bool { return true }
func (frame ResetStream) FrameLength() uint16 { return 1 + uint16(VarIntLen(frame.StreamId) + 2 + VarIntLen(frame.FinalOffset)) }
func NewResetStream(buffer *bytes.Reader) *ResetStream {
frame := new(ResetStream)
buffer.ReadByte() // Discard frame type
frame.StreamId, _ = ReadVarInt(buffer)
binary.Read(buffer, binary.BigEndian, &frame.ErrorCode)
frame.FinalOffset, _ = ReadVarInt(buffer)
return frame
}
type ConnectionCloseFrame struct {
ErrorCode uint16
ErrorFrameType uint64
ReasonPhraseLength uint64
ReasonPhrase string
}
func (frame ConnectionCloseFrame) FrameType() FrameType { return ConnectionCloseType }
func (frame ConnectionCloseFrame) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
binary.Write(buffer, binary.BigEndian, frame.ErrorCode)
WriteVarInt(buffer, frame.ErrorFrameType)
WriteVarInt(buffer, frame.ReasonPhraseLength)
if frame.ReasonPhraseLength > 0 {
buffer.Write([]byte(frame.ReasonPhrase))
}
}
func (frame ConnectionCloseFrame) shouldBeRetransmitted() bool { return false }
func (frame ConnectionCloseFrame) FrameLength() uint16 { return 1 + 2 + uint16(VarIntLen(frame.ErrorFrameType) + VarIntLen(frame.ReasonPhraseLength)) + uint16(frame.ReasonPhraseLength) }
func NewConnectionCloseFrame(buffer *bytes.Reader) *ConnectionCloseFrame {
frame := new(ConnectionCloseFrame)
buffer.ReadByte() // Discard frame type
binary.Read(buffer, binary.BigEndian, &frame.ErrorCode)
frame.ErrorFrameType, _ = ReadVarInt(buffer)
frame.ReasonPhraseLength, _ = ReadVarInt(buffer)
if frame.ReasonPhraseLength > 0 {
reasonBytes := make([]byte, frame.ReasonPhraseLength, frame.ReasonPhraseLength)
binary.Read(buffer, binary.BigEndian, &reasonBytes)
frame.ReasonPhrase = string(reasonBytes)
}
return frame
}
type ApplicationCloseFrame struct {
errorCode uint16
reasonPhraseLength uint64
reasonPhrase string
}
func (frame ApplicationCloseFrame) FrameType() FrameType { return ApplicationCloseType }
func (frame ApplicationCloseFrame) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
binary.Write(buffer, binary.BigEndian, frame.errorCode)
WriteVarInt(buffer, frame.reasonPhraseLength)
if frame.reasonPhraseLength > 0 {
buffer.Write([]byte(frame.reasonPhrase))
}
}
func (frame ApplicationCloseFrame) shouldBeRetransmitted() bool { return false }
func (frame ApplicationCloseFrame) FrameLength() uint16 { return 1 + 2 + uint16(VarIntLen(frame.reasonPhraseLength)) + uint16(frame.reasonPhraseLength) }
func NewApplicationCloseFrame(buffer *bytes.Reader) *ApplicationCloseFrame {
frame := new(ApplicationCloseFrame)
buffer.ReadByte() // Discard frame type
binary.Read(buffer, binary.BigEndian, &frame.errorCode)
frame.reasonPhraseLength, _ = ReadVarInt(buffer)
if frame.reasonPhraseLength > 0 {
reasonBytes := make([]byte, frame.reasonPhraseLength, frame.reasonPhraseLength)
binary.Read(buffer, binary.BigEndian, &reasonBytes)
frame.reasonPhrase = string(reasonBytes)
}
return frame
}
type MaxDataFrame struct {
MaximumData uint64
}
func (frame MaxDataFrame) FrameType() FrameType { return MaxDataType }
func (frame MaxDataFrame) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.MaximumData)
}
func (frame MaxDataFrame) shouldBeRetransmitted() bool { return true }
func (frame MaxDataFrame) FrameLength() uint16 { return 1 + uint16(VarIntLen(frame.MaximumData)) }
func NewMaxDataFrame(buffer *bytes.Reader) *MaxDataFrame {
frame := new(MaxDataFrame)
buffer.ReadByte() // Discard frame type
frame.MaximumData, _ = ReadVarInt(buffer)
return frame
}
type MaxStreamDataFrame struct {
StreamId uint64
MaximumStreamData uint64
}
func (frame MaxStreamDataFrame) FrameType() FrameType { return MaxStreamDataType }
func (frame MaxStreamDataFrame) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.StreamId)
WriteVarInt(buffer, frame.MaximumStreamData)
}
func (frame MaxStreamDataFrame) shouldBeRetransmitted() bool { return true }
func (frame MaxStreamDataFrame) FrameLength() uint16 { return 1 + uint16(VarIntLen(frame.StreamId) + VarIntLen(frame.MaximumStreamData)) }
func NewMaxStreamDataFrame(buffer *bytes.Reader) *MaxStreamDataFrame {
frame := new(MaxStreamDataFrame)
buffer.ReadByte() // Discard frame type
frame.StreamId, _ = ReadVarInt(buffer)
frame.MaximumStreamData, _ = ReadVarInt(buffer)
return frame
}
type MaxStreamIdFrame struct {
maximumStreamId uint64
}
func (frame MaxStreamIdFrame) FrameType() FrameType { return MaxStreamIdType }
func (frame MaxStreamIdFrame) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.maximumStreamId)
}
func (frame MaxStreamIdFrame) shouldBeRetransmitted() bool { return true }
func (frame MaxStreamIdFrame) FrameLength() uint16 { return 1 + uint16(VarIntLen(frame.maximumStreamId)) }
func NewMaxStreamIdFrame(buffer *bytes.Reader) *MaxStreamIdFrame {
frame := new(MaxStreamIdFrame)
buffer.ReadByte() // Discard frame type
frame.maximumStreamId, _ = ReadVarInt(buffer)
return frame
}
type PingFrame byte
func (frame PingFrame) FrameType() FrameType { return PingType }
func (frame PingFrame) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
}
func (frame PingFrame) shouldBeRetransmitted() bool { return true }
func (frame PingFrame) FrameLength() uint16 { return 1 }
func NewPingFrame(buffer *bytes.Reader) *PingFrame {
frame := new(PingFrame)
buffer.ReadByte() // Discard frame type
return frame
}
type BlockedFrame struct {
offset uint64
}
func (frame BlockedFrame) FrameType() FrameType { return BlockedType }
func (frame BlockedFrame) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.offset)
}
func (frame BlockedFrame) shouldBeRetransmitted() bool { return true }
func (frame BlockedFrame) FrameLength() uint16 { return 1 + uint16(VarIntLen(frame.offset)) }
func NewBlockedFrame(buffer *bytes.Reader) *BlockedFrame {
frame := new(BlockedFrame)
buffer.ReadByte() // Discard frame type
frame.offset, _ = ReadVarInt(buffer)
return frame
}
type StreamBlockedFrame struct {
streamId uint64
offset uint64
}
func (frame StreamBlockedFrame) FrameType() FrameType { return StreamBlockedType }
func (frame StreamBlockedFrame) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.streamId)
WriteVarInt(buffer, frame.offset)
}
func (frame StreamBlockedFrame) shouldBeRetransmitted() bool { return true }
func (frame StreamBlockedFrame) FrameLength() uint16 { return 1 + uint16(VarIntLen(frame.streamId) + VarIntLen(frame.offset)) }
func NewStreamBlockedFrame(buffer *bytes.Reader) *StreamBlockedFrame {
frame := new(StreamBlockedFrame)
buffer.ReadByte() // Discard frame type
frame.streamId, _ = ReadVarInt(buffer)
frame.offset, _ = ReadVarInt(buffer)
return frame
}
type StreamIdBlockedFrame struct {
streamId uint64
}
func (frame StreamIdBlockedFrame) FrameType() FrameType { return StreamIdBlockedType }
func (frame StreamIdBlockedFrame) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.streamId)
}
func (frame StreamIdBlockedFrame) shouldBeRetransmitted() bool { return true }
func (frame StreamIdBlockedFrame) FrameLength() uint16 { return 1 + uint16(VarIntLen(frame.streamId)) }
func NewStreamIdNeededFrame(buffer *bytes.Reader) *StreamIdBlockedFrame {
frame := new(StreamIdBlockedFrame)
buffer.ReadByte() // Discard frame type
frame.streamId, _ = ReadVarInt(buffer)
return frame
}
type NewConnectionIdFrame struct {
Sequence uint64
Length uint8
ConnectionId []byte
StatelessResetToken [16]byte
}
func (frame NewConnectionIdFrame) FrameType() FrameType { return NewConnectionIdType }
func (frame NewConnectionIdFrame) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.Sequence)
buffer.WriteByte(frame.Length)
buffer.Write(frame.ConnectionId)
binary.Write(buffer, binary.BigEndian, frame.StatelessResetToken)
}
func (frame NewConnectionIdFrame) shouldBeRetransmitted() bool { return true }
func (frame NewConnectionIdFrame) FrameLength() uint16 { return 1 + uint16(VarIntLen(frame.Sequence)) + 1 + uint16(len(frame.ConnectionId)) + 16 }
func NewNewConnectionIdFrame(buffer *bytes.Reader) *NewConnectionIdFrame {
frame := new(NewConnectionIdFrame)
buffer.ReadByte() // Discard frame type
frame.Sequence, _ = ReadVarInt(buffer)
frame.Length, _ = buffer.ReadByte()
frame.ConnectionId = make([]byte, frame.Length, frame.Length)
buffer.Read(frame.ConnectionId)
binary.Read(buffer, binary.BigEndian, &frame.StatelessResetToken)
return frame
}
type StopSendingFrame struct {
StreamId uint64
ErrorCode uint16
}
func (frame StopSendingFrame) FrameType() FrameType { return StopSendingType }
func (frame StopSendingFrame) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.StreamId)
binary.Write(buffer, binary.BigEndian, frame.ErrorCode)
}
func (frame StopSendingFrame) shouldBeRetransmitted() bool { return true }
func (frame StopSendingFrame) FrameLength() uint16 { return 1 + uint16(VarIntLen(frame.StreamId)) + 2 }
func NewStopSendingFrame(buffer *bytes.Reader) *StopSendingFrame {
frame := new(StopSendingFrame)
buffer.ReadByte() // Discard frame type
frame.StreamId, _ = ReadVarInt(buffer)
binary.Read(buffer, binary.BigEndian, &frame.ErrorCode)
return frame
}
type AckFrame struct {
LargestAcknowledged uint64
AckDelay uint64
AckBlockCount uint64
AckBlocks []AckBlock
}
type AckBlock struct {
Gap uint64
Block uint64
}
func (frame AckFrame) FrameType() FrameType { return AckType }
func (frame AckFrame) shouldBeRetransmitted() bool { return false }
func (frame AckFrame) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.LargestAcknowledged)
WriteVarInt(buffer, frame.AckDelay)
WriteVarInt(buffer, frame.AckBlockCount)
for i, ack := range frame.AckBlocks {
if i > 0 {
WriteVarInt(buffer, ack.Gap)
}
WriteVarInt(buffer, ack.Block)
}
}
func (frame AckFrame) FrameLength() uint16 {
var length uint16
length += 1 + uint16(VarIntLen(frame.LargestAcknowledged) + VarIntLen(frame.AckDelay) + VarIntLen(frame.AckBlockCount))
for i, ack := range frame.AckBlocks {
if i > 0 {
length += uint16(VarIntLen(ack.Gap))
}
length += uint16(VarIntLen(ack.Block))
}
return length
}
func (frame AckFrame) GetAckedPackets() []uint64 { // TODO: This is prone to livelock
var packets []uint64
currentPacketNumber := frame.LargestAcknowledged
packets = append(packets, currentPacketNumber)
for i := uint64(0); i < frame.AckBlocks[0].Block; i++ {
currentPacketNumber--
packets = append(packets, currentPacketNumber)
}
for _, ackBlock := range frame.AckBlocks[1:] {
for i := uint64(0); i <= ackBlock.Gap; i++ { // See https://tools.ietf.org/html/draft-ietf-quic-transport-10#section-8.15.1
currentPacketNumber--
packets = append(packets, currentPacketNumber)
}
for i := uint64(0); i < ackBlock.Block; i++ {
currentPacketNumber--
packets = append(packets, currentPacketNumber)
}
}
return packets
}
func ReadAckFrame(buffer *bytes.Reader) *AckFrame {
frame := new(AckFrame)
buffer.ReadByte() // Discard frame byte
frame.LargestAcknowledged, _ = ReadVarInt(buffer)
frame.AckDelay, _ = ReadVarInt(buffer)
frame.AckBlockCount, _ = ReadVarInt(buffer)
firstBlock := AckBlock{}
firstBlock.Block, _ = ReadVarInt(buffer)
frame.AckBlocks = append(frame.AckBlocks, firstBlock)
var i uint64
for i = 0; i < frame.AckBlockCount; i++ {
ack := AckBlock{}
ack.Gap, _ = ReadVarInt(buffer)
ack.Block, _ = ReadVarInt(buffer)
frame.AckBlocks = append(frame.AckBlocks, ack)
}
return frame
}
func NewAckFrame(largestAcknowledged uint64, ackBlockCount uint64) *AckFrame {
frame := new(AckFrame)
frame.LargestAcknowledged = largestAcknowledged
frame.AckBlockCount = 0
frame.AckDelay = 0
frame.AckBlocks = append(frame.AckBlocks, AckBlock{0, ackBlockCount})
return frame
}
type PathChallenge struct {
Data [8]byte
}
func (frame PathChallenge) FrameType() FrameType { return PathChallengeType }
func (frame PathChallenge) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
buffer.Write(frame.Data[:])
}
func (frame PathChallenge) shouldBeRetransmitted() bool { return true }
func (frame PathChallenge) FrameLength() uint16 { return 1 + 8 }
func ReadPathChallenge(buffer *bytes.Reader) *PathChallenge {
frame := new(PathChallenge)
buffer.ReadByte() // Discard frame byte
buffer.Read(frame.Data[:])
return frame
}
type PathResponse struct {
Data [8]byte
}
func (frame PathResponse) FrameType() FrameType { return PathResponseType }
func (frame PathResponse) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
buffer.Write(frame.Data[:])
}
func (frame PathResponse) shouldBeRetransmitted() bool { return false }
func (frame PathResponse) FrameLength() uint16 { return 1 + 8 }
func ReadPathResponse(buffer *bytes.Reader) *PathResponse {
frame := new(PathResponse)
buffer.ReadByte() // Discard frame byte
buffer.Read(frame.Data[:])
return frame
}
func NewPathResponse(data [8]byte) *PathResponse {
frame := new(PathResponse)
frame.Data = data
return frame
}
type StreamFrame struct {
FinBit bool
LenBit bool
OffBit bool
StreamId uint64
Offset uint64
Length uint64
StreamData []byte
}
func (frame StreamFrame) FrameType() FrameType { return StreamType }
func (frame StreamFrame) writeTo(buffer *bytes.Buffer) {
typeByte := uint64(frame.FrameType())
if frame.FinBit {
typeByte |= 0x01
}
if frame.LenBit {
typeByte |= 0x02
}
if frame.OffBit {
typeByte |= 0x04
}
WriteVarInt(buffer, typeByte)
WriteVarInt(buffer, frame.StreamId)
if frame.OffBit {
WriteVarInt(buffer, frame.Offset)
}
if frame.LenBit {
WriteVarInt(buffer, frame.Length)
}
buffer.Write(frame.StreamData)
}
func (frame StreamFrame) shouldBeRetransmitted() bool { return true }
func (frame StreamFrame) FrameLength() uint16 {
length := 1 + uint16(VarIntLen(frame.StreamId))
if frame.OffBit {
length += uint16(VarIntLen(frame.Offset))
}
if frame.LenBit {
length += uint16(VarIntLen(frame.Length))
}
return length + uint16(len(frame.StreamData))
}
func ReadStreamFrame(buffer *bytes.Reader, conn *Connection) *StreamFrame {
frame := new(StreamFrame)
typeByte, _ := buffer.ReadByte()
frame.FinBit = (typeByte & 0x01) == 0x01
frame.LenBit = (typeByte & 0x02) == 0x02
frame.OffBit = (typeByte & 0x04) == 0x04
frame.StreamId, _ = ReadVarInt(buffer)
if frame.OffBit {
frame.Offset, _ = ReadVarInt(buffer)
}
if frame.LenBit {
frame.Length, _ = ReadVarInt(buffer)
} else {
frame.Length = uint64(buffer.Len())
}
frame.StreamData = make([]byte, frame.Length, frame.Length)
buffer.Read(frame.StreamData)
conn.Streams.Get(frame.StreamId).addToRead(frame)
return frame
}
func NewStreamFrame(streamId uint64, stream *Stream, data []byte, finBit bool) *StreamFrame {
frame := new(StreamFrame)
frame.StreamId = streamId
frame.FinBit = finBit
frame.LenBit = true
frame.Offset = stream.WriteOffset
frame.OffBit = frame.Offset > 0
frame.Length = uint64(len(data))
frame.StreamData = data
stream.WriteOffset += uint64(frame.Length)
stream.WriteData = append(stream.WriteData, data...)
stream.WriteClosed = frame.FinBit
return frame
}
type CryptoFrame struct {
Offset uint64
Length uint64
CryptoData []byte
}
func (frame CryptoFrame) FrameType() FrameType { return CryptoType }
func (frame CryptoFrame) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.Offset)
WriteVarInt(buffer, frame.Length)
buffer.Write(frame.CryptoData)
}
func (frame CryptoFrame) shouldBeRetransmitted() bool { return true }
func (frame CryptoFrame) FrameLength() uint16 { return 1 + uint16(VarIntLen(frame.Offset) + VarIntLen(frame.Length)) + uint16(len(frame.CryptoData))}
func ReadCryptoFrame(buffer *bytes.Reader, conn *Connection) *CryptoFrame {
frame := new(CryptoFrame)
ReadVarInt(buffer) // Discards frame type
frame.Offset, _ = ReadVarInt(buffer)
frame.Length, _ = ReadVarInt(buffer)
frame.CryptoData = make([]byte, frame.Length)
buffer.Read(frame.CryptoData)
return frame
}
func NewCryptoFrame(cryptoStream *Stream, data []byte) *CryptoFrame {
frame := &CryptoFrame{Offset: cryptoStream.WriteOffset, CryptoData: data, Length: uint64(len(data))}
cryptoStream.WriteOffset += frame.Length
cryptoStream.WriteData = append(cryptoStream.WriteData, data...)
return frame
}
type NewTokenFrame struct {
Token []byte
}
func (frame NewTokenFrame) FrameType() FrameType { return NewTokenType }
func (frame NewTokenFrame) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, uint64(len(frame.Token)))
buffer.Write(frame.Token)
}
func (frame NewTokenFrame) shouldBeRetransmitted() bool { return true }
func (frame NewTokenFrame) FrameLength() uint16 { return 1 + uint16(VarIntLen(uint64(len(frame.Token)))) + uint16(len(frame.Token))}
func ReadNewTokenFrame(buffer *bytes.Reader, conn *Connection) *NewTokenFrame {
frame := new(NewTokenFrame)
ReadVarInt(buffer) // Discard frame type
tokenLength, _ := ReadVarInt(buffer)
frame.Token = make([]byte, tokenLength)
buffer.Read(frame.Token)
return frame
}
type AckECNFrame struct {
AckFrame
ECT0Count uint64
ECT1Count uint64
ECTCECount uint64
}
func (frame AckECNFrame) FrameType() FrameType { return AckECNType }
func (frame AckECNFrame) writeTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.LargestAcknowledged)
WriteVarInt(buffer, frame.ECT0Count)
WriteVarInt(buffer, frame.ECT1Count)
WriteVarInt(buffer, frame.ECTCECount)
WriteVarInt(buffer, frame.AckDelay)
WriteVarInt(buffer, frame.AckBlockCount)
for i, ack := range frame.AckBlocks {
if i > 0 {
WriteVarInt(buffer, ack.Gap)
}
WriteVarInt(buffer, ack.Block)
}
}
func (frame AckECNFrame) shouldBeRetransmitted() bool { return true }
func (frame AckECNFrame) FrameLength() uint16 { return frame.AckFrame.FrameLength() + uint16(VarIntLen(frame.ECT0Count) + VarIntLen(frame.ECT1Count) + VarIntLen(frame.ECTCECount))}
func ReadAckECNFrame(buffer *bytes.Reader, conn *Connection) *AckECNFrame {
frame := new(AckECNFrame)
ReadVarInt(buffer) // Discards frame type
frame.LargestAcknowledged, _ = ReadVarInt(buffer)
frame.ECT0Count, _ = ReadVarInt(buffer)
frame.ECT1Count, _ = ReadVarInt(buffer)
frame.ECTCECount, _ = ReadVarInt(buffer)
frame.AckDelay, _ = ReadVarInt(buffer)
frame.AckBlockCount, _ = ReadVarInt(buffer)
firstBlock := AckBlock{}
firstBlock.Block, _ = ReadVarInt(buffer)
frame.AckBlocks = append(frame.AckBlocks, firstBlock)
var i uint64
for i = 0; i < frame.AckBlockCount; i++ {
ack := AckBlock{}
ack.Gap, _ = ReadVarInt(buffer)
ack.Block, _ = ReadVarInt(buffer)
frame.AckBlocks = append(frame.AckBlocks, ack)
}
return frame
}