-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconnection.go
292 lines (257 loc) · 9.55 KB
/
connection.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
package quictracker
import (
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
. "github.com/QUIC-Tracker/quic-tracker/lib"
"github.com/dustin/go-broadcast"
"github.com/mpiraux/pigotls"
"log"
"net"
"os"
"sort"
"strings"
"unsafe"
)
type Connection struct {
ServerName string
UdpConnection *net.UDPConn
UseIPv6 bool
Host *net.UDPAddr
Tls *pigotls.Connection
TLSTPHandler *TLSTransportParameterHandler
KeyPhaseIndex uint
CryptoStates map[EncryptionLevel]*CryptoState
ExporterSecret []byte
ReceivedPacketHandler func([]byte, unsafe.Pointer)
SentPacketHandler func([]byte, unsafe.Pointer)
CryptoStreams CryptoStreams // TODO: It should be a parent class without closing states
Streams Streams
IncomingPackets broadcast.Broadcaster //type: Packet
OutgoingPackets broadcast.Broadcaster //type: Packet
IncomingPayloads broadcast.Broadcaster //type: []byte
UnprocessedPayloads broadcast.Broadcaster //type: UnprocessedPayload
EncryptionLevelsAvailable broadcast.Broadcaster //type: DirectionalEncryptionLevel
FrameQueue broadcast.Broadcaster //type: QueuedFrame
OriginalDestinationCID ConnectionID
SourceCID ConnectionID
DestinationCID ConnectionID
Version uint32
ALPN string
Token []byte
ResumptionTicket []byte
PacketNumber map[PNSpace]PacketNumber // Stores the next PN to be sent
LargestPNsReceived map[PNSpace]PacketNumber // Stores the largest PN received
LargestPNsAcknowledged map[PNSpace]PacketNumber // Stores the largest PN we have sent that were acknowledged by the peer
AckQueue map[PNSpace][]PacketNumber // Stores the packet numbers to be acked TODO: This should be a channel actually
Logger *log.Logger
}
func (c *Connection) ConnectedIp() net.Addr {
return c.UdpConnection.RemoteAddr()
}
func (c *Connection) nextPacketNumber(space PNSpace) PacketNumber { // TODO: This should be thread safe
pn := c.PacketNumber[space]
c.PacketNumber[space]++
return pn
}
func (c *Connection) SendPacket(packet Packet, level EncryptionLevel) {
switch packet.PNSpace() {
case PNSpaceInitial, PNSpaceHandshake, PNSpaceAppData:
c.Logger.Printf("Sending packet {type=%s, number=%d}\n", packet.Header().PacketType().String(), packet.Header().PacketNumber())
cryptoState := c.CryptoStates[level]
payload := packet.EncodePayload()
if h, ok := packet.Header().(*LongHeader); ok {
h.Length = NewVarInt(uint64(h.TruncatedPN().Length + len(payload) + cryptoState.Write.Overhead()))
}
header := packet.EncodeHeader()
protectedPayload := cryptoState.Write.Encrypt(payload, uint64(packet.Header().PacketNumber()), header)
packetBytes := append(header, protectedPayload...)
firstByteMask := byte(0x1F)
if packet.Header().PacketType() != ShortHeaderPacket {
firstByteMask = 0x0F
}
sample, pnOffset := GetPacketSample(packet.Header(), packetBytes)
mask := cryptoState.HeaderWrite.Encrypt(sample, make([]byte, 5, 5))
packetBytes[0] ^= mask[0] & firstByteMask
for i := 0; i < packet.Header().TruncatedPN().Length; i++ {
packetBytes[pnOffset+i] ^= mask[1+i]
}
c.UdpConnection.Write(packetBytes)
if c.SentPacketHandler != nil {
c.SentPacketHandler(packet.Encode(packet.EncodePayload()), packet.Pointer())
}
c.OutgoingPackets.Submit(packet)
default:
// Clients do not send cleartext packets
}
}
func (c *Connection) GetInitialPacket() *InitialPacket {
extensionData, err := c.TLSTPHandler.GetExtensionData()
if err != nil {
println(err)
return nil
}
c.Tls.SetQUICTransportParameters(extensionData)
tlsOutput, notComplete, err := c.Tls.HandleMessage(nil, pigotls.EpochInitial)
if err != nil || !notComplete {
println(err.Error())
return nil
}
clientHello := tlsOutput[0].Data
cryptoFrame := NewCryptoFrame(c.CryptoStreams.Get(PNSpaceInitial), clientHello)
if len(c.Tls.ZeroRTTSecret()) > 0 {
c.Logger.Printf("0-RTT secret is available, installing crypto state")
c.CryptoStates[EncryptionLevel0RTT] = NewProtectedCryptoState(c.Tls, nil, c.Tls.ZeroRTTSecret())
c.EncryptionLevelsAvailable.Submit(DirectionalEncryptionLevel{EncryptionLevel0RTT, false})
}
var initialLength int
if c.UseIPv6 {
initialLength = MinimumInitialLengthv6
} else {
initialLength = MinimumInitialLength
}
initialPacket := NewInitialPacket(c)
initialPacket.Frames = append(initialPacket.Frames, cryptoFrame)
payloadLen := len(initialPacket.EncodePayload())
paddingLength := initialLength - (len(initialPacket.header.Encode()) + int(VarIntLen(uint64(payloadLen))) + payloadLen + c.CryptoStates[EncryptionLevelInitial].Write.Overhead())
for i := 0; i < paddingLength; i++ {
initialPacket.Frames = append(initialPacket.Frames, new(PaddingFrame))
}
return initialPacket
}
func (c *Connection) ProcessVersionNegotation(vn *VersionNegotiationPacket) error {
var version uint32
for _, v := range vn.SupportedVersions {
if v >= MinimumVersion && v <= MaximumVersion {
version = uint32(v)
}
}
if version == 0 {
c.Logger.Println("No appropriate version was found in the VN packet")
c.Logger.Printf("Versions received: %v\n", vn.SupportedVersions)
return errors.New("no appropriate version found")
}
QuicVersion = version
QuicALPNToken = fmt.Sprintf("%s-%02d", strings.Split(c.ALPN, "-")[0], version & 0xff)
c.TransitionTo(QuicVersion, QuicALPNToken)
return nil
}
func (c *Connection) GetAckFrame(space PNSpace) *AckFrame { // Returns an ack frame based on the packet numbers received
sort.Sort(PacketNumberQueue(c.AckQueue[space]))
packetNumbers := c.AckQueue[space]
if len(packetNumbers) == 0 {
return nil
}
frame := new(AckFrame)
frame.AckBlocks = make([]AckBlock, 0, 255)
frame.LargestAcknowledged = packetNumbers[0]
previous := frame.LargestAcknowledged
ackBlock := AckBlock{}
for _, number := range packetNumbers[1:] {
if previous - number == 1 {
ackBlock.Block++
} else {
frame.AckBlocks = append(frame.AckBlocks, ackBlock)
ackBlock = AckBlock{uint64(previous) - uint64(number) - 2, 0}
}
previous = number
}
frame.AckBlocks = append(frame.AckBlocks, ackBlock)
if len(frame.AckBlocks) > 0 {
frame.AckBlockCount = uint64(len(frame.AckBlocks) - 1)
}
return frame
}
func (c *Connection) TransitionTo(version uint32, ALPN string) {
var prevVersion uint32
if c.Version == 0 {
prevVersion = QuicVersion
} else {
prevVersion = c.Version
}
c.TLSTPHandler = NewTLSTransportParameterHandler(version, prevVersion)
c.Version = version
c.ALPN = ALPN
c.Tls = pigotls.NewConnection(c.ServerName, c.ALPN, c.ResumptionTicket)
c.PacketNumber = make(map[PNSpace]PacketNumber)
c.LargestPNsReceived = make(map[PNSpace]PacketNumber)
c.LargestPNsAcknowledged = make(map[PNSpace]PacketNumber)
c.AckQueue = make(map[PNSpace][]PacketNumber)
for _, space := range []PNSpace{PNSpaceInitial, PNSpaceHandshake, PNSpaceAppData} {
c.PacketNumber[space] = 0
c.AckQueue[space] = nil
}
c.CryptoStates = make(map[EncryptionLevel]*CryptoState)
c.CryptoStreams = make(map[PNSpace]*Stream)
c.CryptoStates[EncryptionLevelInitial] = NewInitialPacketProtection(c)
c.Streams = make(map[uint64]*Stream)
}
func (c *Connection) CloseConnection(quicLayer bool, errCode uint16, reasonPhrase string) {
if quicLayer {
c.FrameQueue.Submit(QueuedFrame{&ConnectionCloseFrame{errCode,0, uint64(len(reasonPhrase)), reasonPhrase}, EncryptionLevelBest})
} else {
c.FrameQueue.Submit(QueuedFrame{&ApplicationCloseFrame{errCode, uint64(len(reasonPhrase)), reasonPhrase}, EncryptionLevelBest})
}
}
func (c *Connection) SendHTTPGETRequest(path string, streamID uint64) {
c.FrameQueue.Submit(QueuedFrame{NewStreamFrame(streamID, c.Streams.Get(streamID), []byte(fmt.Sprintf("GET %s\r\n", path)), true), EncryptionLevelBestAppData})
}
func (c *Connection) Close() {
c.Tls.Close()
c.UdpConnection.Close()
}
func EstablishUDPConnection(addr *net.UDPAddr) (*net.UDPConn, error) {
udpConn, err := net.DialUDP(addr.Network(), nil, addr)
if err != nil {
return nil, err
}
return udpConn, nil
}
func NewDefaultConnection(address string, serverName string, resumptionTicket []byte, useIPv6 bool, negotiateHTTP3 bool) (*Connection, error) {
scid := make([]byte, 8, 8)
dcid := make([]byte, 8, 8)
rand.Read(scid)
rand.Read(dcid)
var network string
if useIPv6 {
network = "udp6"
} else {
network = "udp4"
}
udpAddr, err := net.ResolveUDPAddr(network, address)
if err != nil {
return nil, err
}
udpConn, err := EstablishUDPConnection(udpAddr)
if err != nil {
return nil, err
}
var c *Connection
if negotiateHTTP3 {
c = NewConnection(serverName, QuicVersion, QuicH3ALPNToken, scid, dcid, udpConn, resumptionTicket)
} else {
c = NewConnection(serverName, QuicVersion, QuicALPNToken, scid, dcid, udpConn, resumptionTicket)
}
c.UseIPv6 = useIPv6
c.Host = udpAddr
return c, nil
}
func NewConnection(serverName string, version uint32, ALPN string, SCID []byte, DCID[]byte , udpConn *net.UDPConn, resumptionTicket []byte) *Connection {
c := new(Connection)
c.ServerName = serverName
c.UdpConnection = udpConn
c.SourceCID = SCID
c.DestinationCID = DCID
c.OriginalDestinationCID = DCID
c.ResumptionTicket = resumptionTicket
c.IncomingPackets = broadcast.NewBroadcaster(1000)
c.OutgoingPackets = broadcast.NewBroadcaster(1000)
c.IncomingPayloads = broadcast.NewBroadcaster(1000)
c.UnprocessedPayloads = broadcast.NewBroadcaster(1000)
c.EncryptionLevelsAvailable = broadcast.NewBroadcaster(10)
c.FrameQueue = broadcast.NewBroadcaster(1000)
c.Logger = log.New(os.Stderr, fmt.Sprintf("[CID %s] ", hex.EncodeToString(c.OriginalDestinationCID)), log.Lshortfile)
c.TransitionTo(version, ALPN)
return c
}