This repository has been 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
/
packets.go
342 lines (327 loc) · 9.62 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
/*
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"
"github.com/davecgh/go-spew/spew"
"unsafe"
"fmt"
)
type Acknowledger interface {
ShouldBeAcknowledged() bool // Indicates whether or not the packet type should be acknowledged by the mean of sending an ack
}
type PacketEncoder interface {
EncodeHeader() []byte
EncodePayload() []byte
Encode([]byte) []byte
}
type Packet interface {
Header() Header
Acknowledger
PacketEncoder
Pointer() unsafe.Pointer
PNSpace() PNSpace
EncryptionLevel() EncryptionLevel
ShortString() string
}
type abstractPacket struct {
header Header
Acknowledger
PacketEncoder
}
func (p abstractPacket) Header() Header {
return p.header
}
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 VersionNegotationPacket struct {
abstractPacket
UnusedField uint8
Version uint32
DestinationCID ConnectionID
SourceCID ConnectionID
SupportedVersions []SupportedVersion
}
type SupportedVersion uint32
func (p *VersionNegotationPacket) ShouldBeAcknowledged() bool { return false }
func (p *VersionNegotationPacket) 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 *VersionNegotationPacket) Pointer() unsafe.Pointer {
return unsafe.Pointer(p)
}
func (p *VersionNegotationPacket) PNSpace() PNSpace { return PNSpaceNoSpace }
func (p *VersionNegotationPacket) EncryptionLevel() EncryptionLevel { return EncryptionLevelNone }
func ReadVersionNegotationPacket(buffer *bytes.Reader) *VersionNegotationPacket {
p := new(VersionNegotationPacket)
b, err := buffer.ReadByte()
if err != nil {
panic(err)
}
p.UnusedField = b & 0x7f
binary.Read(buffer, binary.BigEndian, &p.Version)
CIDL, _ := buffer.ReadByte()
DCIL := 3 + ((CIDL & 0xf0) >> 4)
SCIL := 3 + (CIDL & 0xf)
p.DestinationCID = make([]byte, DCIL, DCIL)
binary.Read(buffer, binary.BigEndian, &p.DestinationCID)
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 NewVersionNegotationPacket(unusedField uint8, version uint32, versions []SupportedVersion, conn *Connection) *VersionNegotationPacket {
p := new(VersionNegotationPacket)
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
GetFirst(frameType FrameType) Frame
GetAll(frameType FrameType) []Frame
}
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) 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) ShouldBeAcknowledged() bool {
for _, frame := range p.Frames {
switch frame.FrameType() {
case AckType, 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
for _, frame := range p.Frames {
if frame.shouldBeRetransmitted() || 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)
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)
return p
}
type RetryPacket struct {
abstractPacket
OriginalDestinationCID ConnectionID
RetryToken []byte
}
func ReadRetryPacket(buffer *bytes.Reader) *RetryPacket {
p := new(RetryPacket)
p.header = ReadLongHeader(buffer)
OCIDL, _ := buffer.ReadByte()
p.OriginalDestinationCID = make([]byte, OCIDL)
p.RetryToken = make([]byte, buffer.Len())
buffer.Read(p.RetryToken)
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.WriteByte(byte(len(p.OriginalDestinationCID)))
buffer.Write(p.OriginalDestinationCID)
buffer.Write(p.RetryToken)
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)
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(KeyPhaseZero, 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
}