only skip packet numbers in the application data packet number space

This commit is contained in:
Marten Seemann 2020-11-26 13:31:26 +07:00
parent e8e6c7b47e
commit 416d88990b
3 changed files with 67 additions and 24 deletions

View file

@ -8,10 +8,35 @@ import (
"github.com/lucas-clemente/quic-go/internal/protocol"
)
// The packetNumberGenerator generates the packet number for the next packet
type packetNumberGenerator interface {
Peek() protocol.PacketNumber
Pop() protocol.PacketNumber
}
type sequentialPacketNumberGenerator struct {
next protocol.PacketNumber
}
var _ packetNumberGenerator = &sequentialPacketNumberGenerator{}
func newSequentialPacketNumberGenerator(initial protocol.PacketNumber) packetNumberGenerator {
return &sequentialPacketNumberGenerator{next: initial}
}
func (p *sequentialPacketNumberGenerator) Peek() protocol.PacketNumber {
return p.next
}
func (p *sequentialPacketNumberGenerator) Pop() protocol.PacketNumber {
next := p.next
p.next++
return next
}
// The skippingPacketNumberGenerator generates the packet number for the next packet
// it randomly skips a packet number every averagePeriod packets (on average).
// It is guaranteed to never skip two consecutive packet numbers.
type packetNumberGenerator struct {
type skippingPacketNumberGenerator struct {
rand *mrand.Rand
averagePeriod protocol.PacketNumber
@ -19,10 +44,12 @@ type packetNumberGenerator struct {
nextToSkip protocol.PacketNumber
}
func newPacketNumberGenerator(initial, averagePeriod protocol.PacketNumber) *packetNumberGenerator {
var _ packetNumberGenerator = &skippingPacketNumberGenerator{}
func newSkippingPacketNumberGenerator(initial, averagePeriod protocol.PacketNumber) packetNumberGenerator {
b := make([]byte, 8)
rand.Read(b) // it's not the end of the world if we don't get perfect random here
g := &packetNumberGenerator{
g := &skippingPacketNumberGenerator{
rand: mrand.New(mrand.NewSource(int64(binary.LittleEndian.Uint64(b)))),
next: initial,
averagePeriod: averagePeriod,
@ -31,16 +58,13 @@ func newPacketNumberGenerator(initial, averagePeriod protocol.PacketNumber) *pac
return g
}
func (p *packetNumberGenerator) Peek() protocol.PacketNumber {
func (p *skippingPacketNumberGenerator) Peek() protocol.PacketNumber {
return p.next
}
func (p *packetNumberGenerator) Pop() protocol.PacketNumber {
func (p *skippingPacketNumberGenerator) Pop() protocol.PacketNumber {
next := p.next
// generate a new packet number for the next packet
p.next++
p.next++ // generate a new packet number for the next packet
if p.next == p.nextToSkip {
p.next++
p.generateNewSkip()
@ -48,7 +72,7 @@ func (p *packetNumberGenerator) Pop() protocol.PacketNumber {
return next
}
func (p *packetNumberGenerator) generateNewSkip() {
func (p *skippingPacketNumberGenerator) generateNewSkip() {
// make sure that there are never two consecutive packet numbers that are skipped
p.nextToSkip = p.next + 2 + protocol.PacketNumber(p.rand.Int31n(int32(2*p.averagePeriod)))
}