Skip to content

Commit

Permalink
Adds additional info to received packets
Browse files Browse the repository at this point in the history
  • Loading branch information
mpiraux committed Mar 25, 2019
1 parent 14981a0 commit 8174bc4
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 40 deletions.
6 changes: 3 additions & 3 deletions agents/buffer_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (a *BufferAgent) Run(conn *Connection) {
uPChan := conn.UnprocessedPayloads.RegisterNewChan(1000)
eLChan := conn.EncryptionLevelsAvailable.RegisterNewChan(1000)

unprocessedPayloads := make(map[EncryptionLevel][][]byte)
unprocessedPayloads := make(map[EncryptionLevel][]IncomingPayload)
encryptionLevelsAvailable := make(map[EncryptionLevel]bool)

go func() {
Expand All @@ -25,9 +25,9 @@ func (a *BufferAgent) Run(conn *Connection) {
case i := <-uPChan:
u := i.(UnprocessedPayload)
if !encryptionLevelsAvailable[u.EncryptionLevel] {
unprocessedPayloads[u.EncryptionLevel] = append(unprocessedPayloads[u.EncryptionLevel], u.Payload)
unprocessedPayloads[u.EncryptionLevel] = append(unprocessedPayloads[u.EncryptionLevel], u.IncomingPayload)
} else {
conn.IncomingPayloads.Submit(u.Payload)
conn.IncomingPayloads.Submit(u.IncomingPayload)
}
case i := <-eLChan:
dEL := i.(DirectionalEncryptionLevel)
Expand Down
15 changes: 8 additions & 7 deletions agents/parse_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ func (a *ParsingAgent) Run(conn *Connection) {
packetSelect:
select {
case i := <-incomingPayloads:
udpPayload := i.([]byte)
ic := i.(IncomingPayload)
var off int
for off < len(udpPayload) {
ciphertext := udpPayload[off:]
for off < len(ic.Payload) {
ciphertext := ic.Payload[off:]

if bytes.Equal(ciphertext[1:5], []byte{0, 0, 0, 0}) {
packet := ReadVersionNegotationPacket(bytes.NewReader(ciphertext))
Expand Down Expand Up @@ -64,7 +64,7 @@ func (a *ParsingAgent) Run(conn *Connection) {
header = ReadHeader(bytes.NewReader(ciphertext), a.conn) // Update PN
} else {
a.Logger.Printf("Crypto state for %s packet of length %d bytes is not ready, putting it back in waiting buffer\n", header.PacketType().String(), len(ciphertext))
a.conn.UnprocessedPayloads.Submit(UnprocessedPayload{header.EncryptionLevel(), ciphertext})
a.conn.UnprocessedPayloads.Submit(UnprocessedPayload{ic, header.EncryptionLevel()})
break packetSelect
}
}
Expand Down Expand Up @@ -105,13 +105,13 @@ func (a *ParsingAgent) Run(conn *Connection) {
a.Logger.Printf("Could not decrypt packet {type=%s, number=%d}\n", header.PacketType().String(), header.PacketNumber())
break packetSelect
}
cleartext = append(append(cleartext, udpPayload[off:off+hLen]...), payload...)
cleartext = append(append(cleartext, ic.Payload[off:off+hLen]...), payload...)
packet = ReadProtectedPacket(bytes.NewReader(cleartext), a.conn)
off = len(udpPayload)
off = len(ic.Payload)
case Retry:
cleartext = ciphertext
packet = ReadRetryPacket(bytes.NewReader(cleartext), a.conn)
off = len(udpPayload)
off = len(ic.Payload)
default:
a.Logger.Printf("Packet type is unknown, the first byte is %x\n", ciphertext[0])
break packetSelect
Expand All @@ -126,6 +126,7 @@ func (a *ParsingAgent) Run(conn *Connection) {
}
}

packet.SetContext(ic.ReceiveContext)
a.conn.IncomingPackets.Submit(packet)
a.SaveCleartextPacket(cleartext, packet.Pointer())

Expand Down
28 changes: 11 additions & 17 deletions agents/socket_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,10 @@ import (
. "github.com/QUIC-Tracker/quic-tracker"
"github.com/QUIC-Tracker/quic-tracker/compat"
"syscall"
"time"
"unsafe"
)

type ECNStatus int

const (
ECNStatusNonECT ECNStatus = 0
ECNStatusECT_1 = 1
ECNStatusECT_0 = 2
ECNStatusCE = 3
)

// The SocketAgent is responsible for receiving the UDP payloads off the socket and putting them in the decryption queue.
// If configured using ConfigureECN(), it will also mark the packet as with ECN(0) and report the ECN status of
// the corresponding IP packet received.
Expand All @@ -27,21 +19,19 @@ type SocketAgent struct {
TotalDataReceived int
DatagramsReceived int
SocketStatus Broadcaster //type: err
ECNStatus Broadcaster //type: ECNStatus
}

func (a *SocketAgent) Run(conn *Connection) {
a.Init("SocketAgent", conn.OriginalDestinationCID)
a.conn = conn
a.SocketStatus = NewBroadcaster(10)
a.ECNStatus = NewBroadcaster(1000)
recChan := make(chan []byte)
recChan := make(chan IncomingPayload)

go func() {
for {
recBuf := make([]byte, MaxUDPPayloadSize)
oob := make([]byte, 128) // Find a reasonable upper-bound
i, oobn, _, _, err := conn.UdpConnection.ReadMsgUDP(recBuf, oob)
i, oobn, _, addr, err := conn.UdpConnection.ReadMsgUDP(recBuf, oob)

if err != nil {
a.Logger.Println("Closing UDP socket because of error", err.Error())
Expand All @@ -55,27 +45,31 @@ func (a *SocketAgent) Run(conn *Connection) {
break
}

sm := IncomingPayload{}
sm.Timestamp = time.Now()
sm.Payload = make([]byte, i)
copy(sm.Payload, recBuf[:i])
sm.RemoteAddr = addr

if a.ecn {
ecn, err := findECNValue(oob[:oobn])
if err != nil {
a.Logger.Println(err.Error())
}
ecn = ecn & 0x03
a.Logger.Printf("Read ECN value %d\n", ecn)
a.ECNStatus.Submit(ECNStatus(ecn))
sm.ECNStatus = ECNStatus(ecn)
}

a.TotalDataReceived += i
a.DatagramsReceived += 1
a.Logger.Printf("Received %d bytes from UDP socket\n", i)
payload := make([]byte, i)
copy(payload, recBuf[:i])
select {
case <-recChan:
return
default:
}
recChan <- payload
recChan <- sm
}
}()

Expand Down
24 changes: 23 additions & 1 deletion common.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import (
_ "github.com/mpiraux/ls-qpack-go"
"github.com/mpiraux/pigotls"
"math"
"net"
"time"
)

// TODO: Reconsider the use of global variables
Expand Down Expand Up @@ -224,9 +226,29 @@ func max(a, b uint64) uint64 {
return b
}

type ECNStatus int

const (
ECNStatusNonECT ECNStatus = 0
ECNStatusECT_1 = 1
ECNStatusECT_0 = 2
ECNStatusCE = 3
)

type ReceiveContext struct {
Timestamp time.Time
RemoteAddr net.Addr
ECNStatus
}

type IncomingPayload struct {
ReceiveContext
Payload []byte
}

type UnprocessedPayload struct {
IncomingPayload
EncryptionLevel
Payload []byte
}

type QueuedFrame struct {
Expand Down
2 changes: 1 addition & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Connection struct {

IncomingPackets Broadcaster //type: Packet
OutgoingPackets Broadcaster //type: Packet
IncomingPayloads Broadcaster //type: []byte
IncomingPayloads Broadcaster //type: IncomingPayload
UnprocessedPayloads Broadcaster //type: UnprocessedPayload
EncryptionLevelsAvailable Broadcaster //type: DirectionalEncryptionLevel
FrameQueue Broadcaster //type: QueuedFrame
Expand Down
18 changes: 14 additions & 4 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package quictracker
import (
"bytes"
"encoding/binary"
"io"
"encoding/hex"
"fmt"
"github.com/davecgh/go-spew/spew"
"io"
"unsafe"
"fmt"
"encoding/hex"
)

type Packet interface {
Expand All @@ -20,14 +20,23 @@ type Packet interface {
PNSpace() PNSpace
EncryptionLevel() EncryptionLevel
ShortString() string
ReceiveContext() ReceiveContext
SetContext(ctx ReceiveContext)
}

type abstractPacket struct {
header Header
receiveContext ReceiveContext
}
func (p abstractPacket) Header() Header {
return p.header
}
func (p abstractPacket) ReceiveContext() ReceiveContext {
return p.receiveContext
}
func (p *abstractPacket) SetContext(ctx ReceiveContext) {
p.receiveContext = ctx
}
func (p abstractPacket) EncodeHeader() []byte {
return p.header.Encode()
}
Expand Down Expand Up @@ -335,4 +344,5 @@ func NewZeroRTTProtectedPacket(conn *Connection) *ZeroRTTProtectedPacket {
p := new(ZeroRTTProtectedPacket)
p.header = NewLongHeader(ZeroRTTProtected, conn, PNSpaceAppData)
return p
}
}

10 changes: 3 additions & 7 deletions scenarii/ack_ecn.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ func (s *AckECNScenario) Run(conn *qt.Connection, trace *qt.Trace, preferredPath
defer connAgents.CloseConnection(false, 0, "")

incPackets := conn.IncomingPackets.RegisterNewChan(1000)

socketAgent := connAgents.Get("SocketAgent").(*agents.SocketAgent)
ecnStatus := socketAgent.ECNStatus.RegisterNewChan(1000)


err := socketAgent.ConfigureECN()
if err != nil {
Expand All @@ -55,10 +52,9 @@ func (s *AckECNScenario) Run(conn *qt.Connection, trace *qt.Trace, preferredPath
}
}
}
case i := <-ecnStatus:
switch i.(agents.ECNStatus) {
case agents.ECNStatusNonECT:
case agents.ECNStatusECT_0, agents.ECNStatusECT_1, agents.ECNStatusCE:
switch i.(qt.Packet).ReceiveContext().ECNStatus {
case qt.ECNStatusNonECT:
case qt.ECNStatusECT_0, qt.ECNStatusECT_1, qt.ECNStatusCE:
if trace.ErrorCode == AE_NonECN {
trace.ErrorCode = AE_NoACKECNReceived
} else if trace.ErrorCode == AE_NonECNButACKECN {
Expand Down

0 comments on commit 8174bc4

Please sign in to comment.