forked from QUIC-Tracker/quic-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframes.go
More file actions
968 lines (901 loc) · 32.1 KB
/
frames.go
File metadata and controls
968 lines (901 loc) · 32.1 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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
package quictracker
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
. "github.com/PROGNOSISTool/adapter-quic/lib"
"github.com/google/go-cmp/cmp"
)
type Frame interface {
FrameType() FrameType
WriteTo(buffer *bytes.Buffer)
shouldBeRetransmitted() bool
FrameLength() uint16
MarshalJSON() ([]byte, error)
}
func NewFrameFromType(frameType FrameType, buffer *bytes.Reader, conn *Connection) (Frame, error) {
switch {
case frameType == PaddingFrameType:
return Frame(NewPaddingFrame(buffer)), nil
case frameType == PingType:
return Frame(NewPingFrame(buffer)), nil
case frameType == AckType:
return Frame(ReadAckFrame(buffer)), nil
case frameType == AckECNType:
return Frame(ReadAckECNFrame(buffer, conn)), nil
case frameType == ResetStreamType:
return Frame(NewResetStream(buffer)), nil
case frameType == StopSendingType:
return Frame(NewStopSendingFrame(buffer)), nil
case frameType == CryptoType:
return Frame(ReadCryptoFrame(buffer, conn)), nil
case frameType == NewTokenType:
return Frame(ReadNewTokenFrame(buffer, conn)), nil
case (frameType&StreamType) == StreamType && frameType <= 0x0f:
return Frame(ReadStreamFrame(buffer, conn)), nil
case frameType == MaxDataType:
return Frame(NewMaxDataFrame(buffer)), nil
case frameType == MaxStreamDataType:
return Frame(NewMaxStreamDataFrame(buffer)), nil
case frameType&0xFE == MaxStreamsType:
return Frame(NewMaxStreamIdFrame(buffer)), nil
case frameType == DataBlockedType:
return Frame(NewBlockedFrame(buffer)), nil
case frameType == StreamDataBlockedType:
return Frame(NewStreamBlockedFrame(buffer)), nil
case frameType&0xFE == StreamsBlockedType:
return Frame(NewStreamIdNeededFrame(buffer)), nil
case frameType == NewConnectionIdType:
return Frame(NewNewConnectionIdFrame(buffer)), nil
case frameType == RetireConnectionIdType:
return Frame(ReadRetireConnectionId(buffer)), nil
case frameType == PathChallengeType:
return Frame(ReadPathChallenge(buffer)), nil
case frameType == PathResponseType:
return Frame(ReadPathResponse(buffer)), nil
case frameType == ConnectionCloseType:
return Frame(NewConnectionCloseFrame(buffer)), nil
case frameType == ApplicationCloseType:
return Frame(NewApplicationCloseFrame(buffer)), nil
case frameType == HandshakeDoneType:
return Frame(NewHandshakeDoneFrame(buffer)), nil
default:
return nil, errors.New(fmt.Sprintf("Unknown frame type %d", frameType))
}
}
func NewFrame(buffer *bytes.Reader, conn *Connection) (Frame, error) {
typeValue, length, err := ReadVarIntValue(buffer)
if err == io.EOF {
return nil, nil
} else if err != nil {
return nil, err
}
for i := 0; i < length; i++ {
err := buffer.UnreadByte()
if err != nil {
return nil, err
}
}
frameType := FrameType(typeValue)
return NewFrameFromType(frameType, buffer, conn)
}
type FrameType uint64
const (
PaddingFrameType FrameType = 0x00
PingType = 0x01
AckType = 0x02
AckECNType = 0x03
ResetStreamType = 0x04
StopSendingType = 0x05
CryptoType = 0x06
NewTokenType = 0x07
StreamType = 0x08
MaxDataType = 0x10
MaxStreamDataType = 0x11
MaxStreamsType = 0x12
DataBlockedType = 0x14
StreamDataBlockedType = 0x15
StreamsBlockedType = 0x16
NewConnectionIdType = 0x18
RetireConnectionIdType = 0x19
PathChallengeType = 0x1a
PathResponseType = 0x1b
ConnectionCloseType = 0x1c
ApplicationCloseType = 0x1d
HandshakeDoneType = 0x1e
)
var frameTypeToString = map[FrameType]string {
PaddingFrameType: "PADDING",
PingType: "PING",
AckType: "ACK",
AckECNType: "ACK_ECN",
ResetStreamType: "RESET_STREAM",
StopSendingType: "STOP_SENDING",
CryptoType: "CRYPTO",
NewTokenType: "NEW_TOKEN",
StreamType: "STREAM",
MaxDataType: "MAX_DATA",
MaxStreamDataType: "MAX_STREAM_DATA",
MaxStreamsType: "MAX_STREAMS",
DataBlockedType: "DATA_BLOCKED",
StreamDataBlockedType: "STREAM_DATA_BLOCKED",
StreamsBlockedType: "STREAMS_BLOCKED",
NewConnectionIdType: "NEW_CONNECTION_ID",
RetireConnectionIdType: "RETIRE_CONNECTION_ID",
PathChallengeType: "PATH_CHALLENGE",
PathResponseType: "PATH_RESPONSE",
ConnectionCloseType: "CONNECTION_CLOSE",
ApplicationCloseType: "APPLICATION_CLOSE",
HandshakeDoneType: "HANDSHAKE_DONE",
}
func (t FrameType) String() string {
return frameTypeToString[t]
}
var stringToFrameType = map[string]FrameType {
"PADDING": PaddingFrameType,
"PING": PingType,
"ACK": AckType,
"ACK_ECN": AckECNType,
"RESET_STREAM": ResetStreamType,
"STOP_SENDING": StopSendingType,
"CRYPTO": CryptoType,
"NEW_TOKEN": NewTokenType,
"STREAM": StreamType,
"MAX_DATA": MaxDataType,
"MAX_STREAM_DATA": MaxStreamDataType,
"MAX_STREAMS": MaxStreamsType,
"DATA_BLOCKED": DataBlockedType,
"STREAM_DATA_BLOCKED": StreamDataBlockedType,
"STREAMS_BLOCKED": StreamsBlockedType,
"NEW_CONNECTION_ID": NewConnectionIdType,
"RETIRE_CONNECTION_ID": RetireConnectionIdType,
"PATH_CHALLENGE": PathChallengeType,
"PATH_RESPONSE": PathResponseType,
"CONNECTION_CLOSE": ConnectionCloseType,
"APPLICATION_CLOSE": ApplicationCloseType,
"HANDSHAKE_DONE": HandshakeDoneType,
}
func FrameTypeFromString(input string) FrameType {
return stringToFrameType[input]
}
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 (frame PaddingFrame) MarshalJSON() ([]byte, error) {
type localFrame PaddingFrame
envelope := Envelope{
Type: PaddingFrameJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
func NewPaddingFrame(buffer *bytes.Reader) *PaddingFrame {
_, _ = ReadVarInt(buffer) // Discard frame type
return new(PaddingFrame)
}
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 false }
func (frame *PingFrame) FrameLength() uint16 { return 1 }
func (frame PingFrame) MarshalJSON() ([]byte, error) {
type localFrame PingFrame
envelope := Envelope{
Type: PingFrameJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
func NewPingFrame(buffer *bytes.Reader) *PingFrame {
frame := new(PingFrame)
_, _ = ReadVarInt(buffer) // Discard frame type
return frame
}
type AckFrame struct {
LargestAcknowledged PacketNumber
AckDelay uint64
AckRangeCount uint64
AckRanges []AckRange
}
type AckRange struct {
Gap uint64
AckRange 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, uint64(frame.LargestAcknowledged))
WriteVarInt(buffer, frame.AckDelay)
WriteVarInt(buffer, frame.AckRangeCount)
for i, ack := range frame.AckRanges {
if i > 0 {
WriteVarInt(buffer, ack.Gap)
}
WriteVarInt(buffer, ack.AckRange)
}
}
func (frame *AckFrame) FrameLength() uint16 {
var length uint16
length += 1 + uint16(VarIntLen(uint64(frame.LargestAcknowledged))+VarIntLen(frame.AckDelay)+VarIntLen(frame.AckRangeCount))
for i, ack := range frame.AckRanges {
if i > 0 {
length += uint16(VarIntLen(ack.Gap))
}
length += uint16(VarIntLen(ack.AckRange))
}
return length
}
func (frame *AckFrame) GetAckedPackets() []PacketNumber { // TODO: This is prone to livelock
var packets []PacketNumber
currentPacketNumber := frame.LargestAcknowledged
packets = append(packets, currentPacketNumber)
for i := uint64(0); i < frame.AckRanges[0].AckRange; i++ {
currentPacketNumber--
packets = append(packets, currentPacketNumber)
}
for _, ackBlock := range frame.AckRanges[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.AckRange; i++ {
currentPacketNumber--
packets = append(packets, currentPacketNumber)
}
}
return packets
}
func (frame AckFrame) MarshalJSON() ([]byte, error) {
type localFrame AckFrame
envelope := Envelope{
Type: AckFrameJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
func (frame AckFrame) Equal(otherFrame AckFrame) bool {
if cmp.Equal(frame.AckRangeCount, otherFrame.AckRangeCount) &&
cmp.Equal(frame.AckRanges, otherFrame.AckRanges) &&
cmp.Equal(frame.LargestAcknowledged, otherFrame.LargestAcknowledged){
return true
}
return false
}
func ReadAckFrame(buffer *bytes.Reader) *AckFrame {
frame := new(AckFrame)
buffer.ReadByte() // Discard frame byte
frame.LargestAcknowledged = ReadPacketNumber(buffer)
frame.AckDelay, _, _ = ReadVarIntValue(buffer)
frame.AckRangeCount, _, _ = ReadVarIntValue(buffer)
firstBlock := AckRange{}
firstBlock.AckRange, _, _ = ReadVarIntValue(buffer)
frame.AckRanges = append(frame.AckRanges, firstBlock)
var i uint64
for i = 0; i < frame.AckRangeCount; i++ {
ack := AckRange{}
ack.Gap, _, _ = ReadVarIntValue(buffer)
ack.AckRange, _, _ = ReadVarIntValue(buffer)
frame.AckRanges = append(frame.AckRanges, ack)
}
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) {
frame.AckFrame.WriteTo(buffer)
WriteVarInt(buffer, frame.ECT0Count)
WriteVarInt(buffer, frame.ECT1Count)
WriteVarInt(buffer, frame.ECTCECount)
}
func (frame *AckECNFrame) shouldBeRetransmitted() bool { return false }
func (frame *AckECNFrame) FrameLength() uint16 { return frame.AckFrame.FrameLength() + uint16(VarIntLen(frame.ECT0Count)+VarIntLen(frame.ECT1Count)+VarIntLen(frame.ECTCECount)) }
func (frame AckECNFrame) MarshalJSON() ([]byte, error) {
type localFrame AckECNFrame
envelope := Envelope{
Type: AckECNFrameJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
func ReadAckECNFrame(buffer *bytes.Reader, conn *Connection) *AckECNFrame {
frame := &AckECNFrame{*ReadAckFrame(buffer), 0, 0, 0}
frame.ECT0Count, _, _ = ReadVarIntValue(buffer)
frame.ECT1Count, _, _ = ReadVarIntValue(buffer)
frame.ECTCECount, _, _ = ReadVarIntValue(buffer)
return frame
}
type ResetStream struct {
StreamId uint64
ApplicationErrorCode uint64
FinalSize uint64
}
func (frame *ResetStream) FrameType() FrameType { return ResetStreamType }
func (frame *ResetStream) WriteTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.StreamId)
WriteVarInt(buffer, frame.ApplicationErrorCode)
WriteVarInt(buffer, frame.FinalSize)
}
func (frame *ResetStream) shouldBeRetransmitted() bool { return true }
func (frame *ResetStream) FrameLength() uint16 { return 1 + uint16(VarIntLen(frame.StreamId)+VarIntLen(frame.ApplicationErrorCode)+VarIntLen(frame.FinalSize)) }
func (frame ResetStream) MarshalJSON() ([]byte, error) {
type localFrame ResetStream
envelope := Envelope{
Type: ResetStreamJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
func NewResetStream(buffer *bytes.Reader) *ResetStream {
frame := new(ResetStream)
_, _ = ReadVarInt(buffer) // Discard frame type
frame.StreamId, _, _ = ReadVarIntValue(buffer)
frame.ApplicationErrorCode, _, _ = ReadVarIntValue(buffer)
frame.FinalSize, _, _ = ReadVarIntValue(buffer)
return frame
}
type StopSendingFrame struct {
StreamId uint64
ApplicationErrorCode uint64
}
func (frame *StopSendingFrame) FrameType() FrameType { return StopSendingType }
func (frame *StopSendingFrame) WriteTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.StreamId)
WriteVarInt(buffer, frame.ApplicationErrorCode)
}
func (frame *StopSendingFrame) shouldBeRetransmitted() bool { return true }
func (frame *StopSendingFrame) FrameLength() uint16 { return 1 + uint16(VarIntLen(frame.StreamId) + VarIntLen(frame.ApplicationErrorCode)) }
func (frame StopSendingFrame) MarshalJSON() ([]byte, error) {
type localFrame StopSendingFrame
envelope := Envelope{
Type: StopSendingFrameJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
func NewStopSendingFrame(buffer *bytes.Reader) *StopSendingFrame {
frame := new(StopSendingFrame)
_, _ = ReadVarInt(buffer) // Discard frame type
frame.StreamId, _, _ = ReadVarIntValue(buffer)
frame.ApplicationErrorCode, _, _ = ReadVarIntValue(buffer)
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)
ReadVarIntValue(buffer) // Discards frame type
frame.Offset, _, _ = ReadVarIntValue(buffer)
frame.Length, _, _ = ReadVarIntValue(buffer)
frame.CryptoData = make([]byte, frame.Length)
buffer.Read(frame.CryptoData)
return frame
}
func (frame CryptoFrame) MarshalJSON() ([]byte, error) {
type localFrame CryptoFrame
envelope := Envelope{
Type: CryptoFrameJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
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 (frame NewTokenFrame) MarshalJSON() ([]byte, error) {
type localFrame NewTokenFrame
envelope := Envelope{
Type: NewTokenFrameJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
func ReadNewTokenFrame(buffer *bytes.Reader, conn *Connection) *NewTokenFrame {
frame := new(NewTokenFrame)
ReadVarIntValue(buffer) // Discard frame type
tokenLength, _, _ := ReadVarIntValue(buffer)
frame.Token = make([]byte, tokenLength)
buffer.Read(frame.Token)
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 (frame StreamFrame) MarshalJSON() ([]byte, error) {
type localFrame StreamFrame
envelope := Envelope{
Type: StreamFrameJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
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, _, _ = ReadVarIntValue(buffer)
if frame.OffBit {
frame.Offset, _, _ = ReadVarIntValue(buffer)
}
if frame.LenBit {
frame.Length, _, _ = ReadVarIntValue(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, offset uint64, data []byte, finBit bool) *StreamFrame {
frame := new(StreamFrame)
frame.StreamId = streamId
frame.FinBit = finBit
frame.LenBit = true
frame.Offset = offset
frame.OffBit = frame.Offset > 0
frame.Length = uint64(len(data))
frame.StreamData = data
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)
_, _ = ReadVarInt(buffer) // Discard frame type
frame.MaximumData, _, _ = ReadVarIntValue(buffer)
return frame
}
func (frame MaxDataFrame) MarshalJSON() ([]byte, error) {
type localFrame MaxDataFrame
envelope := Envelope{
Type: MaxDataFrameJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
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 (frame MaxStreamDataFrame) MarshalJSON() ([]byte, error) {
type localFrame MaxStreamDataFrame
envelope := Envelope{
Type: MaxStreamDataFrameJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
func NewMaxStreamDataFrame(buffer *bytes.Reader) *MaxStreamDataFrame {
frame := new(MaxStreamDataFrame)
_, _ = ReadVarInt(buffer) // Discard frame type
frame.StreamId, _, _ = ReadVarIntValue(buffer)
frame.MaximumStreamData, _, _ = ReadVarIntValue(buffer)
return frame
}
type MaxStreamsFrame struct {
StreamsType StreamsType
MaximumStreams uint64
}
func (frame *MaxStreamsFrame) FrameType() FrameType {
if frame.StreamsType == BidiStreams {
return MaxStreamsType
} else {
return MaxStreamsType + 1
}
}
func (frame *MaxStreamsFrame) WriteTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.MaximumStreams)
}
func (frame *MaxStreamsFrame) shouldBeRetransmitted() bool { return true }
func (frame *MaxStreamsFrame) IsUni() bool { return frame.StreamsType == UniStreams }
func (frame *MaxStreamsFrame) IsBidi() bool { return frame.StreamsType == BidiStreams }
func (frame *MaxStreamsFrame) FrameLength() uint16 { return 1 + uint16(VarIntLen(frame.MaximumStreams)) }
func (frame MaxStreamsFrame) MarshalJSON() ([]byte, error) {
type localFrame MaxStreamsFrame
envelope := Envelope{
Type: MaxStreamsFrameJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
func NewMaxStreamIdFrame(buffer *bytes.Reader) *MaxStreamsFrame {
frame := new(MaxStreamsFrame)
_, _ = ReadVarInt(buffer) // Discard frame type
frame.MaximumStreams, _, _ = ReadVarIntValue(buffer)
return frame
}
type DataBlockedFrame struct {
DataLimit uint64
}
func (frame *DataBlockedFrame) FrameType() FrameType { return DataBlockedType }
func (frame *DataBlockedFrame) WriteTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.DataLimit)
}
func (frame *DataBlockedFrame) shouldBeRetransmitted() bool { return false }
func (frame *DataBlockedFrame) FrameLength() uint16 { return 1 + uint16(VarIntLen(frame.DataLimit)) }
func (frame DataBlockedFrame) MarshalJSON() ([]byte, error) {
type localFrame DataBlockedFrame
envelope := Envelope{
Type: DataBlockedFrameJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
func NewBlockedFrame(buffer *bytes.Reader) *DataBlockedFrame {
frame := new(DataBlockedFrame)
_, _ = ReadVarInt(buffer) // Discard frame type
frame.DataLimit, _, _ = ReadVarIntValue(buffer)
return frame
}
type StreamDataBlockedFrame struct {
StreamId uint64
StreamDataLimit uint64
}
func (frame *StreamDataBlockedFrame) FrameType() FrameType { return StreamDataBlockedType }
func (frame *StreamDataBlockedFrame) WriteTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.StreamId)
WriteVarInt(buffer, frame.StreamDataLimit)
}
func (frame *StreamDataBlockedFrame) shouldBeRetransmitted() bool { return false }
func (frame *StreamDataBlockedFrame) FrameLength() uint16 { return 1 + uint16(VarIntLen(frame.StreamId)+VarIntLen(frame.StreamDataLimit)) }
func (frame StreamDataBlockedFrame) MarshalJSON() ([]byte, error) {
type localFrame StreamDataBlockedFrame
envelope := Envelope{
Type: StreamDataBlockedFrameJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
func NewStreamBlockedFrame(buffer *bytes.Reader) *StreamDataBlockedFrame {
frame := new(StreamDataBlockedFrame)
_, _ = ReadVarInt(buffer) // Discard frame type
frame.StreamId, _, _ = ReadVarIntValue(buffer)
frame.StreamDataLimit, _, _ = ReadVarIntValue(buffer)
return frame
}
type StreamsBlockedFrame struct {
StreamsType StreamsType
StreamLimit uint64
}
func (frame *StreamsBlockedFrame) FrameType() FrameType {
if frame.StreamsType == BidiStreams {
return StreamsBlockedType
} else {
return StreamsBlockedType + 1
}
}
func (frame *StreamsBlockedFrame) WriteTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.StreamLimit)
}
func (frame *StreamsBlockedFrame) shouldBeRetransmitted() bool { return false }
func (frame *StreamsBlockedFrame) IsUni() bool { return frame.StreamsType == UniStreams }
func (frame *StreamsBlockedFrame) IsBidi() bool { return frame.StreamsType == BidiStreams }
func (frame *StreamsBlockedFrame) FrameLength() uint16 { return 1 + uint16(VarIntLen(frame.StreamLimit)) }
func (frame StreamsBlockedFrame) MarshalJSON() ([]byte, error) {
type localFrame StreamsBlockedFrame
envelope := Envelope{
Type: StreamsBlockedFrameJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
func NewStreamIdNeededFrame(buffer *bytes.Reader) *StreamsBlockedFrame {
frame := new(StreamsBlockedFrame)
_, _ = ReadVarInt(buffer) // Discard frame type
frame.StreamLimit, _, _ = ReadVarIntValue(buffer)
return frame
}
type NewConnectionIdFrame struct {
Sequence uint64
RetirePriorTo 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)
WriteVarInt(buffer, frame.RetirePriorTo)
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) + VarIntLen(frame.RetirePriorTo)) + 1 + uint16(len(frame.ConnectionId)) + 16 }
func (frame NewConnectionIdFrame) MarshalJSON() ([]byte, error) {
type localFrame NewConnectionIdFrame
envelope := Envelope{
Type: NewConnectionIdFrameJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
func NewNewConnectionIdFrame(buffer *bytes.Reader) *NewConnectionIdFrame {
frame := new(NewConnectionIdFrame)
_, _ = ReadVarInt(buffer) // Discard frame type
frame.Sequence, _, _ = ReadVarIntValue(buffer)
frame.RetirePriorTo, _, _ = ReadVarIntValue(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 RetireConnectionId struct {
SequenceNumber uint64
}
func (frame *RetireConnectionId) FrameType() FrameType { return RetireConnectionIdType }
func (frame *RetireConnectionId) WriteTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, frame.SequenceNumber)
}
func (frame *RetireConnectionId) shouldBeRetransmitted() bool { return true }
func (frame *RetireConnectionId) FrameLength() uint16 { return 1 + uint16(VarIntLen(frame.SequenceNumber)) }
func (frame RetireConnectionId) MarshalJSON() ([]byte, error) {
type localFrame RetireConnectionId
envelope := Envelope{
Type: RetireConnectionIdJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
func ReadRetireConnectionId(buffer *bytes.Reader) *RetireConnectionId {
frame := new(RetireConnectionId)
buffer.ReadByte() // Discard frame byte
frame.SequenceNumber, _, _ = ReadVarIntValue(buffer)
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 (frame PathChallenge) MarshalJSON() ([]byte, error) {
type localFrame PathChallenge
envelope := Envelope{
Type: PathChallengeJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
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 (frame PathResponse) MarshalJSON() ([]byte, error) {
type localFrame PathResponse
envelope := Envelope{
Type: PathResponseJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
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 ConnectionCloseFrame struct {
ErrorCode uint64
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()))
WriteVarInt(buffer, 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 + uint16(VarIntLen(frame.ErrorCode)+VarIntLen(frame.ErrorFrameType)+VarIntLen(frame.ReasonPhraseLength)) + uint16(frame.ReasonPhraseLength) }
func (frame ConnectionCloseFrame) MarshalJSON() ([]byte, error) {
type localFrame ConnectionCloseFrame
envelope := Envelope{
Type: ConnectionCloseFrameJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
func NewConnectionCloseFrame(buffer *bytes.Reader) *ConnectionCloseFrame {
frame := new(ConnectionCloseFrame)
_, _ = ReadVarInt(buffer) // Discard frame type
frame.ErrorCode, _, _ = ReadVarIntValue(buffer)
frame.ErrorFrameType, _, _ = ReadVarIntValue(buffer)
frame.ReasonPhraseLength, _, _ = ReadVarIntValue(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 {
// TODO: Merge it with 0x1c
ErrorCode uint64
ReasonPhraseLength uint64
ReasonPhrase string
}
func (frame *ApplicationCloseFrame) FrameType() FrameType { return ApplicationCloseType }
func (frame *ApplicationCloseFrame) WriteTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
WriteVarInt(buffer, 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 (frame ApplicationCloseFrame) MarshalJSON() ([]byte, error) {
type localFrame ApplicationCloseFrame
envelope := Envelope{
Type: ApplicationCloseFrameJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
func NewApplicationCloseFrame(buffer *bytes.Reader) *ApplicationCloseFrame {
frame := new(ApplicationCloseFrame)
_, _ = ReadVarInt(buffer) // Discard frame type
frame.ErrorCode, _, _ = ReadVarIntValue(buffer)
frame.ReasonPhraseLength, _, _ = ReadVarIntValue(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 HandshakeDoneFrame byte
func (frame *HandshakeDoneFrame) FrameType() FrameType { return HandshakeDoneType }
func (frame *HandshakeDoneFrame) WriteTo(buffer *bytes.Buffer) {
WriteVarInt(buffer, uint64(frame.FrameType()))
}
func (frame *HandshakeDoneFrame) shouldBeRetransmitted() bool { return true }
func (frame *HandshakeDoneFrame) FrameLength() uint16 { return 1 }
func (frame HandshakeDoneFrame) MarshalJSON() ([]byte, error) {
type localFrame HandshakeDoneFrame
envelope := Envelope{
Type: HandshakeDoneFrameJSON,
Message: localFrame(frame),
}
return json.Marshal(envelope)
}
func NewHandshakeDoneFrame(buffer *bytes.Reader) *HandshakeDoneFrame {
frame := new(HandshakeDoneFrame)
_, _ = ReadVarInt(buffer) // Discard frame type
return frame
}