-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket_header.go
338 lines (301 loc) · 9.09 KB
/
packet_header.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
package quic
import (
"encoding/binary"
"fmt"
"math/rand"
"github.com/ami-GS/gQUIC/qtype"
)
type LongHeaderPacketType byte
const (
ZeroRTTProtectedPacketType LongHeaderPacketType = iota + 0x7c
HandshakePacketType
RetryPacketType
InitialPacketType
)
func (lht LongHeaderPacketType) String() string {
return map[LongHeaderPacketType]string{
ZeroRTTProtectedPacketType: "0 RTT Protected Packet",
HandshakePacketType: "Handshake Packet",
RetryPacketType: "Retry Packet",
InitialPacketType: "Initial Pakcet",
}[lht]
}
type ShortHeaderFlagType byte
const (
KeyPhaseFlag ShortHeaderFlagType = 0x40
GQUICDemultipexingFlag = 0x08
//Reserved = 0x04
)
type ShortHeaderPacketType byte
const (
ShortHeaderReservedBits ShortHeaderPacketType = 0x30
KeyPhase ShortHeaderPacketType = 0x40
)
func (sht ShortHeaderPacketType) String() string {
keyPhase := sht&KeyPhase == KeyPhase
return fmt.Sprintf("KeyPhrase:%v", keyPhase)
}
type PacketHeaderType byte
const (
LongHeaderType PacketHeaderType = 0x80
ShortHeaderType PacketHeaderType = 0x00
)
type PacketHeader interface {
// TODO: can be defined as different interface, like WireObject?
GetWire() ([]byte, error)
SetWire(wire []byte)
GetWireSize() int
genWire() ([]byte, error)
String() string
GetConnectionIDPair() (qtype.ConnectionID, qtype.ConnectionID) // srcID, destID
getPacketNumber() qtype.PacketNumber
}
type BasePacketHeader struct {
DestConnID qtype.ConnectionID
SrcConnID qtype.ConnectionID
PacketNumber qtype.PacketNumber
wire []byte
}
func (ph *BasePacketHeader) GetConnectionIDPair() (qtype.ConnectionID, qtype.ConnectionID) {
return ph.SrcConnID, ph.DestConnID
}
func (ph *BasePacketHeader) getPacketNumber() qtype.PacketNumber {
return ph.PacketNumber
}
func (ph *BasePacketHeader) SetWire(wire []byte) {
ph.wire = wire
}
func (ph *BasePacketHeader) GetWireSize() int {
return len(ph.wire)
}
func (ph *BasePacketHeader) String() string {
return fmt.Sprintf("DstConnID:%v\nSrcConnID:%v\nPacketNum:%d", ph.DestConnID.Bytes(), ph.SrcConnID.Bytes(), ph.PacketNumber)
}
// before passing data, need to check whether data[1:5] == 0x00 or not
// data[1:5] == 0x00 means version negotiation packet
type PacketHeaderPerser func(data []byte) (p PacketHeader, length int, err error)
var PacketHeaderParserMap = map[PacketHeaderType]PacketHeaderPerser{
LongHeaderType: ParseLongHeader,
ShortHeaderType: ParseShortHeader,
}
// Long Header
/*
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+
|1| Type (7) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Version (32) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|DCIL(4)|SCIL(4)|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Connection ID (0/32..144) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Connection ID (0/32..144) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Length (i) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Packet Number (8/16/32) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Payload (*) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
type LongHeader struct {
*BasePacketHeader
PacketType LongHeaderPacketType
Version qtype.Version
DCIL byte
SCIL byte
Length qtype.QuicInt
}
func NewLongHeader(packetType LongHeaderPacketType, version qtype.Version, destConnID, srcConnID qtype.ConnectionID, packetNumber qtype.PacketNumber, length qtype.QuicInt) *LongHeader {
dcil := 0
if destConnID != nil {
dcil = len(destConnID) - 3
}
scil := 0
if srcConnID != nil {
scil = len(srcConnID) - 3
}
lh := &LongHeader{
BasePacketHeader: &BasePacketHeader{
DestConnID: destConnID,
SrcConnID: srcConnID,
PacketNumber: packetNumber,
},
PacketType: packetType,
Version: version,
DCIL: byte(dcil),
SCIL: byte(scil),
Length: length,
}
var err error
lh.wire, err = lh.genWire()
if err != nil {
// error
return nil
}
return lh
}
func ParseLongHeader(data []byte) (PacketHeader, int, error) {
var err error
idx := 0
lh := NewLongHeader(0, 0, nil, nil, 0, 0)
lh.PacketType = LongHeaderPacketType(data[idx] & 0x7f)
idx++
lh.Version = qtype.Version(binary.BigEndian.Uint32(data[idx:]))
idx += 4
lh.DCIL = data[idx] >> 4
lh.SCIL = data[idx] & 0x0f
idx++
if lh.DCIL != 0 {
dcil := int(lh.DCIL + 3)
lh.DestConnID, err = qtype.ReadConnectionID(data[idx:], dcil)
if err != nil {
return nil, 0, err
}
idx += dcil
}
if lh.SCIL != 0 {
scil := int(lh.SCIL + 3)
lh.SrcConnID, err = qtype.ReadConnectionID(data[idx:], scil)
if err != nil {
return nil, 0, err
}
idx += scil
}
if lh.PacketType == RetryPacketType {
lh.wire = data[:idx]
return lh, idx, nil
} else if lh.PacketType == InitialPacketType {
lh.wire = data[:idx]
return lh, idx, nil
}
lh.Length = qtype.DecodeQuicInt(data[idx:])
idx += lh.Length.GetByteLen()
lh.PacketNumber = qtype.DecodePacketNumber(data[idx:])
idx += lh.PacketNumber.GetByteLen()
lh.wire = data[:idx]
return lh, idx, nil
}
func (lh LongHeader) String() string {
return fmt.Sprintf("LongHeader:%s\tVer:%s\nDCIL:%d,SCIL:%d\n%s\nLength:%d", lh.PacketType, lh.Version, lh.DCIL, lh.SCIL, lh.BasePacketHeader, lh.Length)
}
func (lh LongHeader) genWire() (wire []byte, err error) {
wireLen := 6
if lh.PacketType != InitialPacketType && lh.PacketType != RetryPacketType {
wireLen += int(lh.Length.GetByteLen() + lh.PacketNumber.GetByteLen())
}
if lh.DCIL != 0 {
wireLen += int(lh.DCIL + 3)
}
if lh.SCIL != 0 {
wireLen += int(lh.SCIL + 3)
}
wire = make([]byte, wireLen)
wire[0] = 0x80 | byte(lh.PacketType)
binary.BigEndian.PutUint32(wire[1:], uint32(lh.Version))
wire[5] = (lh.DCIL << 4) | lh.SCIL
idx := 6
if lh.DCIL != 0 {
copy(wire[idx:], lh.DestConnID.Bytes())
idx += int(lh.DCIL + 3)
}
if lh.SCIL != 0 {
copy(wire[idx:], lh.SrcConnID.Bytes())
idx += int(lh.SCIL + 3)
}
if lh.PacketType == InitialPacketType || lh.PacketType == RetryPacketType {
// These packets are exceptional
return
}
lh.Length.PutWire(wire[idx:])
idx += lh.Length.GetByteLen()
lh.PacketNumber.PutWire(wire[idx:])
return
}
func (lh LongHeader) GetWire() ([]byte, error) {
var err error
if lh.wire == nil {
lh.wire, err = lh.genWire()
}
return lh.wire, err
}
// Short Header
/*
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+
|0|K|1|1|0|R R R|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Connection ID (0..144) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Packet Number (8/16/32) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Protected Payload (*) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
type ShortHeader struct {
*BasePacketHeader
PacketType ShortHeaderPacketType
}
func NewShortHeader(key bool, destConnID qtype.ConnectionID, packetNumber qtype.PacketNumber) *ShortHeader {
packetType := ShortHeaderPacketType(ShortHeaderType) | 0x30
if key {
packetType |= KeyPhase
}
sh := &ShortHeader{
BasePacketHeader: &BasePacketHeader{
DestConnID: destConnID,
SrcConnID: nil,
PacketNumber: packetNumber,
},
PacketType: packetType,
}
var err error
sh.wire, err = sh.genWire()
if err != nil {
// error
}
return sh
}
func ParseShortHeader(data []byte) (PacketHeader, int, error) {
var err error
idx := 0
sh := NewShortHeader(false, nil, 0)
sh.PacketType = ShortHeaderPacketType(data[idx])
idx++
sh.DestConnID, err = qtype.ReadConnectionID(data[idx:], qtype.ConnectionIDLen)
if err != nil {
return nil, 0, err
}
idx += qtype.ConnectionIDLen
sh.PacketNumber = qtype.DecodePacketNumber(data[idx:])
idx += sh.PacketNumber.GetByteLen()
sh.wire = data[:idx]
return sh, idx, nil
}
func (sh ShortHeader) String() string {
return fmt.Sprintf("ShortHeader:%s\n%s", sh.PacketType, sh.BasePacketHeader)
}
func (sh ShortHeader) genWire() (wire []byte, err error) {
connIDLen := len(sh.DestConnID)
wire = make([]byte, 1+connIDLen+sh.PacketNumber.GetByteLen())
wire[0] = byte(sh.PacketType)
// set reserved bit randomely for security reason
wire[0] |= byte(rand.Intn(7))
idx := 1
if connIDLen != 0 {
copy(wire[idx:idx+connIDLen], sh.DestConnID)
idx += connIDLen
}
sh.PacketNumber.PutWire(wire[idx:])
return
}
func (sh ShortHeader) GetWire() ([]byte, error) {
var err error
if sh.wire == nil {
sh.wire, err = sh.genWire()
}
return sh.wire, err
}