rename the wire.InvariantHeader to Header

This commit is contained in:
Marten Seemann 2018-11-20 17:17:43 +07:00
parent bf96707f48
commit 7b91ba56b6
8 changed files with 178 additions and 178 deletions

View file

@ -326,11 +326,11 @@ var _ = Describe("Header", func() {
srcConnID := protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad, 0x013, 0x37, 0x13, 0x37} srcConnID := protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad, 0x013, 0x37, 0x13, 0x37}
data, err := ComposeVersionNegotiation(destConnID, srcConnID, []protocol.VersionNumber{0x12345678, 0x87654321}) data, err := ComposeVersionNegotiation(destConnID, srcConnID, []protocol.VersionNumber{0x12345678, 0x87654321})
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
iHdr, err := ParseInvariantHeader(bytes.NewReader(data), 4) hdr, err := ParseHeader(bytes.NewReader(data), 4)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
hdr, err := iHdr.Parse(bytes.NewReader(data), versionIETFHeader) extHdr, err := hdr.Parse(bytes.NewReader(data), versionIETFHeader)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
hdr.Log(logger) extHdr.Log(logger)
Expect(buf.String()).To(ContainSubstring("VersionNegotiationPacket{DestConnectionID: 0xdeadbeefcafe1337, SrcConnectionID: 0xdecafbad13371337")) Expect(buf.String()).To(ContainSubstring("VersionNegotiationPacket{DestConnectionID: 0xdeadbeefcafe1337, SrcConnectionID: 0xdecafbad13371337"))
Expect(buf.String()).To(ContainSubstring("0x12345678")) Expect(buf.String()).To(ContainSubstring("0x12345678"))
Expect(buf.String()).To(ContainSubstring("0x87654321")) Expect(buf.String()).To(ContainSubstring("0x87654321"))

View file

@ -10,8 +10,8 @@ import (
"github.com/lucas-clemente/quic-go/internal/utils" "github.com/lucas-clemente/quic-go/internal/utils"
) )
// The InvariantHeader is the version independent part of the header // The Header is the version independent part of the header
type InvariantHeader struct { type Header struct {
IsLongHeader bool IsLongHeader bool
Version protocol.VersionNumber Version protocol.VersionNumber
SrcConnectionID protocol.ConnectionID SrcConnectionID protocol.ConnectionID
@ -21,10 +21,10 @@ type InvariantHeader struct {
len int // how many bytes were read while parsing this header len int // how many bytes were read while parsing this header
} }
// ParseInvariantHeader parses the version independent part of the header // ParseHeader parses the version independent part of the header
func ParseInvariantHeader(b *bytes.Reader, shortHeaderConnIDLen int) (*InvariantHeader, error) { func ParseHeader(b *bytes.Reader, shortHeaderConnIDLen int) (*Header, error) {
startLen := b.Len() startLen := b.Len()
h, err := parseInvariantHeaderImpl(b, shortHeaderConnIDLen) h, err := parseHeaderImpl(b, shortHeaderConnIDLen)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -32,13 +32,13 @@ func ParseInvariantHeader(b *bytes.Reader, shortHeaderConnIDLen int) (*Invariant
return h, nil return h, nil
} }
func parseInvariantHeaderImpl(b *bytes.Reader, shortHeaderConnIDLen int) (*InvariantHeader, error) { func parseHeaderImpl(b *bytes.Reader, shortHeaderConnIDLen int) (*Header, error) {
typeByte, err := b.ReadByte() typeByte, err := b.ReadByte()
if err != nil { if err != nil {
return nil, err return nil, err
} }
h := &InvariantHeader{typeByte: typeByte} h := &Header{typeByte: typeByte}
h.IsLongHeader = typeByte&0x80 > 0 h.IsLongHeader = typeByte&0x80 > 0
// If this is not a Long Header, it could either be a Public Header or a Short Header. // If this is not a Long Header, it could either be a Public Header or a Short Header.
@ -74,71 +74,71 @@ func parseInvariantHeaderImpl(b *bytes.Reader, shortHeaderConnIDLen int) (*Invar
// Parse parses the version dependent part of the header. // Parse parses the version dependent part of the header.
// The Reader has to be set such that it points to the first byte of the header. // The Reader has to be set such that it points to the first byte of the header.
func (iv *InvariantHeader) Parse(b *bytes.Reader, ver protocol.VersionNumber) (*ExtendedHeader, error) { func (h *Header) Parse(b *bytes.Reader, ver protocol.VersionNumber) (*ExtendedHeader, error) {
if _, err := b.Seek(int64(iv.len), io.SeekCurrent); err != nil { if _, err := b.Seek(int64(h.len), io.SeekCurrent); err != nil {
return nil, err return nil, err
} }
if iv.IsLongHeader { if h.IsLongHeader {
if iv.Version == 0 { // Version Negotiation Packet if h.Version == 0 { // Version Negotiation Packet
return iv.parseVersionNegotiationPacket(b) return h.parseVersionNegotiationPacket(b)
} }
return iv.parseLongHeader(b, ver) return h.parseLongHeader(b, ver)
} }
return iv.parseShortHeader(b, ver) return h.parseShortHeader(b, ver)
} }
func (iv *InvariantHeader) toExtendedHeader() *ExtendedHeader { func (h *Header) toExtendedHeader() *ExtendedHeader {
return &ExtendedHeader{ return &ExtendedHeader{
IsLongHeader: iv.IsLongHeader, IsLongHeader: h.IsLongHeader,
DestConnectionID: iv.DestConnectionID, DestConnectionID: h.DestConnectionID,
SrcConnectionID: iv.SrcConnectionID, SrcConnectionID: h.SrcConnectionID,
Version: iv.Version, Version: h.Version,
} }
} }
func (iv *InvariantHeader) parseVersionNegotiationPacket(b *bytes.Reader) (*ExtendedHeader, error) { func (h *Header) parseVersionNegotiationPacket(b *bytes.Reader) (*ExtendedHeader, error) {
h := iv.toExtendedHeader() eh := h.toExtendedHeader()
if b.Len() == 0 { if b.Len() == 0 {
return nil, qerr.Error(qerr.InvalidVersionNegotiationPacket, "empty version list") return nil, qerr.Error(qerr.InvalidVersionNegotiationPacket, "empty version list")
} }
h.IsVersionNegotiation = true eh.IsVersionNegotiation = true
h.SupportedVersions = make([]protocol.VersionNumber, b.Len()/4) eh.SupportedVersions = make([]protocol.VersionNumber, b.Len()/4)
for i := 0; b.Len() > 0; i++ { for i := 0; b.Len() > 0; i++ {
v, err := utils.BigEndian.ReadUint32(b) v, err := utils.BigEndian.ReadUint32(b)
if err != nil { if err != nil {
return nil, qerr.InvalidVersionNegotiationPacket return nil, qerr.InvalidVersionNegotiationPacket
} }
h.SupportedVersions[i] = protocol.VersionNumber(v) eh.SupportedVersions[i] = protocol.VersionNumber(v)
} }
return h, nil return eh, nil
} }
func (iv *InvariantHeader) parseLongHeader(b *bytes.Reader, v protocol.VersionNumber) (*ExtendedHeader, error) { func (h *Header) parseLongHeader(b *bytes.Reader, v protocol.VersionNumber) (*ExtendedHeader, error) {
h := iv.toExtendedHeader() eh := h.toExtendedHeader()
h.Type = protocol.PacketType(iv.typeByte & 0x7f) eh.Type = protocol.PacketType(h.typeByte & 0x7f)
if h.Type != protocol.PacketTypeInitial && h.Type != protocol.PacketTypeRetry && h.Type != protocol.PacketType0RTT && h.Type != protocol.PacketTypeHandshake { if eh.Type != protocol.PacketTypeInitial && eh.Type != protocol.PacketTypeRetry && eh.Type != protocol.PacketType0RTT && eh.Type != protocol.PacketTypeHandshake {
return nil, qerr.Error(qerr.InvalidPacketHeader, fmt.Sprintf("Received packet with invalid packet type: %d", h.Type)) return nil, qerr.Error(qerr.InvalidPacketHeader, fmt.Sprintf("Received packet with invalid packet type: %d", eh.Type))
} }
if h.Type == protocol.PacketTypeRetry { if eh.Type == protocol.PacketTypeRetry {
odcilByte, err := b.ReadByte() odcilByte, err := b.ReadByte()
if err != nil { if err != nil {
return nil, err return nil, err
} }
odcil := decodeSingleConnIDLen(odcilByte & 0xf) odcil := decodeSingleConnIDLen(odcilByte & 0xf)
h.OrigDestConnectionID, err = protocol.ReadConnectionID(b, odcil) eh.OrigDestConnectionID, err = protocol.ReadConnectionID(b, odcil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
h.Token = make([]byte, b.Len()) eh.Token = make([]byte, b.Len())
if _, err := io.ReadFull(b, h.Token); err != nil { if _, err := io.ReadFull(b, eh.Token); err != nil {
return nil, err return nil, err
} }
return h, nil return eh, nil
} }
if h.Type == protocol.PacketTypeInitial { if eh.Type == protocol.PacketTypeInitial {
tokenLen, err := utils.ReadVarInt(b) tokenLen, err := utils.ReadVarInt(b)
if err != nil { if err != nil {
return nil, err return nil, err
@ -146,8 +146,8 @@ func (iv *InvariantHeader) parseLongHeader(b *bytes.Reader, v protocol.VersionNu
if tokenLen > uint64(b.Len()) { if tokenLen > uint64(b.Len()) {
return nil, io.EOF return nil, io.EOF
} }
h.Token = make([]byte, tokenLen) eh.Token = make([]byte, tokenLen)
if _, err := io.ReadFull(b, h.Token); err != nil { if _, err := io.ReadFull(b, eh.Token); err != nil {
return nil, err return nil, err
} }
} }
@ -156,27 +156,27 @@ func (iv *InvariantHeader) parseLongHeader(b *bytes.Reader, v protocol.VersionNu
if err != nil { if err != nil {
return nil, err return nil, err
} }
h.Length = protocol.ByteCount(pl) eh.Length = protocol.ByteCount(pl)
pn, pnLen, err := utils.ReadVarIntPacketNumber(b) pn, pnLen, err := utils.ReadVarIntPacketNumber(b)
if err != nil { if err != nil {
return nil, err return nil, err
} }
h.PacketNumber = pn eh.PacketNumber = pn
h.PacketNumberLen = pnLen eh.PacketNumberLen = pnLen
return h, nil return eh, nil
} }
func (iv *InvariantHeader) parseShortHeader(b *bytes.Reader, v protocol.VersionNumber) (*ExtendedHeader, error) { func (h *Header) parseShortHeader(b *bytes.Reader, v protocol.VersionNumber) (*ExtendedHeader, error) {
h := iv.toExtendedHeader() eh := h.toExtendedHeader()
h.KeyPhase = int(iv.typeByte&0x40) >> 6 eh.KeyPhase = int(h.typeByte&0x40) >> 6
pn, pnLen, err := utils.ReadVarIntPacketNumber(b) pn, pnLen, err := utils.ReadVarIntPacketNumber(b)
if err != nil { if err != nil {
return nil, err return nil, err
} }
h.PacketNumber = pn eh.PacketNumber = pn
h.PacketNumberLen = pnLen eh.PacketNumberLen = pnLen
return h, nil return eh, nil
} }

View file

@ -25,20 +25,20 @@ var _ = Describe("Header Parsing", func() {
versions := []protocol.VersionNumber{0x22334455, 0x33445566} versions := []protocol.VersionNumber{0x22334455, 0x33445566}
data, err := ComposeVersionNegotiation(destConnID, srcConnID, versions) data, err := ComposeVersionNegotiation(destConnID, srcConnID, versions)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
iHdr, err := ParseInvariantHeader(bytes.NewReader(data), 0) hdr, err := ParseHeader(bytes.NewReader(data), 0)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(iHdr.DestConnectionID).To(Equal(destConnID))
Expect(iHdr.SrcConnectionID).To(Equal(srcConnID))
Expect(iHdr.IsLongHeader).To(BeTrue())
b := bytes.NewReader(data)
hdr, err := iHdr.Parse(b, versionIETFFrames)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.IsVersionNegotiation).To(BeTrue())
Expect(hdr.Version).To(BeZero())
Expect(hdr.DestConnectionID).To(Equal(destConnID)) Expect(hdr.DestConnectionID).To(Equal(destConnID))
Expect(hdr.SrcConnectionID).To(Equal(srcConnID)) Expect(hdr.SrcConnectionID).To(Equal(srcConnID))
Expect(hdr.IsLongHeader).To(BeTrue())
b := bytes.NewReader(data)
extHdr, err := hdr.Parse(b, versionIETFFrames)
Expect(err).ToNot(HaveOccurred())
Expect(extHdr.IsVersionNegotiation).To(BeTrue())
Expect(extHdr.Version).To(BeZero())
Expect(extHdr.DestConnectionID).To(Equal(destConnID))
Expect(extHdr.SrcConnectionID).To(Equal(srcConnID))
for _, v := range versions { for _, v := range versions {
Expect(hdr.SupportedVersions).To(ContainElement(v)) Expect(extHdr.SupportedVersions).To(ContainElement(v))
} }
Expect(b.Len()).To(BeZero()) Expect(b.Len()).To(BeZero())
}) })
@ -49,9 +49,9 @@ var _ = Describe("Header Parsing", func() {
data, err := ComposeVersionNegotiation(connID, connID, versions) data, err := ComposeVersionNegotiation(connID, connID, versions)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
data = data[:len(data)-2] data = data[:len(data)-2]
iHdr, err := ParseInvariantHeader(bytes.NewReader(data), 0) hdr, err := ParseHeader(bytes.NewReader(data), 0)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
_, err = iHdr.Parse(bytes.NewReader(data), versionIETFFrames) _, err = hdr.Parse(bytes.NewReader(data), versionIETFFrames)
Expect(err).To(MatchError(qerr.InvalidVersionNegotiationPacket)) Expect(err).To(MatchError(qerr.InvalidVersionNegotiationPacket))
}) })
@ -62,9 +62,9 @@ var _ = Describe("Header Parsing", func() {
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
// remove 8 bytes (two versions), since ComposeVersionNegotiation also added a reserved version number // remove 8 bytes (two versions), since ComposeVersionNegotiation also added a reserved version number
data = data[:len(data)-8] data = data[:len(data)-8]
iHdr, err := ParseInvariantHeader(bytes.NewReader(data), 0) hdr, err := ParseHeader(bytes.NewReader(data), 0)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
_, err = iHdr.Parse(bytes.NewReader(data), versionIETFFrames) _, err = hdr.Parse(bytes.NewReader(data), versionIETFFrames)
Expect(err).To(MatchError("InvalidVersionNegotiationPacket: empty version list")) Expect(err).To(MatchError("InvalidVersionNegotiationPacket: empty version list"))
}) })
}) })
@ -86,24 +86,24 @@ var _ = Describe("Header Parsing", func() {
// packet number // packet number
data = appendPacketNumber(data, 0xbeef, protocol.PacketNumberLen4) data = appendPacketNumber(data, 0xbeef, protocol.PacketNumberLen4)
iHdr, err := ParseInvariantHeader(bytes.NewReader(data), 0) hdr, err := ParseHeader(bytes.NewReader(data), 0)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(iHdr.IsLongHeader).To(BeTrue())
Expect(iHdr.DestConnectionID).To(Equal(destConnID))
Expect(iHdr.SrcConnectionID).To(Equal(srcConnID))
b := bytes.NewReader(data)
hdr, err := iHdr.Parse(b, versionIETFFrames)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.Type).To(Equal(protocol.PacketTypeInitial))
Expect(hdr.IsLongHeader).To(BeTrue()) Expect(hdr.IsLongHeader).To(BeTrue())
Expect(hdr.DestConnectionID).To(Equal(destConnID)) Expect(hdr.DestConnectionID).To(Equal(destConnID))
Expect(hdr.SrcConnectionID).To(Equal(srcConnID)) Expect(hdr.SrcConnectionID).To(Equal(srcConnID))
Expect(hdr.Token).To(Equal([]byte("foobar"))) b := bytes.NewReader(data)
Expect(hdr.Length).To(Equal(protocol.ByteCount(0x1337))) extHdr, err := hdr.Parse(b, versionIETFFrames)
Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0xbeef))) Expect(err).ToNot(HaveOccurred())
Expect(hdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen4)) Expect(extHdr.Type).To(Equal(protocol.PacketTypeInitial))
Expect(hdr.Version).To(Equal(protocol.VersionNumber(0x1020304))) Expect(extHdr.IsLongHeader).To(BeTrue())
Expect(hdr.IsVersionNegotiation).To(BeFalse()) Expect(extHdr.DestConnectionID).To(Equal(destConnID))
Expect(extHdr.SrcConnectionID).To(Equal(srcConnID))
Expect(extHdr.Token).To(Equal([]byte("foobar")))
Expect(extHdr.Length).To(Equal(protocol.ByteCount(0x1337)))
Expect(extHdr.PacketNumber).To(Equal(protocol.PacketNumber(0xbeef)))
Expect(extHdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen4))
Expect(extHdr.Version).To(Equal(protocol.VersionNumber(0x1020304)))
Expect(extHdr.IsVersionNegotiation).To(BeFalse())
Expect(b.Len()).To(BeZero()) Expect(b.Len()).To(BeZero())
}) })
@ -116,10 +116,10 @@ var _ = Describe("Header Parsing", func() {
} }
data = append(data, encodeVarInt(0x42)...) // length data = append(data, encodeVarInt(0x42)...) // length
data = append(data, []byte{0xde, 0xca, 0xfb, 0xad}...) data = append(data, []byte{0xde, 0xca, 0xfb, 0xad}...)
iHdr, err := ParseInvariantHeader(bytes.NewReader(data), 0) hdr, err := ParseHeader(bytes.NewReader(data), 0)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(iHdr.SrcConnectionID).To(Equal(protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef})) Expect(hdr.SrcConnectionID).To(Equal(protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef}))
Expect(iHdr.DestConnectionID).To(BeEmpty()) Expect(hdr.DestConnectionID).To(BeEmpty())
}) })
It("parses a Long Header without a source connection ID", func() { It("parses a Long Header without a source connection ID", func() {
@ -131,10 +131,10 @@ var _ = Describe("Header Parsing", func() {
} }
data = append(data, encodeVarInt(0x42)...) // length data = append(data, encodeVarInt(0x42)...) // length
data = append(data, []byte{0xde, 0xca, 0xfb, 0xad}...) data = append(data, []byte{0xde, 0xca, 0xfb, 0xad}...)
iHdr, err := ParseInvariantHeader(bytes.NewReader(data), 0) hdr, err := ParseHeader(bytes.NewReader(data), 0)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(iHdr.SrcConnectionID).To(BeEmpty()) Expect(hdr.SrcConnectionID).To(BeEmpty())
Expect(iHdr.DestConnectionID).To(Equal(protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})) Expect(hdr.DestConnectionID).To(Equal(protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}))
}) })
It("parses a Long Header with a 2 byte packet number", func() { It("parses a Long Header with a 2 byte packet number", func() {
@ -147,13 +147,13 @@ var _ = Describe("Header Parsing", func() {
data = append(data, encodeVarInt(0x42)...) // length data = append(data, encodeVarInt(0x42)...) // length
data = appendPacketNumber(data, 0x123, protocol.PacketNumberLen2) data = appendPacketNumber(data, 0x123, protocol.PacketNumberLen2)
iHdr, err := ParseInvariantHeader(bytes.NewReader(data), 0) hdr, err := ParseHeader(bytes.NewReader(data), 0)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data) b := bytes.NewReader(data)
hdr, err := iHdr.Parse(b, versionIETFFrames) extHdr, err := hdr.Parse(b, versionIETFFrames)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0x123))) Expect(extHdr.PacketNumber).To(Equal(protocol.PacketNumber(0x123)))
Expect(hdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen2)) Expect(extHdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen2))
Expect(b.Len()).To(BeZero()) Expect(b.Len()).To(BeZero())
}) })
@ -166,14 +166,14 @@ var _ = Describe("Header Parsing", func() {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, // source connection ID 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, // source connection ID
'f', 'o', 'o', 'b', 'a', 'r', // token 'f', 'o', 'o', 'b', 'a', 'r', // token
} }
iHdr, err := ParseInvariantHeader(bytes.NewReader(data), 0) hdr, err := ParseHeader(bytes.NewReader(data), 0)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data) b := bytes.NewReader(data)
hdr, err := iHdr.Parse(b, versionIETFFrames) extHdr, err := hdr.Parse(b, versionIETFFrames)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(hdr.Type).To(Equal(protocol.PacketTypeRetry)) Expect(extHdr.Type).To(Equal(protocol.PacketTypeRetry))
Expect(hdr.OrigDestConnectionID).To(Equal(protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})) Expect(extHdr.OrigDestConnectionID).To(Equal(protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}))
Expect(hdr.Token).To(Equal([]byte("foobar"))) Expect(extHdr.Token).To(Equal([]byte("foobar")))
Expect(b.Len()).To(BeZero()) Expect(b.Len()).To(BeZero())
}) })
@ -189,9 +189,9 @@ var _ = Describe("Header Parsing", func() {
PacketNumberLen: protocol.PacketNumberLen1, PacketNumberLen: protocol.PacketNumberLen1,
}).Write(buf, protocol.VersionTLS)).To(Succeed()) }).Write(buf, protocol.VersionTLS)).To(Succeed())
b := bytes.NewReader(buf.Bytes()) b := bytes.NewReader(buf.Bytes())
iHdr, err := ParseInvariantHeader(b, 0) hdr, err := ParseHeader(b, 0)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
_, err = iHdr.Parse(b, versionIETFFrames) _, err = hdr.Parse(b, versionIETFFrames)
Expect(err).To(MatchError("InvalidPacketHeader: Received packet with invalid packet type: 42")) Expect(err).To(MatchError("InvalidPacketHeader: Received packet with invalid packet type: 42"))
}) })
@ -206,13 +206,13 @@ var _ = Describe("Header Parsing", func() {
data = appendPacketNumber(data, 0x123, protocol.PacketNumberLen2) // 2 bytes data = appendPacketNumber(data, 0x123, protocol.PacketNumberLen2) // 2 bytes
b := bytes.NewReader(data) b := bytes.NewReader(data)
iHdr, err := ParseInvariantHeader(b, 0) hdr, err := ParseHeader(b, 0)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
_, err = iHdr.Parse(b, versionIETFFrames) _, err = hdr.Parse(b, versionIETFFrames)
Expect(err).To(MatchError(io.EOF)) Expect(err).To(MatchError(io.EOF))
}) })
It("errors on EOF, when parsing the invariant header", func() { It("errors on EOF, when parsing the header", func() {
data := []byte{ data := []byte{
0x80 ^ uint8(protocol.PacketTypeInitial), 0x80 ^ uint8(protocol.PacketTypeInitial),
0x1, 0x2, 0x3, 0x4, // version number 0x1, 0x2, 0x3, 0x4, // version number
@ -221,7 +221,7 @@ var _ = Describe("Header Parsing", func() {
0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37, // source connection ID 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37, // source connection ID
} }
for i := 0; i < len(data); i++ { for i := 0; i < len(data); i++ {
_, err := ParseInvariantHeader(bytes.NewReader(data[:i]), 0) _, err := ParseHeader(bytes.NewReader(data[:i]), 0)
Expect(err).To(Equal(io.EOF)) Expect(err).To(Equal(io.EOF))
} }
}) })
@ -232,14 +232,14 @@ var _ = Describe("Header Parsing", func() {
0x1, 0x2, 0x3, 0x4, // version number 0x1, 0x2, 0x3, 0x4, // version number
0x0, // connection ID lengths 0x0, // connection ID lengths
} }
iHdrLen := len(data) hdrLen := len(data)
data = append(data, encodeVarInt(0x1337)...) data = append(data, encodeVarInt(0x1337)...)
data = appendPacketNumber(data, 0xdeadbeef, protocol.PacketNumberLen4) data = appendPacketNumber(data, 0xdeadbeef, protocol.PacketNumberLen4)
for i := iHdrLen; i < len(data); i++ { for i := hdrLen; i < len(data); i++ {
b := bytes.NewReader(data[:i]) b := bytes.NewReader(data[:i])
iHdr, err := ParseInvariantHeader(b, 0) hdr, err := ParseHeader(b, 0)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
_, err = iHdr.Parse(b, versionIETFFrames) _, err = hdr.Parse(b, versionIETFFrames)
Expect(err).To(Equal(io.EOF)) Expect(err).To(Equal(io.EOF))
} }
}) })
@ -250,16 +250,16 @@ var _ = Describe("Header Parsing", func() {
0x1, 0x2, 0x3, 0x4, // version number 0x1, 0x2, 0x3, 0x4, // version number
0x0, // connection ID lengths 0x0, // connection ID lengths
} }
iHdrLen := len(data) hdrLen := len(data)
data = append(data, []byte{ data = append(data, []byte{
0x97, // Orig Destination Connection ID length 0x97, // Orig Destination Connection ID length
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, // source connection ID 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, // source connection ID
}...) }...)
for i := iHdrLen; i < len(data); i++ { for i := hdrLen; i < len(data); i++ {
b := bytes.NewReader(data[:i]) b := bytes.NewReader(data[:i])
iHdr, err := ParseInvariantHeader(b, 0) hdr, err := ParseHeader(b, 0)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
_, err = iHdr.Parse(b, versionIETFFrames) _, err = hdr.Parse(b, versionIETFFrames)
Expect(err).To(Equal(io.EOF)) Expect(err).To(Equal(io.EOF))
} }
}) })
@ -270,18 +270,18 @@ var _ = Describe("Header Parsing", func() {
connID := protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37} connID := protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}
data := append([]byte{0x30}, connID...) data := append([]byte{0x30}, connID...)
data = appendPacketNumber(data, 0x42, protocol.PacketNumberLen1) data = appendPacketNumber(data, 0x42, protocol.PacketNumberLen1)
iHdr, err := ParseInvariantHeader(bytes.NewReader(data), 8) hdr, err := ParseHeader(bytes.NewReader(data), 8)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(iHdr.IsLongHeader).To(BeFalse()) Expect(hdr.IsLongHeader).To(BeFalse())
Expect(iHdr.DestConnectionID).To(Equal(connID))
b := bytes.NewReader(data)
hdr, err := iHdr.Parse(b, versionIETFFrames)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.KeyPhase).To(Equal(0))
Expect(hdr.DestConnectionID).To(Equal(connID)) Expect(hdr.DestConnectionID).To(Equal(connID))
Expect(hdr.SrcConnectionID).To(BeEmpty()) b := bytes.NewReader(data)
Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0x42))) extHdr, err := hdr.Parse(b, versionIETFFrames)
Expect(hdr.IsVersionNegotiation).To(BeFalse()) Expect(err).ToNot(HaveOccurred())
Expect(extHdr.KeyPhase).To(Equal(0))
Expect(extHdr.DestConnectionID).To(Equal(connID))
Expect(extHdr.SrcConnectionID).To(BeEmpty())
Expect(extHdr.PacketNumber).To(Equal(protocol.PacketNumber(0x42)))
Expect(extHdr.IsVersionNegotiation).To(BeFalse())
Expect(b.Len()).To(BeZero()) Expect(b.Len()).To(BeZero())
}) })
@ -289,16 +289,16 @@ var _ = Describe("Header Parsing", func() {
connID := protocol.ConnectionID{1, 2, 3, 4, 5} connID := protocol.ConnectionID{1, 2, 3, 4, 5}
data := append([]byte{0x30}, connID...) data := append([]byte{0x30}, connID...)
data = appendPacketNumber(data, 0x42, protocol.PacketNumberLen1) data = appendPacketNumber(data, 0x42, protocol.PacketNumberLen1)
iHdr, err := ParseInvariantHeader(bytes.NewReader(data), 5) hdr, err := ParseHeader(bytes.NewReader(data), 5)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(iHdr.IsLongHeader).To(BeFalse()) Expect(hdr.IsLongHeader).To(BeFalse())
Expect(iHdr.DestConnectionID).To(Equal(connID))
b := bytes.NewReader(data)
hdr, err := iHdr.Parse(b, versionIETFFrames)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.KeyPhase).To(Equal(0))
Expect(hdr.DestConnectionID).To(Equal(connID)) Expect(hdr.DestConnectionID).To(Equal(connID))
Expect(hdr.SrcConnectionID).To(BeEmpty()) b := bytes.NewReader(data)
extHdr, err := hdr.Parse(b, versionIETFFrames)
Expect(err).ToNot(HaveOccurred())
Expect(extHdr.KeyPhase).To(Equal(0))
Expect(extHdr.DestConnectionID).To(Equal(connID))
Expect(extHdr.SrcConnectionID).To(BeEmpty())
Expect(b.Len()).To(BeZero()) Expect(b.Len()).To(BeZero())
}) })
@ -308,13 +308,13 @@ var _ = Describe("Header Parsing", func() {
0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, // connection ID 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, // connection ID
} }
data = appendPacketNumber(data, 11, protocol.PacketNumberLen1) data = appendPacketNumber(data, 11, protocol.PacketNumberLen1)
iHdr, err := ParseInvariantHeader(bytes.NewReader(data), 6) hdr, err := ParseHeader(bytes.NewReader(data), 6)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data) b := bytes.NewReader(data)
hdr, err := iHdr.Parse(b, versionIETFFrames) extHdr, err := hdr.Parse(b, versionIETFFrames)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(hdr.IsLongHeader).To(BeFalse()) Expect(extHdr.IsLongHeader).To(BeFalse())
Expect(hdr.KeyPhase).To(Equal(1)) Expect(extHdr.KeyPhase).To(Equal(1))
Expect(b.Len()).To(BeZero()) Expect(b.Len()).To(BeZero())
}) })
@ -324,14 +324,14 @@ var _ = Describe("Header Parsing", func() {
0xde, 0xad, 0xbe, 0xef, // connection ID 0xde, 0xad, 0xbe, 0xef, // connection ID
} }
data = appendPacketNumber(data, 0x1337, protocol.PacketNumberLen2) data = appendPacketNumber(data, 0x1337, protocol.PacketNumberLen2)
iHdr, err := ParseInvariantHeader(bytes.NewReader(data), 4) hdr, err := ParseHeader(bytes.NewReader(data), 4)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data) b := bytes.NewReader(data)
hdr, err := iHdr.Parse(b, versionIETFFrames) extHdr, err := hdr.Parse(b, versionIETFFrames)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(hdr.IsLongHeader).To(BeFalse()) Expect(extHdr.IsLongHeader).To(BeFalse())
Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0x1337))) Expect(extHdr.PacketNumber).To(Equal(protocol.PacketNumber(0x1337)))
Expect(hdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen2)) Expect(extHdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen2))
Expect(b.Len()).To(BeZero()) Expect(b.Len()).To(BeZero())
}) })
@ -341,24 +341,24 @@ var _ = Describe("Header Parsing", func() {
0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x1, 0x2, 0x3, 0x4, // connection ID 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x1, 0x2, 0x3, 0x4, // connection ID
} }
data = appendPacketNumber(data, 0x99beef, protocol.PacketNumberLen4) data = appendPacketNumber(data, 0x99beef, protocol.PacketNumberLen4)
iHdr, err := ParseInvariantHeader(bytes.NewReader(data), 10) hdr, err := ParseHeader(bytes.NewReader(data), 10)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data) b := bytes.NewReader(data)
hdr, err := iHdr.Parse(b, versionIETFFrames) extHdr, err := hdr.Parse(b, versionIETFFrames)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(hdr.IsLongHeader).To(BeFalse()) Expect(extHdr.IsLongHeader).To(BeFalse())
Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0x99beef))) Expect(extHdr.PacketNumber).To(Equal(protocol.PacketNumber(0x99beef)))
Expect(hdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen4)) Expect(extHdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen4))
Expect(b.Len()).To(BeZero()) Expect(b.Len()).To(BeZero())
}) })
It("errors on EOF, when parsing the invariant header", func() { It("errors on EOF, when parsing the header", func() {
data := []byte{ data := []byte{
0x30 ^ 0x2, 0x30 ^ 0x2,
0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37, // connection ID 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37, // connection ID
} }
for i := 0; i < len(data); i++ { for i := 0; i < len(data); i++ {
_, err := ParseInvariantHeader(bytes.NewReader(data[:i]), 8) _, err := ParseHeader(bytes.NewReader(data[:i]), 8)
Expect(err).To(Equal(io.EOF)) Expect(err).To(Equal(io.EOF))
} }
}) })
@ -368,13 +368,13 @@ var _ = Describe("Header Parsing", func() {
0x30 ^ 0x2, 0x30 ^ 0x2,
0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, // connection ID 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, // connection ID
} }
iHdrLen := len(data) hdrLen := len(data)
data = appendPacketNumber(data, 0xdeadbeef, protocol.PacketNumberLen4) data = appendPacketNumber(data, 0xdeadbeef, protocol.PacketNumberLen4)
for i := iHdrLen; i < len(data); i++ { for i := hdrLen; i < len(data); i++ {
b := bytes.NewReader(data[:i]) b := bytes.NewReader(data[:i])
iHdr, err := ParseInvariantHeader(b, 6) hdr, err := ParseHeader(b, 6)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
_, err = iHdr.Parse(b, versionIETFFrames) _, err = hdr.Parse(b, versionIETFFrames)
Expect(err).To(Equal(io.EOF)) Expect(err).To(Equal(io.EOF))
} }
}) })

View file

@ -16,19 +16,19 @@ var _ = Describe("Version Negotiation Packets", func() {
data, err := ComposeVersionNegotiation(destConnID, srcConnID, versions) data, err := ComposeVersionNegotiation(destConnID, srcConnID, versions)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(data[0] & 0x80).ToNot(BeZero()) Expect(data[0] & 0x80).ToNot(BeZero())
iHdr, err := ParseInvariantHeader(bytes.NewReader(data), 4) hdr, err := ParseHeader(bytes.NewReader(data), 4)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data) b := bytes.NewReader(data)
hdr, err := iHdr.Parse(b, versionIETFFrames) extHdr, err := hdr.Parse(b, versionIETFFrames)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(hdr.IsVersionNegotiation).To(BeTrue()) Expect(extHdr.IsVersionNegotiation).To(BeTrue())
Expect(hdr.DestConnectionID).To(Equal(destConnID)) Expect(extHdr.DestConnectionID).To(Equal(destConnID))
Expect(hdr.SrcConnectionID).To(Equal(srcConnID)) Expect(extHdr.SrcConnectionID).To(Equal(srcConnID))
Expect(hdr.Version).To(BeZero()) Expect(extHdr.Version).To(BeZero())
// the supported versions should include one reserved version number // the supported versions should include one reserved version number
Expect(hdr.SupportedVersions).To(HaveLen(len(versions) + 1)) Expect(extHdr.SupportedVersions).To(HaveLen(len(versions) + 1))
for _, version := range versions { for _, version := range versions {
Expect(hdr.SupportedVersions).To(ContainElement(version)) Expect(extHdr.SupportedVersions).To(ContainElement(version))
} }
Expect(b.Len()).To(BeZero()) Expect(b.Len()).To(BeZero())
}) })

View file

@ -165,14 +165,14 @@ func (h *packetHandlerMap) handlePacket(addr net.Addr, data []byte) error {
rcvTime := time.Now() rcvTime := time.Now()
r := bytes.NewReader(data) r := bytes.NewReader(data)
iHdr, err := wire.ParseInvariantHeader(r, h.connIDLen) hdr, err := wire.ParseHeader(r, h.connIDLen)
// drop the packet if we can't parse the header // drop the packet if we can't parse the header
if err != nil { if err != nil {
return fmt.Errorf("error parsing invariant header: %s", err) return fmt.Errorf("error parsing header: %s", err)
} }
h.mutex.RLock() h.mutex.RLock()
handlerEntry, handlerFound := h.handlers[string(iHdr.DestConnectionID)] handlerEntry, handlerFound := h.handlers[string(hdr.DestConnectionID)]
server := h.server server := h.server
var version protocol.VersionNumber var version protocol.VersionNumber
@ -183,7 +183,7 @@ func (h *packetHandlerMap) handlePacket(addr net.Addr, data []byte) error {
handlePacket = handler.handlePacket handlePacket = handler.handlePacket
} else { // no session found } else { // no session found
// this might be a stateless reset // this might be a stateless reset
if !iHdr.IsLongHeader { if !hdr.IsLongHeader {
if len(data) >= protocol.MinStatelessResetSize { if len(data) >= protocol.MinStatelessResetSize {
var token [16]byte var token [16]byte
copy(token[:], data[len(data)-16:]) copy(token[:], data[len(data)-16:])
@ -194,39 +194,39 @@ func (h *packetHandlerMap) handlePacket(addr net.Addr, data []byte) error {
} }
} }
// TODO(#943): send a stateless reset // TODO(#943): send a stateless reset
return fmt.Errorf("received a short header packet with an unexpected connection ID %s", iHdr.DestConnectionID) return fmt.Errorf("received a short header packet with an unexpected connection ID %s", hdr.DestConnectionID)
} }
if server == nil { // no server set if server == nil { // no server set
h.mutex.RUnlock() h.mutex.RUnlock()
return fmt.Errorf("received a packet with an unexpected connection ID %s", iHdr.DestConnectionID) return fmt.Errorf("received a packet with an unexpected connection ID %s", hdr.DestConnectionID)
} }
handlePacket = server.handlePacket handlePacket = server.handlePacket
version = iHdr.Version version = hdr.Version
} }
h.mutex.RUnlock() h.mutex.RUnlock()
r = bytes.NewReader(data) r = bytes.NewReader(data)
hdr, err := iHdr.Parse(r, version) extHdr, err := hdr.Parse(r, version)
if err != nil { if err != nil {
return fmt.Errorf("error parsing header: %s", err) return fmt.Errorf("error parsing extended header: %s", err)
} }
hdr.Raw = data[:len(data)-r.Len()] extHdr.Raw = data[:len(data)-r.Len()]
packetData := data[len(data)-r.Len():] packetData := data[len(data)-r.Len():]
if hdr.IsLongHeader { if extHdr.IsLongHeader {
if hdr.Length < protocol.ByteCount(hdr.PacketNumberLen) { if extHdr.Length < protocol.ByteCount(extHdr.PacketNumberLen) {
return fmt.Errorf("packet length (%d bytes) shorter than packet number (%d bytes)", hdr.Length, hdr.PacketNumberLen) return fmt.Errorf("packet length (%d bytes) shorter than packet number (%d bytes)", extHdr.Length, extHdr.PacketNumberLen)
} }
if protocol.ByteCount(len(packetData))+protocol.ByteCount(hdr.PacketNumberLen) < hdr.Length { if protocol.ByteCount(len(packetData))+protocol.ByteCount(extHdr.PacketNumberLen) < extHdr.Length {
return fmt.Errorf("packet length (%d bytes) is smaller than the expected length (%d bytes)", len(packetData)+int(hdr.PacketNumberLen), hdr.Length) return fmt.Errorf("packet length (%d bytes) is smaller than the expected length (%d bytes)", len(packetData)+int(extHdr.PacketNumberLen), extHdr.Length)
} }
packetData = packetData[:int(hdr.Length)-int(hdr.PacketNumberLen)] packetData = packetData[:int(extHdr.Length)-int(extHdr.PacketNumberLen)]
// TODO(#1312): implement parsing of compound packets // TODO(#1312): implement parsing of compound packets
} }
handlePacket(&receivedPacket{ handlePacket(&receivedPacket{
remoteAddr: addr, remoteAddr: addr,
header: hdr, header: extHdr,
data: packetData, data: packetData,
rcvTime: rcvTime, rcvTime: rcvTime,
}) })

View file

@ -83,7 +83,7 @@ var _ = Describe("Packet Handler Map", func() {
It("drops unparseable packets", func() { It("drops unparseable packets", func() {
err := handler.handlePacket(nil, []byte{0, 1, 2, 3}) err := handler.handlePacket(nil, []byte{0, 1, 2, 3})
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error parsing invariant header:")) Expect(err.Error()).To(ContainSubstring("error parsing header:"))
}) })
It("deletes removed session immediately", func() { It("deletes removed session immediately", func() {

View file

@ -31,12 +31,12 @@ var _ = Describe("Packet packer", func() {
) )
checkLength := func(data []byte) { checkLength := func(data []byte) {
iHdr, err := wire.ParseInvariantHeader(bytes.NewReader(data), 0) hdr, err := wire.ParseHeader(bytes.NewReader(data), 0)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
r := bytes.NewReader(data) r := bytes.NewReader(data)
hdr, err := iHdr.Parse(r, protocol.VersionWhatever) extHdr, err := hdr.Parse(r, protocol.VersionWhatever)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
ExpectWithOffset(0, hdr.Length).To(BeEquivalentTo(r.Len() + int(hdr.PacketNumberLen))) ExpectWithOffset(0, extHdr.Length).To(BeEquivalentTo(r.Len() + int(extHdr.PacketNumberLen)))
} }
expectAppendStreamFrames := func(frames ...wire.Frame) { expectAppendStreamFrames := func(frames ...wire.Frame) {

View file

@ -99,11 +99,11 @@ var _ = Describe("Server", func() {
}) })
parseHeader := func(data []byte) *wire.ExtendedHeader { parseHeader := func(data []byte) *wire.ExtendedHeader {
iHdr, err := wire.ParseInvariantHeader(bytes.NewReader(data), 0) hdr, err := wire.ParseHeader(bytes.NewReader(data), 0)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
hdr, err := iHdr.Parse(bytes.NewReader(data), protocol.VersionTLS) extHdr, err := hdr.Parse(bytes.NewReader(data), protocol.VersionTLS)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
return hdr return extHdr
} }
It("drops Initial packets with a too short connection ID", func() { It("drops Initial packets with a too short connection ID", func() {