Skip to content

Commit

Permalink
Adds a lock to PacketNumber, fixes #23
Browse files Browse the repository at this point in the history
  • Loading branch information
mpiraux committed Apr 28, 2020
1 parent 7059d3a commit 2cbac1f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions agents/send_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ func (a *SendingAgent) Run(conn *Connection) {

if len(packet.GetFrames()) == 0 {
a.Logger.Printf("Preparing a packet for encryption level %s resulted in an empty packet, discarding\n", level.String())
conn.PacketNumberLock.Lock()
conn.PacketNumber[packet.PNSpace()]-- // Avoids PN skipping
conn.PacketNumberLock.Unlock()
return nil
}
return packet
Expand Down
4 changes: 4 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type Connection struct {
Token []byte
ResumptionTicket []byte

PacketNumberLock sync.Locker
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
Expand All @@ -83,8 +84,10 @@ func (c *Connection) ConnectedIp() net.Addr {
return c.UdpConnection.RemoteAddr()
}
func (c *Connection) nextPacketNumber(space PNSpace) PacketNumber { // TODO: This should be thread safe
c.PacketNumberLock.Lock()
pn := c.PacketNumber[space]
c.PacketNumber[space]++
c.PacketNumberLock.Unlock()
return pn
}
func (c *Connection) EncodeAndEncrypt(packet Packet, level EncryptionLevel) []byte {
Expand Down Expand Up @@ -237,6 +240,7 @@ func (c *Connection) TransitionTo(version uint32, ALPN string) {
c.Version = version
c.ALPN = ALPN
c.Tls = pigotls.NewConnection(c.ServerName, c.ALPN, c.ResumptionTicket)
c.PacketNumberLock = &sync.Mutex{}
c.PacketNumber = make(map[PNSpace]PacketNumber)
c.LargestPNsReceived = make(map[PNSpace]PacketNumber)
c.LargestPNsAcknowledged = make(map[PNSpace]PacketNumber)
Expand Down

0 comments on commit 2cbac1f

Please sign in to comment.