mirror of
https://github.com/refraction-networking/uquic.git
synced 2025-04-04 12:47:36 +03:00
use a uint8 to represent the key phase bit
This commit is contained in:
parent
93e3873141
commit
d5a30225ce
4 changed files with 29 additions and 19 deletions
|
@ -3,23 +3,34 @@ package protocol
|
||||||
// KeyPhase is the key phase
|
// KeyPhase is the key phase
|
||||||
type KeyPhase uint64
|
type KeyPhase uint64
|
||||||
|
|
||||||
|
// Bit determines the key phase bit
|
||||||
func (p KeyPhase) Bit() KeyPhaseBit {
|
func (p KeyPhase) Bit() KeyPhaseBit {
|
||||||
return p%2 == 1
|
if p%2 == 0 {
|
||||||
|
return KeyPhaseZero
|
||||||
|
}
|
||||||
|
return KeyPhaseOne
|
||||||
}
|
}
|
||||||
|
|
||||||
// KeyPhaseBit is the key phase bit
|
// KeyPhaseBit is the key phase bit
|
||||||
type KeyPhaseBit bool
|
type KeyPhaseBit uint8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
// KeyPhaseUndefined is an undefined key phase
|
||||||
|
KeyPhaseUndefined KeyPhaseBit = iota
|
||||||
// KeyPhaseZero is key phase 0
|
// KeyPhaseZero is key phase 0
|
||||||
KeyPhaseZero KeyPhaseBit = false
|
KeyPhaseZero
|
||||||
// KeyPhaseOne is key phase 1
|
// KeyPhaseOne is key phase 1
|
||||||
KeyPhaseOne KeyPhaseBit = true
|
KeyPhaseOne
|
||||||
)
|
)
|
||||||
|
|
||||||
func (p KeyPhaseBit) String() string {
|
func (p KeyPhaseBit) String() string {
|
||||||
if p == KeyPhaseZero {
|
//nolint:exhaustive
|
||||||
|
switch p {
|
||||||
|
case KeyPhaseZero:
|
||||||
return "0"
|
return "0"
|
||||||
|
case KeyPhaseOne:
|
||||||
|
return "1"
|
||||||
|
default:
|
||||||
|
return "undefined"
|
||||||
}
|
}
|
||||||
return "1"
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("Key Phases", func() {
|
var _ = Describe("Key Phases", func() {
|
||||||
|
It("has undefined as its default value", func() {
|
||||||
|
var k KeyPhaseBit
|
||||||
|
Expect(k).To(Equal(KeyPhaseUndefined))
|
||||||
|
})
|
||||||
|
|
||||||
It("has the correct string representation", func() {
|
It("has the correct string representation", func() {
|
||||||
Expect(KeyPhaseZero.String()).To(Equal("0"))
|
Expect(KeyPhaseZero.String()).To(Equal("0"))
|
||||||
Expect(KeyPhaseOne.String()).To(Equal("1"))
|
Expect(KeyPhaseOne.String()).To(Equal("1"))
|
||||||
|
|
|
@ -38,7 +38,6 @@ type packetHeader struct {
|
||||||
SrcConnectionID logging.ConnectionID
|
SrcConnectionID logging.ConnectionID
|
||||||
DestConnectionID logging.ConnectionID
|
DestConnectionID logging.ConnectionID
|
||||||
|
|
||||||
hasKeyPhase bool
|
|
||||||
KeyPhaseBit logging.KeyPhaseBit
|
KeyPhaseBit logging.KeyPhaseBit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,10 +54,7 @@ func transformHeader(hdr *wire.Header) *packetHeader {
|
||||||
func transformExtendedHeader(hdr *wire.ExtendedHeader) *packetHeader {
|
func transformExtendedHeader(hdr *wire.ExtendedHeader) *packetHeader {
|
||||||
h := transformHeader(&hdr.Header)
|
h := transformHeader(&hdr.Header)
|
||||||
h.PacketNumber = hdr.PacketNumber
|
h.PacketNumber = hdr.PacketNumber
|
||||||
if !hdr.IsLongHeader {
|
h.KeyPhaseBit = hdr.KeyPhase
|
||||||
h.hasKeyPhase = true
|
|
||||||
h.KeyPhaseBit = hdr.KeyPhase
|
|
||||||
}
|
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,12 +77,7 @@ func (h packetHeader) MarshalJSONObject(enc *gojay.Encoder) {
|
||||||
if h.DestConnectionID.Len() > 0 {
|
if h.DestConnectionID.Len() > 0 {
|
||||||
enc.StringKey("dcid", connectionID(h.DestConnectionID).String())
|
enc.StringKey("dcid", connectionID(h.DestConnectionID).String())
|
||||||
}
|
}
|
||||||
if h.hasKeyPhase {
|
if h.KeyPhaseBit == logging.KeyPhaseZero || h.KeyPhaseBit == logging.KeyPhaseOne {
|
||||||
switch h.KeyPhaseBit {
|
enc.StringKey("key_phase_bit", h.KeyPhaseBit.String())
|
||||||
case logging.KeyPhaseZero:
|
|
||||||
enc.StringKey("key_phase_bit", "0")
|
|
||||||
case logging.KeyPhaseOne:
|
|
||||||
enc.StringKey("key_phase_bit", "1")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,10 @@ var _ = Describe("Packet Header", func() {
|
||||||
|
|
||||||
It("marshals a header for a 1-RTT packet", func() {
|
It("marshals a header for a 1-RTT packet", func() {
|
||||||
check(
|
check(
|
||||||
&wire.ExtendedHeader{PacketNumber: 42},
|
&wire.ExtendedHeader{
|
||||||
|
PacketNumber: 42,
|
||||||
|
KeyPhase: protocol.KeyPhaseZero,
|
||||||
|
},
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"packet_number": 42,
|
"packet_number": 42,
|
||||||
"dcil": 0,
|
"dcil": 0,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue