move cutting of coalesced packets to the wire package

This commit is contained in:
Marten Seemann 2019-01-15 15:37:35 +07:00
parent 9f5b96f3be
commit 28ed85b9c6
11 changed files with 173 additions and 107 deletions

View file

@ -58,7 +58,7 @@ var _ = Describe("Client", func() {
composeVersionNegotiationPacket := func(connID protocol.ConnectionID, versions []protocol.VersionNumber) *receivedPacket {
data, err := wire.ComposeVersionNegotiation(connID, nil, versions)
Expect(err).ToNot(HaveOccurred())
hdr, err := wire.ParseHeader(bytes.NewReader(data), 0)
hdr, _, _, err := wire.ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.IsVersionNegotiation()).To(BeTrue())
return &receivedPacket{

View file

@ -3,6 +3,7 @@ package wire
import (
"bytes"
"errors"
"fmt"
"io"
"github.com/lucas-clemente/quic-go/internal/protocol"
@ -10,6 +11,8 @@ import (
"github.com/lucas-clemente/quic-go/internal/utils"
)
var errUnsupportedVersion = errors.New("unsupported version")
// The Header is the version independent part of the header
type Header struct {
Version protocol.VersionNumber
@ -28,19 +31,43 @@ type Header struct {
parsedLen protocol.ByteCount // how many bytes were read while parsing this header
}
// ParsePacket parses a packet.
// If the packet has a long header, the packet is cut according to the length field.
// If we understand the version, the packet is header up unto the packet number.
// Otherwise, only the invariant part of the header is parsed.
func ParsePacket(data []byte, shortHeaderConnIDLen int) (*Header, []byte /* packet data */, []byte /* rest */, error) {
hdr, err := parseHeader(bytes.NewReader(data), shortHeaderConnIDLen)
if err != nil {
if err == errUnsupportedVersion {
return hdr, nil, nil, nil
}
return nil, nil, nil, err
}
var rest []byte
if hdr.IsLongHeader {
if protocol.ByteCount(len(data)) < hdr.ParsedLen()+hdr.Length {
return nil, nil, nil, fmt.Errorf("packet length (%d bytes) is smaller than the expected length (%d bytes)", len(data)-int(hdr.ParsedLen()), hdr.Length)
}
packetLen := int(hdr.ParsedLen() + hdr.Length)
rest = data[packetLen:]
data = data[:packetLen]
}
return hdr, data, rest, nil
}
// ParseHeader parses the header.
// For short header packets: up to the packet number.
// For long header packets:
// * if we understand the version: up to the packet number
// * if not, only the invariant part of the header
func ParseHeader(b *bytes.Reader, shortHeaderConnIDLen int) (*Header, error) {
func parseHeader(b *bytes.Reader, shortHeaderConnIDLen int) (*Header, error) {
startLen := b.Len()
h, err := parseHeaderImpl(b, shortHeaderConnIDLen)
if err != nil {
return nil, err
return h, err
}
h.parsedLen = protocol.ByteCount(startLen - b.Len())
return h, nil
return h, err
}
func parseHeaderImpl(b *bytes.Reader, shortHeaderConnIDLen int) (*Header, error) {
@ -63,10 +90,7 @@ func parseHeaderImpl(b *bytes.Reader, shortHeaderConnIDLen int) (*Header, error)
}
return h, nil
}
if err := h.parseLongHeader(b); err != nil {
return nil, err
}
return h, nil
return h, h.parseLongHeader(b)
}
func (h *Header) parseShortHeader(b *bytes.Reader, shortHeaderConnIDLen int) error {
@ -102,7 +126,7 @@ func (h *Header) parseLongHeader(b *bytes.Reader) error {
}
// If we don't understand the version, we have no idea how to interpret the rest of the bytes
if !protocol.IsSupportedVersion(protocol.SupportedVersions, h.Version) {
return nil
return errUnsupportedVersion
}
switch (h.typeByte & 0x30) >> 4 {

View file

@ -24,10 +24,9 @@ var _ = Describe("Header Parsing", func() {
srcConnID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
destConnID := protocol.ConnectionID{9, 8, 7, 6, 5, 4, 3, 2, 1}
versions := []protocol.VersionNumber{0x22334455, 0x33445566}
data, err := ComposeVersionNegotiation(destConnID, srcConnID, versions)
vnp, err := ComposeVersionNegotiation(destConnID, srcConnID, versions)
Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data)
hdr, err := ParseHeader(b, 0)
hdr, _, rest, err := ParsePacket(vnp, 0)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.DestConnectionID).To(Equal(destConnID))
Expect(hdr.SrcConnectionID).To(Equal(srcConnID))
@ -37,7 +36,7 @@ var _ = Describe("Header Parsing", func() {
for _, v := range versions {
Expect(hdr.SupportedVersions).To(ContainElement(v))
}
Expect(b.Len()).To(BeZero())
Expect(rest).To(BeEmpty())
})
It("errors if it contains versions of the wrong length", func() {
@ -45,8 +44,7 @@ var _ = Describe("Header Parsing", func() {
versions := []protocol.VersionNumber{0x22334455, 0x33445566}
data, err := ComposeVersionNegotiation(connID, connID, versions)
Expect(err).ToNot(HaveOccurred())
data = data[:len(data)-2]
_, err = ParseHeader(bytes.NewReader(data), 0)
_, _, _, err = ParsePacket(data[:len(data)-2], 0)
Expect(err).To(MatchError(qerr.InvalidVersionNegotiationPacket))
})
@ -57,7 +55,7 @@ var _ = Describe("Header Parsing", func() {
Expect(err).ToNot(HaveOccurred())
// remove 8 bytes (two versions), since ComposeVersionNegotiation also added a reserved version number
data = data[:len(data)-8]
_, err = ParseHeader(bytes.NewReader(data), 0)
_, _, _, err = ParsePacket(data, 0)
Expect(err).To(MatchError("InvalidVersionNegotiationPacket: empty version list"))
})
})
@ -71,28 +69,31 @@ var _ = Describe("Header Parsing", func() {
data = append(data, 0x61) // connection ID lengths
data = append(data, destConnID...)
data = append(data, srcConnID...)
data = append(data, encodeVarInt(6)...) // token length
data = append(data, []byte("foobar")...) // token
data = append(data, encodeVarInt(0x1337)...) // length
data = append(data, encodeVarInt(6)...) // token length
data = append(data, []byte("foobar")...) // token
data = append(data, encodeVarInt(10)...) // length
hdrLen := len(data)
data = append(data, []byte{0, 0, 0xbe, 0xef}...)
data = append(data, []byte{0, 0, 0xbe, 0xef}...) // packet number
data = append(data, []byte("foobar")...)
hdr, err := ParseHeader(bytes.NewReader(data), 0)
hdr, pdata, rest, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
Expect(pdata).To(Equal(data))
Expect(hdr.IsLongHeader).To(BeTrue())
Expect(hdr.IsVersionNegotiation()).To(BeFalse())
Expect(hdr.DestConnectionID).To(Equal(destConnID))
Expect(hdr.SrcConnectionID).To(Equal(srcConnID))
Expect(hdr.Type).To(Equal(protocol.PacketTypeInitial))
Expect(hdr.Token).To(Equal([]byte("foobar")))
Expect(hdr.Length).To(Equal(protocol.ByteCount(0x1337)))
Expect(hdr.Length).To(Equal(protocol.ByteCount(10)))
Expect(hdr.Version).To(Equal(versionIETFFrames))
Expect(rest).To(BeEmpty())
b := bytes.NewReader(data)
extHdr, err := hdr.ParseExtended(b, versionIETFFrames)
Expect(err).ToNot(HaveOccurred())
Expect(extHdr.PacketNumber).To(Equal(protocol.PacketNumber(0xbeef)))
Expect(extHdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen4))
Expect(b.Len()).To(BeZero())
Expect(b.Len()).To(Equal(6)) // foobar
Expect(hdr.ParsedLen()).To(BeEquivalentTo(hdrLen))
})
@ -103,7 +104,7 @@ var _ = Describe("Header Parsing", func() {
0xde, 0xca, 0xfb, 0xad, // dest conn ID
0xde, 0xad, 0xbe, 0xef, // src conn ID
}
_, err := ParseHeader(bytes.NewReader(data), 0)
_, _, _, err := ParsePacket(data, 0)
Expect(err).To(MatchError("not a QUIC packet"))
})
@ -116,14 +117,13 @@ var _ = Describe("Header Parsing", func() {
0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1,
'f', 'o', 'o', 'b', 'a', 'r', // unspecified bytes
}
b := bytes.NewReader(data)
hdr, err := ParseHeader(b, 0)
hdr, _, rest, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.IsLongHeader).To(BeTrue())
Expect(hdr.Version).To(Equal(protocol.VersionNumber(0xdeadbeef)))
Expect(hdr.DestConnectionID).To(Equal(protocol.ConnectionID{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8}))
Expect(hdr.SrcConnectionID).To(Equal(protocol.ConnectionID{0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1}))
Expect(b.Len()).To(Equal(6))
Expect(rest).To(BeEmpty())
})
It("parses a Long Header without a destination connection ID", func() {
@ -131,9 +131,9 @@ var _ = Describe("Header Parsing", func() {
data = appendVersion(data, versionIETFFrames)
data = append(data, 0x01) // connection ID lengths
data = append(data, []byte{0xde, 0xad, 0xbe, 0xef}...) // source connection ID
data = append(data, encodeVarInt(0x42)...) // length
data = append(data, encodeVarInt(0)...) // length
data = append(data, []byte{0xde, 0xca, 0xfb, 0xad}...)
hdr, err := ParseHeader(bytes.NewReader(data), 0)
hdr, _, _, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.Type).To(Equal(protocol.PacketType0RTT))
Expect(hdr.SrcConnectionID).To(Equal(protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef}))
@ -145,9 +145,9 @@ var _ = Describe("Header Parsing", func() {
data = appendVersion(data, versionIETFFrames)
data = append(data, 0x70) // connection ID lengths
data = append(data, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}...) // source connection ID
data = append(data, encodeVarInt(0x42)...) // length
data = append(data, encodeVarInt(0)...) // length
data = append(data, []byte{0xde, 0xca, 0xfb, 0xad}...)
hdr, err := ParseHeader(bytes.NewReader(data), 0)
hdr, _, _, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.SrcConnectionID).To(BeEmpty())
Expect(hdr.DestConnectionID).To(Equal(protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}))
@ -158,12 +158,11 @@ var _ = Describe("Header Parsing", func() {
data = appendVersion(data, versionIETFFrames) // version number
data = append(data, 0x0) // connection ID lengths
data = append(data, encodeVarInt(0)...) // token length
data = append(data, encodeVarInt(0x42)...) // length
data = append(data, encodeVarInt(0)...) // length
data = append(data, []byte{0x1, 0x23}...)
hdr, err := ParseHeader(bytes.NewReader(data), 0)
hdr, _, _, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.Length).To(BeEquivalentTo(0x42))
b := bytes.NewReader(data)
extHdr, err := hdr.ParseExtended(b, versionIETFFrames)
Expect(err).ToNot(HaveOccurred())
@ -178,12 +177,13 @@ var _ = Describe("Header Parsing", func() {
data = append(data, 0x0) // connection ID lengths
data = append(data, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}...) // source connection ID
data = append(data, []byte{'f', 'o', 'o', 'b', 'a', 'r'}...) // token
b := bytes.NewReader(data)
hdr, err := ParseHeader(b, 0)
hdr, pdata, rest, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.Type).To(Equal(protocol.PacketTypeRetry))
Expect(hdr.OrigDestConnectionID).To(Equal(protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}))
Expect(hdr.Token).To(Equal([]byte("foobar")))
Expect(pdata).To(Equal(data))
Expect(rest).To(BeEmpty())
})
It("errors if the token length is too large", func() {
@ -194,17 +194,16 @@ var _ = Describe("Header Parsing", func() {
data = append(data, encodeVarInt(0x42)...) // length, 1 byte
data = append(data, []byte{0x12, 0x34}...) // packet number
_, err := ParseHeader(bytes.NewReader(data), 0)
_, _, _, err := ParsePacket(data, 0)
Expect(err).To(MatchError(io.EOF))
})
It("errors if the 5th or 6th bit are set", func() {
data := []byte{0xc0 | 0x2<<4 | 0x8 /* set the 5th bit */}
data := []byte{0xc0 | 0x2<<4 /* set the 5th bit */ | 0x8}
data = appendVersion(data, versionIETFFrames)
data = append(data, 0x0) // connection ID lengths
data = append(data, 0x42) // packet number
data = append(data, encodeVarInt(1)...) // length
hdr, err := ParseHeader(bytes.NewReader(data), 0)
data = append(data, encodeVarInt(0)...) // length
hdr, _, _, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.Type).To(Equal(protocol.PacketTypeHandshake))
_, err = hdr.ParseExtended(bytes.NewReader(data), versionIETFFrames)
@ -218,7 +217,7 @@ var _ = Describe("Header Parsing", func() {
data = append(data, []byte{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}...) // destination connection ID
data = append(data, []byte{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}...) // source connection ID
for i := 0; i < len(data); i++ {
_, err := ParseHeader(bytes.NewReader(data[:i]), 0)
_, _, _, err := ParsePacket(data[:i], 0)
Expect(err).To(Equal(io.EOF))
}
})
@ -226,13 +225,13 @@ var _ = Describe("Header Parsing", func() {
It("errors on EOF, when parsing the extended header", func() {
data := []byte{0xc0 | 0x2<<4 | 0x3}
data = appendVersion(data, versionIETFFrames)
data = append(data, 0x0) // connection ID lengths
data = append(data, encodeVarInt(0x1337)...)
data = append(data, 0x0) // connection ID lengths
data = append(data, encodeVarInt(0)...) // length
hdrLen := len(data)
data = append(data, []byte{0xde, 0xad, 0xbe, 0xef}...) // packet number
for i := hdrLen; i < len(data); i++ {
data = data[:i]
hdr, err := ParseHeader(bytes.NewReader(data), 0)
hdr, _, _, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data)
_, err = hdr.ParseExtended(b, versionIETFFrames)
@ -249,13 +248,75 @@ var _ = Describe("Header Parsing", func() {
hdrLen := len(data)
for i := hdrLen; i < len(data); i++ {
data = data[:i]
hdr, err := ParseHeader(bytes.NewReader(data), 0)
hdr, _, _, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data)
_, err = hdr.ParseExtended(b, versionIETFFrames)
Expect(err).To(Equal(io.EOF))
}
})
Context("coalesced packets", func() {
It("cuts packets", func() {
buf := &bytes.Buffer{}
hdr := Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: protocol.ConnectionID{1, 2, 3, 4},
Length: 2 + 6,
Version: versionIETFFrames,
}
Expect((&ExtendedHeader{
Header: hdr,
PacketNumber: 0x1337,
PacketNumberLen: 2,
}).Write(buf, versionIETFFrames)).To(Succeed())
hdrRaw := append([]byte{}, buf.Bytes()...)
buf.Write([]byte("foobar")) // payload of the first packet
buf.Write([]byte("raboof")) // second packet
parsedHdr, data, rest, err := ParsePacket(buf.Bytes(), 4)
Expect(err).ToNot(HaveOccurred())
Expect(parsedHdr.Type).To(Equal(hdr.Type))
Expect(parsedHdr.DestConnectionID).To(Equal(hdr.DestConnectionID))
Expect(data).To(Equal(append(hdrRaw, []byte("foobar")...)))
Expect(rest).To(Equal([]byte("raboof")))
})
It("errors on packets that are smaller than the length in the packet header, for too small packet number", func() {
buf := &bytes.Buffer{}
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: protocol.ConnectionID{1, 2, 3, 4},
Length: 3,
Version: versionIETFFrames,
},
PacketNumber: 0x1337,
PacketNumberLen: 2,
}).Write(buf, versionIETFFrames)).To(Succeed())
_, _, _, err := ParsePacket(buf.Bytes(), 4)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("packet length (2 bytes) is smaller than the expected length (3 bytes)"))
})
It("errors on packets that are smaller than the length in the packet header, for too small payload", func() {
buf := &bytes.Buffer{}
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: protocol.ConnectionID{1, 2, 3, 4},
Length: 1000,
Version: versionIETFFrames,
},
PacketNumber: 0x1337,
PacketNumberLen: 2,
}).Write(buf, versionIETFFrames)).To(Succeed())
buf.Write(make([]byte, 500-2 /* for packet number length */))
_, _, _, err := ParsePacket(buf.Bytes(), 4)
Expect(err).To(MatchError("packet length (500 bytes) is smaller than the expected length (1000 bytes)"))
})
})
})
Context("Short Headers", func() {
@ -263,7 +324,7 @@ var _ = Describe("Header Parsing", func() {
connID := protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}
data := append([]byte{0x40}, connID...)
data = append(data, 0x42) // packet number
hdr, err := ParseHeader(bytes.NewReader(data), 8)
hdr, pdata, rest, err := ParsePacket(data, 8)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.IsLongHeader).To(BeFalse())
Expect(hdr.IsVersionNegotiation()).To(BeFalse())
@ -275,20 +336,21 @@ var _ = Describe("Header Parsing", func() {
Expect(extHdr.DestConnectionID).To(Equal(connID))
Expect(extHdr.SrcConnectionID).To(BeEmpty())
Expect(extHdr.PacketNumber).To(Equal(protocol.PacketNumber(0x42)))
Expect(b.Len()).To(BeZero())
Expect(pdata).To(Equal(data))
Expect(rest).To(BeEmpty())
})
It("errors if 0x40 is not set", func() {
connID := protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}
data := append([]byte{0x0}, connID...)
_, err := ParseHeader(bytes.NewReader(data), 8)
_, _, _, err := ParsePacket(data, 8)
Expect(err).To(MatchError("not a QUIC packet"))
})
It("errors if the 4th or 5th bit are set", func() {
connID := protocol.ConnectionID{1, 2, 3, 4, 5}
data := append([]byte{0x40 | 0x10 /* set the 4th bit */}, connID...)
hdr, err := ParseHeader(bytes.NewReader(data), 5)
hdr, _, _, err := ParsePacket(data, 5)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.IsLongHeader).To(BeFalse())
_, err = hdr.ParseExtended(bytes.NewReader(data), versionIETFFrames)
@ -299,8 +361,9 @@ var _ = Describe("Header Parsing", func() {
connID := protocol.ConnectionID{1, 2, 3, 4, 5}
data := append([]byte{0x40}, connID...)
data = append(data, 0x42) // packet number
hdr, err := ParseHeader(bytes.NewReader(data), 5)
hdr, pdata, rest, err := ParsePacket(data, 5)
Expect(err).ToNot(HaveOccurred())
Expect(pdata).To(HaveLen(len(data)))
Expect(hdr.IsLongHeader).To(BeFalse())
Expect(hdr.DestConnectionID).To(Equal(connID))
b := bytes.NewReader(data)
@ -309,7 +372,7 @@ var _ = Describe("Header Parsing", func() {
Expect(extHdr.KeyPhase).To(Equal(0))
Expect(extHdr.DestConnectionID).To(Equal(connID))
Expect(extHdr.SrcConnectionID).To(BeEmpty())
Expect(b.Len()).To(BeZero())
Expect(rest).To(BeEmpty())
})
It("reads the Key Phase Bit", func() {
@ -318,7 +381,7 @@ var _ = Describe("Header Parsing", func() {
0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, // connection ID
}
data = append(data, 11) // packet number
hdr, err := ParseHeader(bytes.NewReader(data), 6)
hdr, _, _, err := ParsePacket(data, 6)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.IsLongHeader).To(BeFalse())
b := bytes.NewReader(data)
@ -334,7 +397,7 @@ var _ = Describe("Header Parsing", func() {
0xde, 0xad, 0xbe, 0xef, // connection ID
}
data = append(data, []byte{0x13, 0x37}...) // packet number
hdr, err := ParseHeader(bytes.NewReader(data), 4)
hdr, _, _, err := ParsePacket(data, 4)
Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data)
extHdr, err := hdr.ParseExtended(b, versionIETFFrames)
@ -351,7 +414,7 @@ var _ = Describe("Header Parsing", func() {
0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x1, 0x2, 0x3, 0x4, // connection ID
}
data = append(data, []byte{0x99, 0xbe, 0xef}...) // packet number
hdr, err := ParseHeader(bytes.NewReader(data), 10)
hdr, _, _, err := ParsePacket(data, 10)
Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data)
extHdr, err := hdr.ParseExtended(b, versionIETFFrames)
@ -369,7 +432,7 @@ var _ = Describe("Header Parsing", func() {
}
for i := 0; i < len(data); i++ {
data = data[:i]
_, err := ParseHeader(bytes.NewReader(data), 8)
_, _, _, err := ParsePacket(data, 8)
Expect(err).To(Equal(io.EOF))
}
})
@ -383,7 +446,7 @@ var _ = Describe("Header Parsing", func() {
data = append(data, []byte{0xde, 0xad, 0xbe, 0xef}...) // packet number
for i := hdrLen; i < len(data); i++ {
data = data[:i]
hdr, err := ParseHeader(bytes.NewReader(data), 6)
hdr, _, _, err := ParsePacket(data, 6)
Expect(err).ToNot(HaveOccurred())
_, err = hdr.ParseExtended(bytes.NewReader(data), versionIETFFrames)
Expect(err).To(Equal(io.EOF))

View file

@ -1,8 +1,6 @@
package wire
import (
"bytes"
"github.com/lucas-clemente/quic-go/internal/protocol"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
@ -16,9 +14,7 @@ var _ = Describe("Version Negotiation Packets", func() {
data, err := ComposeVersionNegotiation(destConnID, srcConnID, versions)
Expect(err).ToNot(HaveOccurred())
Expect(data[0] & 0x80).ToNot(BeZero())
b := bytes.NewReader(data)
hdr, err := ParseHeader(b, 4)
Expect(err).ToNot(HaveOccurred())
hdr, _, rest, err := ParsePacket(data, 4)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.DestConnectionID).To(Equal(destConnID))
Expect(hdr.SrcConnectionID).To(Equal(srcConnID))
@ -28,6 +24,6 @@ var _ = Describe("Version Negotiation Packets", func() {
for _, version := range versions {
Expect(hdr.SupportedVersions).To(ContainElement(version))
}
Expect(b.Len()).To(BeZero())
Expect(rest).To(BeEmpty())
})
})

View file

@ -1,7 +1,6 @@
package quic
import (
"bytes"
"errors"
"fmt"
"net"
@ -199,26 +198,16 @@ func (h *packetHandlerMap) parsePacket(
var counter int
var lastConnID protocol.ConnectionID
for len(data) > 0 {
hdr, err := wire.ParseHeader(bytes.NewReader(data), h.connIDLen)
// drop the packet if we can't parse the header
hdr, packetData, rest, err := wire.ParsePacket(data, h.connIDLen)
if err != nil {
return packets, fmt.Errorf("error parsing header: %s", err)
return packets, fmt.Errorf("error parsing packet: %s", err)
}
if counter > 0 && !hdr.DestConnectionID.Equal(lastConnID) {
return packets, fmt.Errorf("coalesced packet has different destination connection ID: %s, expected %s", hdr.DestConnectionID, lastConnID)
}
lastConnID = hdr.DestConnectionID
var rest []byte
if hdr.IsLongHeader {
if protocol.ByteCount(len(data)) < hdr.ParsedLen()+hdr.Length {
return packets, fmt.Errorf("packet length (%d bytes) is smaller than the expected length (%d bytes)", len(data)-int(hdr.ParsedLen()), hdr.Length)
}
packetLen := int(hdr.ParsedLen() + hdr.Length)
rest = data[packetLen:]
data = data[:packetLen]
}
if counter > 0 {
buffer.Split()
}
@ -227,7 +216,7 @@ func (h *packetHandlerMap) parsePacket(
remoteAddr: addr,
hdr: hdr,
rcvTime: rcvTime,
data: data,
data: packetData,
buffer: buffer,
})

View file

@ -107,7 +107,7 @@ var _ = Describe("Packet Handler Map", func() {
It("drops unparseable packets", func() {
_, err := handler.parsePacket(nil, nil, []byte{0, 1, 2, 3})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error parsing header:"))
Expect(err.Error()).To(ContainSubstring("error parsing packet:"))
})
It("deletes removed session immediately", func() {
@ -161,20 +161,6 @@ var _ = Describe("Packet Handler Map", func() {
})
Context("coalesced packets", func() {
It("errors on packets that are smaller than the length in the packet header, for too small packet number", func() {
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
data := getPacketWithLength(connID, 3) // gets a packet with a 2 byte packet number
_, err := handler.parsePacket(nil, nil, data)
Expect(err).To(MatchError("packet length (2 bytes) is smaller than the expected length (3 bytes)"))
})
It("errors on packets that are smaller than the length in the packet header, for too small payload", func() {
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
data := append(getPacketWithLength(connID, 1000), make([]byte, 500-2 /* for packet number length */)...)
_, err := handler.parsePacket(nil, nil, data)
Expect(err).To(MatchError("packet length (500 bytes) is smaller than the expected length (1000 bytes)"))
})
It("cuts packets to the right length", func() {
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
data := append(getPacketWithLength(connID, 456), make([]byte, 1000)...)

View file

@ -29,7 +29,7 @@ var _ = Describe("Packet packer", func() {
)
checkLength := func(data []byte) {
hdr, err := wire.ParseHeader(bytes.NewReader(data), 0)
hdr, _, _, err := wire.ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
r := bytes.NewReader(data)
extHdr, err := hdr.ParseExtended(r, protocol.VersionWhatever)
@ -808,7 +808,7 @@ var _ = Describe("Packet packer", func() {
Expect(err).ToNot(HaveOccurred())
// cut off the tag that the mock sealer added
packet.raw = packet.raw[:len(packet.raw)-sealer.Overhead()]
hdr, err := wire.ParseHeader(bytes.NewReader(packet.raw), len(packer.destConnID))
hdr, _, _, err := wire.ParsePacket(packet.raw, len(packer.destConnID))
Expect(err).ToNot(HaveOccurred())
r := bytes.NewReader(packet.raw)
extHdr, err := hdr.ParseExtended(r, packer.version)

View file

@ -25,10 +25,14 @@ var _ = Describe("Packet Unpacker", func() {
getHeader := func(extHdr *wire.ExtendedHeader) (*wire.Header, []byte) {
buf := &bytes.Buffer{}
Expect(extHdr.Write(buf, protocol.VersionWhatever)).To(Succeed())
hdr, err := wire.ParseHeader(bytes.NewReader(buf.Bytes()), connID.Len())
Expect(err).ToNot(HaveOccurred())
return hdr, buf.Bytes()
ExpectWithOffset(1, extHdr.Write(buf, protocol.VersionWhatever)).To(Succeed())
hdrLen := buf.Len()
if extHdr.Length > protocol.ByteCount(extHdr.PacketNumberLen) {
buf.Write(make([]byte, int(extHdr.Length)-int(extHdr.PacketNumberLen)))
}
hdr, _, _, err := wire.ParsePacket(buf.Bytes(), connID.Len())
ExpectWithOffset(1, err).ToNot(HaveOccurred())
return hdr, buf.Bytes()[:hdrLen]
}
BeforeEach(func() {

View file

@ -336,8 +336,14 @@ func (s *server) handlePacket(p *receivedPacket) {
return
}
// TODO(#943): send Stateless Reset
p.buffer.Release()
defer p.buffer.Release()
// Drop long header packets.
// There's litte point in sending a Stateless Reset, since the client
// might not have received the token yet.
if hdr.IsLongHeader {
return
}
}
func (s *server) handleInitial(p *receivedPacket) {

View file

@ -118,7 +118,7 @@ var _ = Describe("Server", func() {
})
parseHeader := func(data []byte) *wire.Header {
hdr, err := wire.ParseHeader(bytes.NewReader(data), 0)
hdr, _, _, err := wire.ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
return hdr
}
@ -239,8 +239,7 @@ var _ = Describe("Server", func() {
var write mockPacketConnWrite
Eventually(conn.dataWritten).Should(Receive(&write))
Expect(write.to.String()).To(Equal("127.0.0.1:1337"))
hdr, err := wire.ParseHeader(bytes.NewReader(write.data), 0)
Expect(err).ToNot(HaveOccurred())
hdr := parseHeader(write.data)
Expect(hdr.IsVersionNegotiation()).To(BeTrue())
Expect(hdr.DestConnectionID).To(Equal(srcConnID))
Expect(hdr.SrcConnectionID).To(Equal(destConnID))
@ -370,8 +369,7 @@ var _ = Describe("Server", func() {
var reject mockPacketConnWrite
Eventually(conn.dataWritten).Should(Receive(&reject))
Expect(reject.to).To(Equal(senderAddr))
rejectHdr, err := wire.ParseHeader(bytes.NewReader(reject.data), 0)
Expect(err).ToNot(HaveOccurred())
rejectHdr := parseHeader(reject.data)
Expect(rejectHdr.Type).To(Equal(protocol.PacketTypeInitial))
Expect(rejectHdr.Version).To(Equal(hdr.Version))
Expect(rejectHdr.DestConnectionID).To(Equal(hdr.SrcConnectionID))

View file

@ -496,7 +496,7 @@ var _ = Describe("Session", func() {
buf := &bytes.Buffer{}
Expect(extHdr.Write(buf, sess.version)).To(Succeed())
// need to set extHdr.Header, since the wire.Header contains the parsed length
hdr, err := wire.ParseHeader(bytes.NewReader(buf.Bytes()), 0)
hdr, _, _, err := wire.ParsePacket(buf.Bytes(), 0)
Expect(err).ToNot(HaveOccurred())
extHdr.Header = *hdr
return buf.Bytes()