This repository has been archived by the owner on Apr 21, 2021. It is now read-only.
forked from ekr/minq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.go
200 lines (168 loc) · 4.85 KB
/
packet.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
package minq
import (
"bytes"
)
// Encode a QUIC packet.
/*
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) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ Connection ID (64) +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Packet Number (32) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Version (32) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Payload (*) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
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 11
+-+-+-+-+-+-+-+-+
|0|C|K| Type (5)|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ [Connection ID (64)] +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Packet Number (8/16/32) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Protected Payload (*) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
const (
packetFlagLongHeader = 0x80
packetFlagC = 0x40
packetFlagK = 0x20
)
const (
packetTypeVersionNegotiation = 1
packetTypeClientInitial = 2
packetTypeServerStatelessRetry = 3
packetTypeServerCleartext = 4
packetTypeClientCleartext = 5
packetType0RTTProtected = 6
packetType1RTTProtectedPhase0 = 7
packetType1RTTProtectedPhase1 = 8
packetTypePublicReset = 9
)
type ConnectionId uint64
type version uint32
// The PDU definition for the header.
// These types are capitalized so that |codec| can use the,
type packetHeader struct {
Type byte
ConnectionID ConnectionId
PacketNumber uint64 // Never more than 32 bits on the wire.
Version VersionNumber
}
type packet struct {
packetHeader
payload []byte
}
// Functions to support encoding and decoding.
func isSet(b byte, flag byte) bool {
return (b & flag) != 0
}
func isLongHeader(p *packetHeader) bool {
return isSet(p.Type, packetFlagLongHeader)
}
func (p *packetHeader) isProtected() bool {
if !isLongHeader(p) {
return true
}
switch p.Type & 0x7f {
case packetTypeClientInitial, packetTypeClientCleartext, packetTypeServerCleartext, packetTypeServerStatelessRetry, packetTypeVersionNegotiation:
return false
}
return true
}
func (p *packetHeader) hasConnId() bool {
if isLongHeader(p) {
return true
}
if (p.Type & packetFlagC) != 0 {
return true
}
return false
}
func (p *packetHeader) getHeaderType() byte {
if isLongHeader(p) {
return p.Type & 0x7f
}
// Short header.
if (p.Type & packetFlagK) != 0 {
return packetType1RTTProtectedPhase1
}
return packetType1RTTProtectedPhase0
}
func (p packetHeader) ConnectionID__length() uintptr {
if isLongHeader(&p) || isSet(p.Type, packetFlagC) {
return 8
}
return codecDefaultSize
}
func (p packetHeader) PacketNumber__length() uintptr {
logf(logTypeTrace, "PacketNumber__length()")
if isLongHeader(&p) {
return 4
}
switch p.Type {
case 1, 2, 3:
return 1 << p.Type
default:
return 4
}
}
func (p packetHeader) Version__length() uintptr {
if isLongHeader(&p) {
return 4
}
return codecDefaultSize
}
func (p *packetHeader) setLongHeaderType(typ byte) {
p.Type = packetFlagLongHeader | typ
}
type versionNegotiationPacket struct {
Versions []byte
}
func newVersionNegotiationPacket(versions []VersionNumber) *versionNegotiationPacket {
var buf bytes.Buffer
for _, v := range versions {
buf.Write(encodeArgs(v))
}
return &versionNegotiationPacket{buf.Bytes()}
}
/*
We don't use these.
func encodePacket(c ConnectionState, aead Aead, p *Packet) ([]byte, error) {
hdr, err := encode(&p.packetHeader)
if err != nil {
return nil, err
}
b, err := aead.protect(p.packetHeader.PacketNumber, hdr, p.payload)
if err != nil {
return nil, err
}
return encodeArgs(hdr, b), nil
}
func decodePacket(c ConnectionState, aead Aead, b []byte) (*Packet, error) {
// Parse the header
var hdr packetHeader
br, err := decode(&hdr, b)
if err != nil {
return nil, err
}
hdr.PacketNumber = c.expandPacketNumber(hdr.PacketNumber)
pt, err := aead.unprotect(hdr.PacketNumber, b[0:br], b[br:])
if err != nil {
return nil, err
}
return &Packet{hdr, pt}, nil
}
*/