forked from QUIC-Tracker/quic-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
headers.go
210 lines (193 loc) · 6.44 KB
/
headers.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
package quictracker
import (
"bytes"
"encoding/binary"
)
type PacketType uint8
const (
Initial PacketType = 0x0
ZeroRTTProtected PacketType = 0x1
Handshake PacketType = 0x2
Retry PacketType = 0x3
VersionNegotiation PacketType = 0xfe // TODO: Find a way around this
ShortHeaderPacket PacketType = 0xff // TODO: Find a way around this
)
var packetTypeToString = map[PacketType]string{
Initial: "Initial",
Retry: "Retry",
Handshake: "Handshake",
ZeroRTTProtected: "0-RTT Protected",
ShortHeaderPacket: "1-RTT Protected",
}
var packetTypeToPNSPace = map[PacketType]PNSpace {
Initial: PNSpaceInitial,
Retry: PNSpaceNoSpace,
Handshake: PNSpaceHandshake,
ZeroRTTProtected: PNSpaceAppData,
ShortHeaderPacket: PNSpaceAppData,
}
type Header interface {
PacketType() PacketType
DestinationConnectionID() ConnectionID
PacketNumber() PacketNumber
TruncatedPN() TruncatedPN
EncryptionLevel() EncryptionLevel
Encode() []byte
HeaderLength() int
}
func ReadHeader(buffer *bytes.Reader, conn *Connection) Header {
var h Header
typeByte, _ := buffer.ReadByte()
buffer.UnreadByte()
if typeByte & 0x80 == 0x80 {
h = ReadLongHeader(buffer, conn)
} else {
h = ReadShortHeader(buffer, conn)
}
return h
}
type LongHeader struct {
packetType PacketType
lowerBits byte
Version uint32
DestinationCID ConnectionID
SourceCID ConnectionID
TokenLength VarInt
Token []byte
Length VarInt
packetNumber PacketNumber
truncatedPN TruncatedPN
}
func (h *LongHeader) Encode() []byte {
buffer := new(bytes.Buffer)
typeByte := uint8(0xC0)
typeByte |= uint8(h.packetType) << 4
typeByte |= uint8(h.truncatedPN.Length) - 1
binary.Write(buffer, binary.BigEndian, typeByte)
binary.Write(buffer, binary.BigEndian, h.Version)
buffer.WriteByte(h.DestinationCID.CIDL())
binary.Write(buffer, binary.BigEndian, h.DestinationCID)
buffer.WriteByte(h.SourceCID.CIDL())
binary.Write(buffer, binary.BigEndian, h.SourceCID)
if h.packetType == Initial {
buffer.Write(h.TokenLength.Encode())
buffer.Write(h.Token)
}
if h.packetType != Retry {
buffer.Write(h.Length.Encode())
buffer.Write(h.truncatedPN.Encode())
}
return buffer.Bytes()
}
func (h *LongHeader) PacketType() PacketType { return h.packetType }
func (h *LongHeader) DestinationConnectionID() ConnectionID { return h.DestinationCID }
func (h *LongHeader) PacketNumber() PacketNumber { return h.packetNumber }
func (h *LongHeader) TruncatedPN() TruncatedPN { return h.truncatedPN }
func (h *LongHeader) EncryptionLevel() EncryptionLevel { return packetTypeToEncryptionLevel[h.PacketType()] }
func (h *LongHeader) HeaderLength() int {
length := 7 + len(h.DestinationCID) + len(h.SourceCID) + h.Length.Length + h.truncatedPN.Length
if h.packetType == Initial {
length += h.TokenLength.Length + len(h.Token)
}
return length
}
func ReadLongHeader(buffer *bytes.Reader, conn *Connection) *LongHeader {
h := new(LongHeader)
typeByte, _ := buffer.ReadByte()
h.lowerBits = typeByte & 0x0F
h.packetType = PacketType(typeByte - 0xC0) >> 4
binary.Read(buffer, binary.BigEndian, &h.Version)
DCIL, _ := buffer.ReadByte()
h.DestinationCID = make([]byte, DCIL, DCIL)
binary.Read(buffer, binary.BigEndian, &h.DestinationCID)
SCIL, _ := buffer.ReadByte()
h.SourceCID = make([]byte, SCIL, SCIL)
binary.Read(buffer, binary.BigEndian, &h.SourceCID)
if h.packetType == Initial {
h.TokenLength, _ = ReadVarInt(buffer)
h.Token = make([]byte, h.TokenLength.Value)
buffer.Read(h.Token)
}
if h.packetType != Retry {
h.Length, _ = ReadVarInt(buffer)
h.truncatedPN = ReadTruncatedPN(buffer, int(typeByte & 0x3) + 1)
h.packetNumber = h.truncatedPN.Join(conn.LargestPNsReceived[h.packetType.PNSpace()])
}
return h
}
func NewLongHeader(packetType PacketType, conn *Connection, space PNSpace) *LongHeader {
h := new(LongHeader)
h.packetType = packetType
h.Version = conn.Version
h.SourceCID = conn.SourceCID
if packetType == ZeroRTTProtected {
h.DestinationCID = conn.OriginalDestinationCID
} else {
h.DestinationCID = conn.DestinationCID
}
h.TokenLength = NewVarInt(0)
h.packetNumber = conn.nextPacketNumber(space)
h.truncatedPN = h.packetNumber.Truncate(conn.LargestPNsAcknowledged[h.packetType.PNSpace()])
return h
}
func (t PacketType) String() string {
return packetTypeToString[t]
}
func (t PacketType) PNSpace() PNSpace {
return packetTypeToPNSPace[t]
}
type ShortHeader struct {
SpinBit SpinBit
KeyPhase KeyPhaseBit
DestinationCID ConnectionID
truncatedPN TruncatedPN
packetNumber PacketNumber
}
func (h *ShortHeader) Encode() []byte {
buffer := new(bytes.Buffer)
var typeByte uint8
typeByte |= 0x40
if h.SpinBit == SpinValueOne {
typeByte |= 0x20
}
if h.KeyPhase == KeyPhaseOne {
typeByte |= 0x04
}
typeByte |= uint8(h.truncatedPN.Length) - 1
binary.Write(buffer, binary.BigEndian, typeByte)
binary.Write(buffer, binary.BigEndian, h.DestinationCID)
buffer.Write(h.truncatedPN.Encode())
return buffer.Bytes()
}
func (h *ShortHeader) PacketType() PacketType { return ShortHeaderPacket }
func (h *ShortHeader) DestinationConnectionID() ConnectionID { return h.DestinationCID }
func (h *ShortHeader) PacketNumber() PacketNumber { return h.packetNumber }
func (h *ShortHeader) TruncatedPN() TruncatedPN { return h.truncatedPN }
func (h *ShortHeader) EncryptionLevel() EncryptionLevel { return packetTypeToEncryptionLevel[h.PacketType()] }
func (h *ShortHeader) HeaderLength() int { return 1 + len(h.DestinationCID) + h.truncatedPN.Length }
func ReadShortHeader(buffer *bytes.Reader, conn *Connection) *ShortHeader {
h := new(ShortHeader)
typeByte, _ := buffer.ReadByte()
h.SpinBit = (typeByte & 0x20) == 0x20
h.KeyPhase = (typeByte & 0x04) == 0x04
h.DestinationCID = make([]byte, len(conn.SourceCID))
buffer.Read(h.DestinationCID)
h.truncatedPN = ReadTruncatedPN(buffer, int(typeByte&0x3) + 1)
h.packetNumber = h.truncatedPN.Join(conn.LargestPNsReceived[PNSpaceAppData])
return h
}
func NewShortHeader(conn *Connection) *ShortHeader {
h := new(ShortHeader)
h.SpinBit = conn.SpinBit
h.KeyPhase = conn.KeyPhaseIndex % 2 == 1
h.DestinationCID = conn.DestinationCID
h.packetNumber = conn.nextPacketNumber(PNSpaceAppData)
h.truncatedPN = h.packetNumber.Truncate(conn.LargestPNsAcknowledged[PNSpaceAppData])
return h
}
type KeyPhaseBit bool
const KeyPhaseZero KeyPhaseBit = false
const KeyPhaseOne KeyPhaseBit = true
type SpinBit bool
const SpinValueZero SpinBit = false
const SpinValueOne SpinBit = true