route/vendor/github.com/lucas-clemente/quic-go/internal/protocol/packet_number.go

53 lines
1.7 KiB
Go
Raw Normal View History

2017-12-12 02:51:45 +00:00
package protocol
// InferPacketNumber calculates the packet number based on the received packet number, its length and the last seen packet number
func InferPacketNumber(packetNumberLength PacketNumberLen, lastPacketNumber PacketNumber, wirePacketNumber PacketNumber) PacketNumber {
epochDelta := PacketNumber(1) << (uint8(packetNumberLength) * 8)
epoch := lastPacketNumber & ^(epochDelta - 1)
prevEpochBegin := epoch - epochDelta
nextEpochBegin := epoch + epochDelta
return closestTo(
lastPacketNumber+1,
epoch+wirePacketNumber,
closestTo(lastPacketNumber+1, prevEpochBegin+wirePacketNumber, nextEpochBegin+wirePacketNumber),
)
}
func closestTo(target, a, b PacketNumber) PacketNumber {
if delta(target, a) < delta(target, b) {
return a
}
return b
}
func delta(a, b PacketNumber) PacketNumber {
if a < b {
return b - a
}
return a - b
}
2018-01-03 19:19:49 +00:00
// GetPacketNumberLengthForHeader gets the length of the packet number for the public header
2017-12-12 02:51:45 +00:00
// it never chooses a PacketNumberLen of 1 byte, since this is too short under certain circumstances
2018-01-03 19:19:49 +00:00
func GetPacketNumberLengthForHeader(packetNumber PacketNumber, leastUnacked PacketNumber) PacketNumberLen {
2017-12-12 02:51:45 +00:00
diff := uint64(packetNumber - leastUnacked)
2018-01-03 19:19:49 +00:00
if diff < (1 << (uint8(PacketNumberLen2)*8 - 1)) {
2017-12-12 02:51:45 +00:00
return PacketNumberLen2
}
2018-01-03 19:19:49 +00:00
return PacketNumberLen4
2017-12-12 02:51:45 +00:00
}
// GetPacketNumberLength gets the minimum length needed to fully represent the packet number
func GetPacketNumberLength(packetNumber PacketNumber) PacketNumberLen {
if packetNumber < (1 << (uint8(PacketNumberLen1) * 8)) {
return PacketNumberLen1
}
if packetNumber < (1 << (uint8(PacketNumberLen2) * 8)) {
return PacketNumberLen2
}
if packetNumber < (1 << (uint8(PacketNumberLen4) * 8)) {
return PacketNumberLen4
}
return PacketNumberLen6
}