move parsing of the extended header to the unpacker

This commit is contained in:
Marten Seemann 2018-12-12 12:41:17 +06:30
parent c912860a94
commit aaea375fb6
5 changed files with 165 additions and 117 deletions

View file

@ -11,7 +11,8 @@ import (
)
type unpackedPacket struct {
packetNumber protocol.PacketNumber
packetNumber protocol.PacketNumber // the decoded packet number
hdr *wire.ExtendedHeader
encryptionLevel protocol.EncryptionLevel
frames []wire.Frame
}
@ -40,11 +41,30 @@ func newPacketUnpacker(aead quicAEAD, version protocol.VersionNumber) unpacker {
}
}
func (u *packetUnpacker) Unpack(hdr *wire.ExtendedHeader, data []byte) (*unpackedPacket, error) {
func (u *packetUnpacker) Unpack(hdr *wire.Header, data []byte) (*unpackedPacket, error) {
r := bytes.NewReader(data)
extHdr, err := hdr.ParseExtended(r, u.version)
if err != nil {
return nil, fmt.Errorf("error parsing extended header: %s", err)
}
extHdr.Raw = data[:len(data)-r.Len()]
data = data[len(data)-r.Len():]
if hdr.IsLongHeader {
if hdr.Length < protocol.ByteCount(extHdr.PacketNumberLen) {
return nil, fmt.Errorf("packet length (%d bytes) shorter than packet number (%d bytes)", extHdr.Length, extHdr.PacketNumberLen)
}
if protocol.ByteCount(len(data))+protocol.ByteCount(extHdr.PacketNumberLen) < extHdr.Length {
return nil, fmt.Errorf("packet length (%d bytes) is smaller than the expected length (%d bytes)", len(data)+int(extHdr.PacketNumberLen), extHdr.Length)
}
data = data[:int(extHdr.Length)-int(extHdr.PacketNumberLen)]
// TODO(#1312): implement parsing of compound packets
}
pn := protocol.DecodePacketNumber(
hdr.PacketNumberLen,
extHdr.PacketNumberLen,
u.largestRcvdPacketNumber,
hdr.PacketNumber,
extHdr.PacketNumber,
)
buf := *getPacketBuffer()
@ -53,19 +73,18 @@ func (u *packetUnpacker) Unpack(hdr *wire.ExtendedHeader, data []byte) (*unpacke
var decrypted []byte
var encryptionLevel protocol.EncryptionLevel
var err error
switch hdr.Type {
case protocol.PacketTypeInitial:
decrypted, err = u.aead.OpenInitial(buf, data, pn, hdr.Raw)
decrypted, err = u.aead.OpenInitial(buf, data, pn, extHdr.Raw)
encryptionLevel = protocol.EncryptionInitial
case protocol.PacketTypeHandshake:
decrypted, err = u.aead.OpenHandshake(buf, data, pn, hdr.Raw)
decrypted, err = u.aead.OpenHandshake(buf, data, pn, extHdr.Raw)
encryptionLevel = protocol.EncryptionHandshake
default:
if hdr.IsLongHeader {
return nil, fmt.Errorf("unknown packet type: %s", hdr.Type)
}
decrypted, err = u.aead.Open1RTT(buf, data, pn, hdr.Raw)
decrypted, err = u.aead.Open1RTT(buf, data, pn, extHdr.Raw)
encryptionLevel = protocol.Encryption1RTT
}
if err != nil {
@ -81,6 +100,7 @@ func (u *packetUnpacker) Unpack(hdr *wire.ExtendedHeader, data []byte) (*unpacke
}
return &unpackedPacket{
hdr: extHdr,
packetNumber: pn,
encryptionLevel: encryptionLevel,
frames: fs,