-
Notifications
You must be signed in to change notification settings - Fork 16
/
packets.go
385 lines (373 loc) · 11.1 KB
/
packets.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
package quictracker
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/davecgh/go-spew/spew"
"io"
"unsafe"
)
type Packet interface {
Header() Header
ShouldBeAcknowledged() bool // Indicates whether or not the packet type should be acknowledged by the mean of sending an ack
EncodeHeader() []byte
EncodePayload() []byte
Encode([]byte) []byte
Pointer() unsafe.Pointer
PNSpace() PNSpace
EncryptionLevel() EncryptionLevel
ShortString() string
ReceiveContext() PacketContext
SetReceiveContext(ctx PacketContext)
SendContext() PacketContext
SetSendContext(ctx PacketContext)
}
type abstractPacket struct {
header Header
receiveContext PacketContext
sendContext PacketContext
}
func (p abstractPacket) Header() Header {
return p.header
}
func (p abstractPacket) ReceiveContext() PacketContext {
return p.receiveContext
}
func (p *abstractPacket) SetReceiveContext(ctx PacketContext) {
p.receiveContext = ctx
}
func (p abstractPacket) SendContext() PacketContext {
return p.sendContext
}
func (p *abstractPacket) SetSendContext(ctx PacketContext) {
p.sendContext = ctx
}
func (p abstractPacket) EncodeHeader() []byte {
return p.header.Encode()
}
func (p abstractPacket) Encode(payload []byte) []byte {
buffer := new(bytes.Buffer)
buffer.Write(p.EncodeHeader())
buffer.Write(payload)
return buffer.Bytes()
}
func (p abstractPacket) ShortString() string {
return fmt.Sprintf("{type=%s, number=%d}", p.header.PacketType().String(), p.header.PacketNumber())
}
type VersionNegotiationPacket struct {
abstractPacket
UnusedField uint8
Version uint32
DestinationCID ConnectionID
SourceCID ConnectionID
SupportedVersions []SupportedVersion
}
type SupportedVersion uint32
func (v SupportedVersion) String() string {
return hex.EncodeToString(Uint32ToBEBytes(uint32(v)))
}
func (p *VersionNegotiationPacket) ShouldBeAcknowledged() bool { return false }
func (p *VersionNegotiationPacket) EncodePayload() []byte {
buffer := new(bytes.Buffer)
buffer.WriteByte(p.UnusedField & 0x80)
binary.Write(buffer, binary.BigEndian, p.Version)
buffer.WriteByte((p.DestinationCID.CIDL() << 4) | p.SourceCID.CIDL())
binary.Write(buffer, binary.BigEndian, p.DestinationCID)
binary.Write(buffer, binary.BigEndian, p.SourceCID)
for _, version := range p.SupportedVersions {
binary.Write(buffer, binary.BigEndian, version)
}
return buffer.Bytes()
}
func (p *VersionNegotiationPacket) Pointer() unsafe.Pointer {
return unsafe.Pointer(p)
}
func (p *VersionNegotiationPacket) PNSpace() PNSpace { return PNSpaceNoSpace }
func (p *VersionNegotiationPacket) EncryptionLevel() EncryptionLevel { return EncryptionLevelNone }
func ReadVersionNegotationPacket(buffer *bytes.Reader) *VersionNegotiationPacket {
p := new(VersionNegotiationPacket)
b, err := buffer.ReadByte()
if err != nil {
panic(err)
}
p.UnusedField = b & 0x7f
binary.Read(buffer, binary.BigEndian, &p.Version)
DCIL, _ := buffer.ReadByte()
p.DestinationCID = make([]byte, DCIL, DCIL)
binary.Read(buffer, binary.BigEndian, &p.DestinationCID)
SCIL, _ := buffer.ReadByte()
p.SourceCID = make([]byte, SCIL, SCIL)
binary.Read(buffer, binary.BigEndian, &p.SourceCID)
for {
var version uint32
err := binary.Read(buffer, binary.BigEndian, &version)
if err == io.EOF || err == io.ErrUnexpectedEOF {
break
} else if err != nil {
panic(err)
}
p.SupportedVersions = append(p.SupportedVersions, SupportedVersion(version))
}
return p
}
func NewVersionNegotiationPacket(unusedField uint8, version uint32, versions []SupportedVersion, conn *Connection) *VersionNegotiationPacket {
p := new(VersionNegotiationPacket)
p.UnusedField = unusedField
p.DestinationCID = conn.DestinationCID
p.SourceCID = conn.SourceCID
p.Version = version
p.SupportedVersions = versions
return p
}
type Framer interface {
Packet
GetFrames() []Frame
AddFrame(frame Frame)
GetRetransmittableFrames() []Frame
Contains(frameType FrameType) bool
OnlyContains(frameType FrameType) bool
GetFirst(frameType FrameType) Frame
GetAll(frameType FrameType) []Frame
PadTo(length int)
}
type FramePacket struct {
abstractPacket
Frames []Frame
}
func (p *FramePacket) GetFrames() []Frame {
return p.Frames
}
func (p *FramePacket) AddFrame(frame Frame) {
p.Frames = append(p.Frames, frame)
}
func (p *FramePacket) GetRetransmittableFrames() []Frame {
var frames []Frame
for _, frame := range p.Frames {
if frame.shouldBeRetransmitted() {
frames = append(frames, frame)
}
}
return frames
}
func (p *FramePacket) Pointer() unsafe.Pointer {
return unsafe.Pointer(p)
}
func (p *FramePacket) Contains(frameType FrameType) bool {
for _, f := range p.Frames {
if f.FrameType() == frameType {
return true
}
}
return false
}
func (p *FramePacket) OnlyContains(frameType FrameType) bool {
for _, f := range p.Frames {
if f.FrameType() != frameType {
return false
}
}
return true
}
func (p *FramePacket) GetFirst(frameType FrameType) Frame {
for _, f := range p.Frames {
if f.FrameType() == frameType {
return f
}
}
return nil
}
func (p *FramePacket) GetAll(frameType FrameType) []Frame {
var frames []Frame
for _, f := range p.Frames {
if f.FrameType() == frameType {
frames = append(frames, f)
}
}
return frames
}
func (p *FramePacket) PadTo(length int) {
switch h := p.Header().(type) {
case *LongHeader:
h.Length = NewVarInt(uint64(len(p.EncodePayload())))
}
currentLen := len(p.Encode(p.EncodePayload()))
for currentLen < length {
p.AddFrame(new(PaddingFrame))
switch h := p.Header().(type) {
case *LongHeader:
h.Length = NewVarInt(h.Length.Value + 1)
}
currentLen = len(p.Encode(p.EncodePayload()))
}
}
func (p *FramePacket) ShouldBeAcknowledged() bool {
for _, frame := range p.Frames {
switch frame.FrameType() {
case AckType, AckECNType, PaddingFrameType, ConnectionCloseType, ApplicationCloseType:
default:
return true
}
}
return false
}
func (p *FramePacket) EncodePayload() []byte {
buffer := new(bytes.Buffer)
for _, frame := range p.Frames {
frame.WriteTo(buffer)
}
return buffer.Bytes()
}
type InitialPacket struct {
FramePacket
}
func (p *InitialPacket) GetRetransmittableFrames() []Frame {
var frames []Frame
hasCrypto := p.Contains(CryptoType)
for _, frame := range p.Frames {
if frame.shouldBeRetransmitted() || (hasCrypto && frame.FrameType() == PaddingFrameType) {
frames = append(frames, frame)
}
}
return frames
}
func (p *InitialPacket) PNSpace() PNSpace { return PNSpaceInitial }
func (p *InitialPacket) EncryptionLevel() EncryptionLevel { return EncryptionLevelInitial }
func ReadInitialPacket(buffer *bytes.Reader, conn *Connection) *InitialPacket {
p := new(InitialPacket)
p.header = ReadLongHeader(buffer, conn)
for {
frame, err := NewFrame(buffer, conn)
if err != nil {
spew.Dump(p)
panic(err)
}
if frame == nil {
break
}
if cf, ok := frame.(*CryptoFrame); ok {
conn.CryptoStreams.Get(p.PNSpace()).addToRead(&StreamFrame{Offset: cf.Offset, Length: cf.Length, StreamData: cf.CryptoData})
}
p.Frames = append(p.Frames, frame)
}
return p
}
func NewInitialPacket(conn *Connection) *InitialPacket {
p := new(InitialPacket)
p.header = NewLongHeader(Initial, conn, PNSpaceInitial)
if len(conn.Token) > 0 {
p.header.(*LongHeader).Token = conn.Token
p.header.(*LongHeader).TokenLength = NewVarInt(uint64(len(conn.Token)))
}
return p
}
type RetryPacket struct {
abstractPacket
RetryToken []byte
RetryIntegrityTag [16]byte
}
func ReadRetryPacket(buffer *bytes.Reader, conn *Connection) *RetryPacket {
p := new(RetryPacket)
h := ReadLongHeader(buffer, conn) // TODO: This should not be a full-length long header. Retry header ?
p.header = h
p.RetryToken = make([]byte, buffer.Len() - len(p.RetryIntegrityTag))
buffer.Read(p.RetryToken)
buffer.Read(p.RetryIntegrityTag[:])
return p
}
func (p *RetryPacket) GetRetransmittableFrames() []Frame { return nil }
func (p *RetryPacket) Pointer() unsafe.Pointer { return unsafe.Pointer(p) }
func (p *RetryPacket) PNSpace() PNSpace { return PNSpaceNoSpace }
func (p *RetryPacket) EncryptionLevel() EncryptionLevel { return EncryptionLevelNone }
func (p *RetryPacket) ShouldBeAcknowledged() bool { return false }
func (p *RetryPacket) EncodePayload() []byte {
buffer := new(bytes.Buffer)
buffer.Write(p.RetryToken)
buffer.Write(p.RetryIntegrityTag[:])
return buffer.Bytes()
}
type HandshakePacket struct {
FramePacket
}
func (p *HandshakePacket) PNSpace() PNSpace { return PNSpaceHandshake }
func (p *HandshakePacket) EncryptionLevel() EncryptionLevel { return EncryptionLevelHandshake }
func ReadHandshakePacket(buffer *bytes.Reader, conn *Connection) *HandshakePacket {
p := new(HandshakePacket)
p.header = ReadLongHeader(buffer, conn)
for {
frame, err := NewFrame(buffer, conn)
if err != nil {
spew.Dump(p)
panic(err)
}
if frame == nil {
break
}
if cf, ok := frame.(*CryptoFrame); ok {
conn.CryptoStreams.Get(p.PNSpace()).addToRead(&StreamFrame{Offset: cf.Offset, Length: cf.Length, StreamData: cf.CryptoData})
}
p.Frames = append(p.Frames, frame)
}
return p
}
func NewHandshakePacket(conn *Connection) *HandshakePacket {
p := new(HandshakePacket)
p.header = NewLongHeader(Handshake, conn, PNSpaceHandshake)
return p
}
type ProtectedPacket struct {
FramePacket
}
func (p *ProtectedPacket) PNSpace() PNSpace { return PNSpaceAppData }
func (p *ProtectedPacket) EncryptionLevel() EncryptionLevel { return EncryptionLevel1RTT }
func ReadProtectedPacket(buffer *bytes.Reader, conn *Connection) *ProtectedPacket {
p := new(ProtectedPacket)
p.header = ReadHeader(buffer, conn)
for {
frame, err := NewFrame(buffer, conn)
if err != nil {
spew.Dump(p)
panic(err)
}
if frame == nil {
break
}
if cf, ok := frame.(*CryptoFrame); ok {
conn.CryptoStreams.Get(p.PNSpace()).addToRead(&StreamFrame{Offset: cf.Offset, Length: cf.Length, StreamData: cf.CryptoData})
}
p.Frames = append(p.Frames, frame)
}
return p
}
func NewProtectedPacket(conn *Connection) *ProtectedPacket {
p := new(ProtectedPacket)
p.header = NewShortHeader(conn)
return p
}
type ZeroRTTProtectedPacket struct {
FramePacket
}
func (p *ZeroRTTProtectedPacket) PNSpace() PNSpace { return PNSpaceAppData }
func (p *ZeroRTTProtectedPacket) EncryptionLevel() EncryptionLevel { return EncryptionLevel0RTT }
func NewZeroRTTProtectedPacket(conn *Connection) *ZeroRTTProtectedPacket {
p := new(ZeroRTTProtectedPacket)
p.header = NewLongHeader(ZeroRTTProtected, conn, PNSpaceAppData)
return p
}
type StatelessResetPacket struct {
abstractPacket
UnpredictableBits []byte
StatelessResetToken [16]byte
}
func (*StatelessResetPacket) ShouldBeAcknowledged() bool { return false }
func (*StatelessResetPacket) PNSpace() PNSpace { return PNSpaceNoSpace }
func (p *StatelessResetPacket) Pointer() unsafe.Pointer { return unsafe.Pointer(p) }
func (*StatelessResetPacket) EncryptionLevel() EncryptionLevel { return EncryptionLevelNone }
func ( StatelessResetPacket) EncodePayload() []byte { return nil }
func ReadStatelessResetPacket(buffer *bytes.Reader) *StatelessResetPacket {
p := new(StatelessResetPacket)
p.UnpredictableBits = make([]byte, buffer.Len() - len(p.StatelessResetToken))
buffer.Read(p.UnpredictableBits)
buffer.Read(p.StatelessResetToken[:])
return p
}