2022-04-01 16:16:46 +00:00
|
|
|
package quic
|
|
|
|
|
|
|
|
import (
|
2022-11-04 13:57:20 +00:00
|
|
|
"math/bits"
|
|
|
|
"net"
|
2022-04-01 16:16:46 +00:00
|
|
|
|
|
|
|
"github.com/lucas-clemente/quic-go/internal/protocol"
|
|
|
|
"github.com/lucas-clemente/quic-go/internal/utils"
|
|
|
|
)
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
// A closedLocalConn is a connection that we closed locally.
|
|
|
|
// When receiving packets for such a connection, we need to retransmit the packet containing the CONNECTION_CLOSE frame,
|
2022-04-01 16:16:46 +00:00
|
|
|
// with an exponential backoff.
|
2022-08-19 16:34:07 +00:00
|
|
|
type closedLocalConn struct {
|
2022-11-04 13:57:20 +00:00
|
|
|
counter uint32
|
2022-04-01 16:16:46 +00:00
|
|
|
perspective protocol.Perspective
|
2022-11-04 13:57:20 +00:00
|
|
|
logger utils.Logger
|
2022-04-01 16:16:46 +00:00
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
sendPacket func(net.Addr, *packetInfo)
|
2022-04-01 16:16:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
var _ packetHandler = &closedLocalConn{}
|
2022-04-01 16:16:46 +00:00
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
// newClosedLocalConn creates a new closedLocalConn and runs it.
|
2022-11-04 13:57:20 +00:00
|
|
|
func newClosedLocalConn(sendPacket func(net.Addr, *packetInfo), pers protocol.Perspective, logger utils.Logger) packetHandler {
|
|
|
|
return &closedLocalConn{
|
|
|
|
sendPacket: sendPacket,
|
|
|
|
perspective: pers,
|
|
|
|
logger: logger,
|
2022-04-01 16:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
func (c *closedLocalConn) handlePacket(p *receivedPacket) {
|
|
|
|
c.counter++
|
2022-04-01 16:16:46 +00:00
|
|
|
// exponential backoff
|
|
|
|
// only send a CONNECTION_CLOSE for the 1st, 2nd, 4th, 8th, 16th, ... packet arriving
|
2022-11-04 13:57:20 +00:00
|
|
|
if bits.OnesCount32(c.counter) != 1 {
|
|
|
|
return
|
2022-04-01 16:16:46 +00:00
|
|
|
}
|
2022-11-04 13:57:20 +00:00
|
|
|
c.logger.Debugf("Received %d packets after sending CONNECTION_CLOSE. Retransmitting.", c.counter)
|
|
|
|
c.sendPacket(p.remoteAddr, p.info)
|
2022-04-01 16:16:46 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
func (c *closedLocalConn) shutdown() {}
|
|
|
|
func (c *closedLocalConn) destroy(error) {}
|
|
|
|
func (c *closedLocalConn) getPerspective() protocol.Perspective { return c.perspective }
|
2022-04-01 16:16:46 +00:00
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
// A closedRemoteConn is a connection that was closed remotely.
|
|
|
|
// For such a connection, we might receive reordered packets that were sent before the CONNECTION_CLOSE.
|
2022-04-01 16:16:46 +00:00
|
|
|
// We can just ignore those packets.
|
2022-08-19 16:34:07 +00:00
|
|
|
type closedRemoteConn struct {
|
2022-04-01 16:16:46 +00:00
|
|
|
perspective protocol.Perspective
|
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
var _ packetHandler = &closedRemoteConn{}
|
2022-04-01 16:16:46 +00:00
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
func newClosedRemoteConn(pers protocol.Perspective) packetHandler {
|
|
|
|
return &closedRemoteConn{perspective: pers}
|
2022-04-01 16:16:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
func (s *closedRemoteConn) handlePacket(*receivedPacket) {}
|
|
|
|
func (s *closedRemoteConn) shutdown() {}
|
|
|
|
func (s *closedRemoteConn) destroy(error) {}
|
|
|
|
func (s *closedRemoteConn) getPerspective() protocol.Perspective { return s.perspective }
|