Skip to content

Commit

Permalink
impl: replace token length with token presence
Browse files Browse the repository at this point in the history
  • Loading branch information
gaukas committed Jul 27, 2023
1 parent 7cce34b commit 57a989f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
20 changes: 15 additions & 5 deletions quic_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"golang.org/x/crypto/cryptobyte"
)

const (
TOKEN_ABSENT uint32 = 0x00000000
TOKEN_PRESENT uint32 = 0x00000001
)

var (
ErrNotQUICLongHeaderFormat = errors.New("packet is not in QUIC Long Header Format")
ErrNotQUICInitialPacket = errors.New("packet is not a QUIC Initial Packet")
Expand All @@ -30,7 +35,7 @@ type QUICHeader struct {
FrameIDs utils.Uint8Arr `json:"frame_id,omitempty"` // sorted
frames []Frame

TokenLength uint32 `json:"token_len,omitempty"`
Token bool `json:"token,omitempty"`

HexID string `json:"hdrid,omitempty"`
NumericID uint64 `json:"hdrnid,omitempty"`
Expand Down Expand Up @@ -85,17 +90,18 @@ func DecodeQUICHeaderAndFrames(p []byte) (*QUICHeader, error) {
if err != nil {
return nil, err
}
qHdr.TokenLength = uint32(tokenLen)

// read token bytes
token := make([]byte, qHdr.TokenLength)
token := make([]byte, tokenLen)
n, err := r.Read(token)
if err != nil {
return nil, err
}
if n != int(tokenLen) {
return nil, errors.New("failed to read all token bytes, short read")
}
if tokenLen > 0 {
qHdr.Token = true
}

// packet length is a VLI
packetLen, _, err := ReadNextVLI(r)
Expand Down Expand Up @@ -188,7 +194,11 @@ func (qHdr *QUICHeader) NID() uint64 {
updateU32(h, qHdr.SCIDLength)
updateArr(h, qHdr.PacketNumber)
updateArr(h, qHdr.FrameIDs)
updateU32(h, qHdr.TokenLength)
if qHdr.Token {
updateU32(h, TOKEN_PRESENT)
} else {
updateU32(h, TOKEN_ABSENT)
}

qHdr.NumericID = binary.BigEndian.Uint64(h.Sum(nil)[0:8])
return qHdr.NumericID
Expand Down
8 changes: 4 additions & 4 deletions quic_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func testcaseDecodeQUICHeaderAndFrames(t *testing.T, data []byte, truth *QUICHea
t.Errorf("qHdr.FrameIDs = %v, want %v", qHdr.FrameIDs, truth.FrameIDs)
}

if qHdr.TokenLength != truth.TokenLength {
t.Errorf("qHdr.TokenLength = %d, want %d", qHdr.TokenLength, truth.TokenLength)
if qHdr.Token != truth.Token {
t.Errorf("qHdr.Token = %t, want %t", qHdr.Token, truth.Token)
}
}

Expand Down Expand Up @@ -226,7 +226,7 @@ var (
FrameIDs: []byte{
0x00, 0x01, 0x06, // sorted unique IDs
},
TokenLength: 0,
Token: false,
}

quicIETFData_MozillaFirefox = []byte{
Expand Down Expand Up @@ -411,6 +411,6 @@ var (
FrameIDs: []byte{
0x06, // sorted unique IDs
},
TokenLength: 86,
Token: true,
}
)

0 comments on commit 57a989f

Please sign in to comment.