include the packet number len in the length calculation for long headers

This commit is contained in:
Marten Seemann 2018-11-25 14:04:13 +07:00
parent 34dd046cb6
commit fd646ff877
8 changed files with 78 additions and 56 deletions

View file

@ -28,7 +28,7 @@ type Header struct {
Type protocol.PacketType Type protocol.PacketType
IsLongHeader bool IsLongHeader bool
KeyPhase int KeyPhase int
PayloadLen protocol.ByteCount Length protocol.ByteCount
Token []byte Token []byte
} }
@ -72,7 +72,7 @@ func (h *Header) writeLongHeader(b *bytes.Buffer, v protocol.VersionNumber) erro
return nil return nil
} }
utils.WriteVarInt(b, uint64(h.PayloadLen)) utils.WriteVarInt(b, uint64(h.Length))
return utils.WriteVarIntPacketNumber(b, h.PacketNumber, h.PacketNumberLen) return utils.WriteVarIntPacketNumber(b, h.PacketNumber, h.PacketNumberLen)
} }
@ -88,7 +88,7 @@ func (h *Header) writeShortHeader(b *bytes.Buffer, v protocol.VersionNumber) err
// GetLength determines the length of the Header. // GetLength determines the length of the Header.
func (h *Header) GetLength(v protocol.VersionNumber) protocol.ByteCount { func (h *Header) GetLength(v protocol.VersionNumber) protocol.ByteCount {
if h.IsLongHeader { if h.IsLongHeader {
length := 1 /* type byte */ + 4 /* version */ + 1 /* conn id len byte */ + protocol.ByteCount(h.DestConnectionID.Len()+h.SrcConnectionID.Len()) + protocol.ByteCount(h.PacketNumberLen) + utils.VarIntLen(uint64(h.PayloadLen)) length := 1 /* type byte */ + 4 /* version */ + 1 /* conn id len byte */ + protocol.ByteCount(h.DestConnectionID.Len()+h.SrcConnectionID.Len()) + protocol.ByteCount(h.PacketNumberLen) + utils.VarIntLen(uint64(h.Length))
if h.Type == protocol.PacketTypeInitial { if h.Type == protocol.PacketTypeInitial {
length += utils.VarIntLen(uint64(len(h.Token))) + protocol.ByteCount(len(h.Token)) length += utils.VarIntLen(uint64(len(h.Token))) + protocol.ByteCount(len(h.Token))
} }
@ -118,7 +118,7 @@ func (h *Header) Log(logger utils.Logger) {
logger.Debugf("\tLong Header{Type: %s, DestConnectionID: %s, SrcConnectionID: %s, %sOrigDestConnectionID: %s, Version: %s}", h.Type, h.DestConnectionID, h.SrcConnectionID, token, h.OrigDestConnectionID, h.Version) logger.Debugf("\tLong Header{Type: %s, DestConnectionID: %s, SrcConnectionID: %s, %sOrigDestConnectionID: %s, Version: %s}", h.Type, h.DestConnectionID, h.SrcConnectionID, token, h.OrigDestConnectionID, h.Version)
return return
} }
logger.Debugf("\tLong Header{Type: %s, DestConnectionID: %s, SrcConnectionID: %s, %sPacketNumber: %#x, PacketNumberLen: %d, PayloadLen: %d, Version: %s}", h.Type, h.DestConnectionID, h.SrcConnectionID, token, h.PacketNumber, h.PacketNumberLen, h.PayloadLen, h.Version) logger.Debugf("\tLong Header{Type: %s, DestConnectionID: %s, SrcConnectionID: %s, %sPacketNumber: %#x, PacketNumberLen: %d, Length: %d, Version: %s}", h.Type, h.DestConnectionID, h.SrcConnectionID, token, h.PacketNumber, h.PacketNumberLen, h.Length, h.Version)
} }
} else { } else {
logger.Debugf("\tShort Header{DestConnectionID: %s, PacketNumber: %#x, PacketNumberLen: %d, KeyPhase: %d}", h.DestConnectionID, h.PacketNumber, h.PacketNumberLen, h.KeyPhase) logger.Debugf("\tShort Header{DestConnectionID: %s, PacketNumber: %#x, PacketNumberLen: %d, KeyPhase: %d}", h.DestConnectionID, h.PacketNumber, h.PacketNumberLen, h.KeyPhase)

View file

@ -141,7 +141,7 @@ func (iv *InvariantHeader) parseLongHeader(b *bytes.Reader, sentBy protocol.Pers
if err != nil { if err != nil {
return nil, err return nil, err
} }
h.PayloadLen = protocol.ByteCount(pl) h.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

View file

@ -81,7 +81,7 @@ var _ = Describe("Header Parsing", func() {
data = append(data, srcConnID...) data = append(data, srcConnID...)
data = append(data, encodeVarInt(6)...) // token length data = append(data, encodeVarInt(6)...) // token length
data = append(data, []byte("foobar")...) // token data = append(data, []byte("foobar")...) // token
data = append(data, encodeVarInt(0x1337)...) // payload length data = append(data, encodeVarInt(0x1337)...) // length
// packet number // packet number
data = appendPacketNumber(data, 0xbeef, protocol.PacketNumberLen4) data = appendPacketNumber(data, 0xbeef, protocol.PacketNumberLen4)
@ -98,7 +98,7 @@ var _ = Describe("Header Parsing", func() {
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"))) Expect(hdr.Token).To(Equal([]byte("foobar")))
Expect(hdr.PayloadLen).To(Equal(protocol.ByteCount(0x1337))) Expect(hdr.Length).To(Equal(protocol.ByteCount(0x1337)))
Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0xbeef))) Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0xbeef)))
Expect(hdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen4)) Expect(hdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen4))
Expect(hdr.Version).To(Equal(protocol.VersionNumber(0x1020304))) Expect(hdr.Version).To(Equal(protocol.VersionNumber(0x1020304)))
@ -113,7 +113,7 @@ var _ = Describe("Header Parsing", func() {
0x01, // connection ID lengths 0x01, // connection ID lengths
0xde, 0xad, 0xbe, 0xef, // source connection ID 0xde, 0xad, 0xbe, 0xef, // source connection ID
} }
data = append(data, encodeVarInt(0x42)...) // payload length data = append(data, encodeVarInt(0x42)...) // length
data = append(data, []byte{0xde, 0xca, 0xfb, 0xad}...) data = append(data, []byte{0xde, 0xca, 0xfb, 0xad}...)
b := bytes.NewReader(data) b := bytes.NewReader(data)
iHdr, err := ParseInvariantHeader(b, 0) iHdr, err := ParseInvariantHeader(b, 0)
@ -129,7 +129,7 @@ var _ = Describe("Header Parsing", func() {
0x70, // connection ID lengths 0x70, // connection ID lengths
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
} }
data = append(data, encodeVarInt(0x42)...) // payload length data = append(data, encodeVarInt(0x42)...) // length
data = append(data, []byte{0xde, 0xca, 0xfb, 0xad}...) data = append(data, []byte{0xde, 0xca, 0xfb, 0xad}...)
b := bytes.NewReader(data) b := bytes.NewReader(data)
iHdr, err := ParseInvariantHeader(b, 0) iHdr, err := ParseInvariantHeader(b, 0)
@ -145,7 +145,7 @@ var _ = Describe("Header Parsing", func() {
0x0, // connection ID lengths 0x0, // connection ID lengths
} }
data = append(data, encodeVarInt(0)...) // token length data = append(data, encodeVarInt(0)...) // token length
data = append(data, encodeVarInt(0x42)...) // payload length data = append(data, encodeVarInt(0x42)...) // length
data = appendPacketNumber(data, 0x123, protocol.PacketNumberLen2) data = appendPacketNumber(data, 0x123, protocol.PacketNumberLen2)
b := bytes.NewReader(data) b := bytes.NewReader(data)
@ -202,7 +202,7 @@ var _ = Describe("Header Parsing", func() {
0x0, // connection ID lengths 0x0, // connection ID lengths
} }
data = append(data, encodeVarInt(4)...) // token length: 4 bytes (1 byte too long) data = append(data, encodeVarInt(4)...) // token length: 4 bytes (1 byte too long)
data = append(data, encodeVarInt(0x42)...) // payload length, 1 byte data = append(data, encodeVarInt(0x42)...) // length, 1 byte
data = appendPacketNumber(data, 0x123, protocol.PacketNumberLen2) // 2 bytes data = appendPacketNumber(data, 0x123, protocol.PacketNumberLen2) // 2 bytes
b := bytes.NewReader(data) b := bytes.NewReader(data)

View file

@ -36,7 +36,7 @@ var _ = Describe("Header", func() {
Type: 0x5, Type: 0x5,
DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe}, DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe},
SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad, 0x0, 0x0, 0x13, 0x37}, SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad, 0x0, 0x0, 0x13, 0x37},
PayloadLen: 0xcafe, Length: 0xcafe,
PacketNumber: 0xdecaf, PacketNumber: 0xdecaf,
PacketNumberLen: protocol.PacketNumberLen4, PacketNumberLen: protocol.PacketNumberLen4,
Version: 0x1020304, Version: 0x1020304,
@ -49,7 +49,7 @@ var _ = Describe("Header", func() {
0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, // dest connection ID 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, // dest connection ID
0xde, 0xca, 0xfb, 0xad, 0x0, 0x0, 0x13, 0x37, // source connection ID 0xde, 0xca, 0xfb, 0xad, 0x0, 0x0, 0x13, 0x37, // source connection ID
} }
expected = append(expected, encodeVarInt(0xcafe)...) // payload length expected = append(expected, encodeVarInt(0xcafe)...) // length
expected = appendPacketNumber(expected, 0xdecaf, protocol.PacketNumberLen4) expected = appendPacketNumber(expected, 0xdecaf, protocol.PacketNumberLen4)
Expect(buf.Bytes()).To(Equal(expected)) Expect(buf.Bytes()).To(Equal(expected))
}) })
@ -220,30 +220,30 @@ var _ = Describe("Header", func() {
buf = &bytes.Buffer{} buf = &bytes.Buffer{}
}) })
It("has the right length for the Long Header, for a short payload length", func() { It("has the right length for the Long Header, for a short length", func() {
h := &Header{ h := &Header{
IsLongHeader: true, IsLongHeader: true,
PayloadLen: 1, Length: 1,
DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8},
SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8},
PacketNumberLen: protocol.PacketNumberLen1, PacketNumberLen: protocol.PacketNumberLen1,
} }
expectedLen := 1 /* type byte */ + 4 /* version */ + 1 /* conn ID len */ + 8 /* dest conn id */ + 8 /* src conn id */ + 1 /* short payload len */ + 1 /* packet number */ expectedLen := 1 /* type byte */ + 4 /* version */ + 1 /* conn ID len */ + 8 /* dest conn id */ + 8 /* src conn id */ + 1 /* short len */ + 1 /* packet number */
Expect(h.GetLength(versionIETFHeader)).To(BeEquivalentTo(expectedLen)) Expect(h.GetLength(versionIETFHeader)).To(BeEquivalentTo(expectedLen))
err := h.Write(buf, protocol.PerspectiveClient, versionIETFHeader) err := h.Write(buf, protocol.PerspectiveClient, versionIETFHeader)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(buf.Len()).To(Equal(expectedLen)) Expect(buf.Len()).To(Equal(expectedLen))
}) })
It("has the right length for the Long Header, for a long payload length", func() { It("has the right length for the Long Header, for a long length", func() {
h := &Header{ h := &Header{
IsLongHeader: true, IsLongHeader: true,
PayloadLen: 1500, Length: 1500,
DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8},
SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8},
PacketNumberLen: protocol.PacketNumberLen2, PacketNumberLen: protocol.PacketNumberLen2,
} }
expectedLen := 1 /* type byte */ + 4 /* version */ + 1 /* conn ID len */ + 8 /* dest conn id */ + 8 /* src conn id */ + 2 /* long payload len */ + 2 /* packet number */ expectedLen := 1 /* type byte */ + 4 /* version */ + 1 /* conn ID len */ + 8 /* dest conn id */ + 8 /* src conn id */ + 2 /* long len */ + 2 /* packet number */
Expect(h.GetLength(versionIETFHeader)).To(BeEquivalentTo(expectedLen)) Expect(h.GetLength(versionIETFHeader)).To(BeEquivalentTo(expectedLen))
err := h.Write(buf, protocol.PerspectiveServer, versionIETFHeader) err := h.Write(buf, protocol.PerspectiveServer, versionIETFHeader)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
@ -254,12 +254,12 @@ var _ = Describe("Header", func() {
h := &Header{ h := &Header{
Type: protocol.PacketTypeInitial, Type: protocol.PacketTypeInitial,
IsLongHeader: true, IsLongHeader: true,
PayloadLen: 1500, Length: 1500,
DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8},
SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4}, SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4},
PacketNumberLen: protocol.PacketNumberLen2, PacketNumberLen: protocol.PacketNumberLen2,
} }
expectedLen := 1 /* type byte */ + 4 /* version */ + 1 /* conn ID len */ + 8 /* dest conn id */ + 4 /* src conn id */ + 1 /* token length */ + 2 /* long payload len */ + 2 /* packet number */ expectedLen := 1 /* type byte */ + 4 /* version */ + 1 /* conn ID len */ + 8 /* dest conn id */ + 4 /* src conn id */ + 1 /* token length */ + 2 /* long len */ + 2 /* packet number */
Expect(h.GetLength(versionIETFHeader)).To(BeEquivalentTo(expectedLen)) Expect(h.GetLength(versionIETFHeader)).To(BeEquivalentTo(expectedLen))
err := h.Write(buf, protocol.PerspectiveServer, versionIETFHeader) err := h.Write(buf, protocol.PerspectiveServer, versionIETFHeader)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
@ -270,13 +270,13 @@ var _ = Describe("Header", func() {
h := &Header{ h := &Header{
Type: protocol.PacketTypeInitial, Type: protocol.PacketTypeInitial,
IsLongHeader: true, IsLongHeader: true,
PayloadLen: 1500, Length: 1500,
DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8},
SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4}, SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4},
PacketNumberLen: protocol.PacketNumberLen2, PacketNumberLen: protocol.PacketNumberLen2,
Token: []byte("foo"), Token: []byte("foo"),
} }
expectedLen := 1 /* type byte */ + 4 /* version */ + 1 /* conn ID len */ + 8 /* dest conn id */ + 4 /* src conn id */ + 1 /* token length */ + 3 /* token */ + 2 /* long payload len */ + 2 /* packet number */ expectedLen := 1 /* type byte */ + 4 /* version */ + 1 /* conn ID len */ + 8 /* dest conn id */ + 4 /* src conn id */ + 1 /* token length */ + 3 /* token */ + 2 /* long len */ + 2 /* packet number */
Expect(h.GetLength(versionIETFHeader)).To(BeEquivalentTo(expectedLen)) Expect(h.GetLength(versionIETFHeader)).To(BeEquivalentTo(expectedLen))
err := h.Write(buf, protocol.PerspectiveServer, versionIETFHeader) err := h.Write(buf, protocol.PerspectiveServer, versionIETFHeader)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
@ -358,12 +358,12 @@ var _ = Describe("Header", func() {
Type: protocol.PacketTypeHandshake, Type: protocol.PacketTypeHandshake,
PacketNumber: 0x1337, PacketNumber: 0x1337,
PacketNumberLen: protocol.PacketNumberLen2, PacketNumberLen: protocol.PacketNumberLen2,
PayloadLen: 54321, Length: 54321,
DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}, DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37},
SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad, 0x013, 0x37, 0x13, 0x37}, SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad, 0x013, 0x37, 0x13, 0x37},
Version: 0xfeed, Version: 0xfeed,
}).Log(logger) }).Log(logger)
Expect(buf.String()).To(ContainSubstring("Long Header{Type: Handshake, DestConnectionID: 0xdeadbeefcafe1337, SrcConnectionID: 0xdecafbad13371337, PacketNumber: 0x1337, PacketNumberLen: 2, PayloadLen: 54321, Version: 0xfeed}")) Expect(buf.String()).To(ContainSubstring("Long Header{Type: Handshake, DestConnectionID: 0xdeadbeefcafe1337, SrcConnectionID: 0xdecafbad13371337, PacketNumber: 0x1337, PacketNumberLen: 2, Length: 54321, Version: 0xfeed}"))
}) })
It("logs Initial Packets with a Token", func() { It("logs Initial Packets with a Token", func() {
@ -373,12 +373,12 @@ var _ = Describe("Header", func() {
Token: []byte{0xde, 0xad, 0xbe, 0xef}, Token: []byte{0xde, 0xad, 0xbe, 0xef},
PacketNumber: 0x42, PacketNumber: 0x42,
PacketNumberLen: protocol.PacketNumberLen2, PacketNumberLen: protocol.PacketNumberLen2,
PayloadLen: 100, Length: 100,
DestConnectionID: protocol.ConnectionID{0xca, 0xfe, 0x13, 0x37}, DestConnectionID: protocol.ConnectionID{0xca, 0xfe, 0x13, 0x37},
SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad}, SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad},
Version: 0xfeed, Version: 0xfeed,
}).Log(logger) }).Log(logger)
Expect(buf.String()).To(ContainSubstring("Long Header{Type: Initial, DestConnectionID: 0xcafe1337, SrcConnectionID: 0xdecafbad, Token: 0xdeadbeef, PacketNumber: 0x42, PacketNumberLen: 2, PayloadLen: 100, Version: 0xfeed}")) Expect(buf.String()).To(ContainSubstring("Long Header{Type: Initial, DestConnectionID: 0xcafe1337, SrcConnectionID: 0xdecafbad, Token: 0xdeadbeef, PacketNumber: 0x42, PacketNumberLen: 2, Length: 100, Version: 0xfeed}"))
}) })
It("logs Initial Packets without a Token", func() { It("logs Initial Packets without a Token", func() {
@ -387,12 +387,12 @@ var _ = Describe("Header", func() {
Type: protocol.PacketTypeInitial, Type: protocol.PacketTypeInitial,
PacketNumber: 0x42, PacketNumber: 0x42,
PacketNumberLen: protocol.PacketNumberLen2, PacketNumberLen: protocol.PacketNumberLen2,
PayloadLen: 100, Length: 100,
DestConnectionID: protocol.ConnectionID{0xca, 0xfe, 0x13, 0x37}, DestConnectionID: protocol.ConnectionID{0xca, 0xfe, 0x13, 0x37},
SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad}, SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad},
Version: 0xfeed, Version: 0xfeed,
}).Log(logger) }).Log(logger)
Expect(buf.String()).To(ContainSubstring("Long Header{Type: Initial, DestConnectionID: 0xcafe1337, SrcConnectionID: 0xdecafbad, Token: (empty), PacketNumber: 0x42, PacketNumberLen: 2, PayloadLen: 100, Version: 0xfeed}")) Expect(buf.String()).To(ContainSubstring("Long Header{Type: Initial, DestConnectionID: 0xcafe1337, SrcConnectionID: 0xdecafbad, Token: (empty), PacketNumber: 0x42, PacketNumberLen: 2, Length: 100, Version: 0xfeed}"))
}) })
It("logs Initial Packets with a Token", func() { It("logs Initial Packets with a Token", func() {

View file

@ -216,10 +216,13 @@ func (h *packetHandlerMap) handlePacket(addr net.Addr, data []byte) error {
packetData := data[len(data)-r.Len():] packetData := data[len(data)-r.Len():]
if hdr.IsLongHeader { if hdr.IsLongHeader {
if protocol.ByteCount(len(packetData)) < hdr.PayloadLen { if hdr.Length < protocol.ByteCount(hdr.PacketNumberLen) {
return fmt.Errorf("packet payload (%d bytes) is smaller than the expected payload length (%d bytes)", len(packetData), hdr.PayloadLen) return fmt.Errorf("packet length (%d bytes) shorter than packet number (%d bytes)", hdr.Length, hdr.PacketNumberLen)
} }
packetData = packetData[:int(hdr.PayloadLen)] if protocol.ByteCount(len(packetData))+protocol.ByteCount(hdr.PacketNumberLen) < hdr.Length {
return fmt.Errorf("packet length (%d bytes) is smaller than the expected length (%d bytes)", len(packetData)+int(hdr.PacketNumberLen), hdr.Length)
}
packetData = packetData[:int(hdr.Length)-int(hdr.PacketNumberLen)]
// TODO(#1312): implement parsing of compound packets // TODO(#1312): implement parsing of compound packets
} }

View file

@ -26,6 +26,7 @@ var _ = Describe("Packet Handler Map", func() {
Type: protocol.PacketTypeHandshake, Type: protocol.PacketTypeHandshake,
DestConnectionID: connID, DestConnectionID: connID,
PacketNumberLen: protocol.PacketNumberLen1, PacketNumberLen: protocol.PacketNumberLen1,
Length: 1,
Version: protocol.VersionWhatever, Version: protocol.VersionWhatever,
}).Write(buf, protocol.PerspectiveServer, protocol.VersionWhatever) }).Write(buf, protocol.PerspectiveServer, protocol.VersionWhatever)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
@ -124,7 +125,7 @@ var _ = Describe("Packet Handler Map", func() {
Expect(err).To(MatchError("received a packet with an unexpected connection ID 0x0102030405060708")) Expect(err).To(MatchError("received a packet with an unexpected connection ID 0x0102030405060708"))
}) })
It("errors on packets that are smaller than the Payload Length in the packet header", func() { It("errors on packets that are smaller than the length in the packet header", func() {
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8} connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
packetHandler := NewMockPacketHandler(mockCtrl) packetHandler := NewMockPacketHandler(mockCtrl)
packetHandler.EXPECT().GetVersion().Return(protocol.VersionWhatever) packetHandler.EXPECT().GetVersion().Return(protocol.VersionWhatever)
@ -133,33 +134,52 @@ var _ = Describe("Packet Handler Map", func() {
hdr := &wire.Header{ hdr := &wire.Header{
IsLongHeader: true, IsLongHeader: true,
Type: protocol.PacketTypeHandshake, Type: protocol.PacketTypeHandshake,
PayloadLen: 1000, Length: 1000,
DestConnectionID: connID, DestConnectionID: connID,
PacketNumberLen: protocol.PacketNumberLen1, PacketNumberLen: protocol.PacketNumberLen2,
Version: protocol.VersionWhatever, Version: protocol.VersionWhatever,
} }
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
Expect(hdr.Write(buf, protocol.PerspectiveServer, protocol.VersionWhatever)).To(Succeed()) Expect(hdr.Write(buf, protocol.PerspectiveServer, protocol.VersionWhatever)).To(Succeed())
buf.Write(bytes.Repeat([]byte{0}, 500)) buf.Write(bytes.Repeat([]byte{0}, 500-2 /* for packet number length */))
err := handler.handlePacket(nil, buf.Bytes()) err := handler.handlePacket(nil, buf.Bytes())
Expect(err).To(MatchError("packet payload (500 bytes) is smaller than the expected payload length (1000 bytes)")) Expect(err).To(MatchError("packet length (500 bytes) is smaller than the expected length (1000 bytes)"))
}) })
It("cuts packets at the Payload Length", func() { It("errors when receiving a packet that has a length smaller than the packet number length", func() {
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
packetHandler := NewMockPacketHandler(mockCtrl)
packetHandler.EXPECT().GetVersion().Return(protocol.VersionWhatever)
packetHandler.EXPECT().GetPerspective().Return(protocol.PerspectiveClient)
handler.Add(connID, packetHandler)
hdr := &wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
Length: 3,
DestConnectionID: connID,
PacketNumberLen: protocol.PacketNumberLen4,
Version: protocol.VersionWhatever,
}
buf := &bytes.Buffer{}
Expect(hdr.Write(buf, protocol.PerspectiveServer, protocol.VersionWhatever)).To(Succeed())
Expect(handler.handlePacket(nil, buf.Bytes())).To(MatchError("packet length (3 bytes) shorter than packet number (4 bytes)"))
})
It("cuts packets to the right length", func() {
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8} connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
packetHandler := NewMockPacketHandler(mockCtrl) packetHandler := NewMockPacketHandler(mockCtrl)
packetHandler.EXPECT().GetVersion().Return(protocol.VersionWhatever) packetHandler.EXPECT().GetVersion().Return(protocol.VersionWhatever)
packetHandler.EXPECT().GetPerspective().Return(protocol.PerspectiveClient) packetHandler.EXPECT().GetPerspective().Return(protocol.PerspectiveClient)
handler.Add(connID, packetHandler) handler.Add(connID, packetHandler)
packetHandler.EXPECT().handlePacket(gomock.Any()).Do(func(p *receivedPacket) { packetHandler.EXPECT().handlePacket(gomock.Any()).Do(func(p *receivedPacket) {
Expect(p.data).To(HaveLen(456)) Expect(p.data).To(HaveLen(456 - int(p.header.PacketNumberLen)))
}) })
hdr := &wire.Header{ hdr := &wire.Header{
IsLongHeader: true, IsLongHeader: true,
Type: protocol.PacketTypeHandshake, Type: protocol.PacketTypeHandshake,
PayloadLen: 456, Length: 456,
DestConnectionID: connID, DestConnectionID: connID,
PacketNumberLen: protocol.PacketNumberLen1, PacketNumberLen: protocol.PacketNumberLen1,
Version: protocol.VersionWhatever, Version: protocol.VersionWhatever,
@ -167,8 +187,7 @@ var _ = Describe("Packet Handler Map", func() {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
Expect(hdr.Write(buf, protocol.PerspectiveServer, protocol.VersionWhatever)).To(Succeed()) Expect(hdr.Write(buf, protocol.PerspectiveServer, protocol.VersionWhatever)).To(Succeed())
buf.Write(bytes.Repeat([]byte{0}, 500)) buf.Write(bytes.Repeat([]byte{0}, 500))
err := handler.handlePacket(nil, buf.Bytes()) Expect(handler.handlePacket(nil, buf.Bytes())).To(Succeed())
Expect(err).ToNot(HaveOccurred())
}) })
It("closes the packet handlers when reading from the conn fails", func() { It("closes the packet handlers when reading from the conn fails", func() {

View file

@ -409,9 +409,9 @@ func (p *packetPacker) getHeader(encLevel protocol.EncryptionLevel) *wire.Header
if encLevel != protocol.Encryption1RTT { if encLevel != protocol.Encryption1RTT {
header.IsLongHeader = true header.IsLongHeader = true
header.SrcConnectionID = p.srcConnID header.SrcConnectionID = p.srcConnID
// Set the payload len to maximum size. // Set the length to the maximum packet size.
// Since it is encoded as a varint, this guarantees us that the header will end up at most as big as GetLength() returns. // Since it is encoded as a varint, this guarantees us that the header will end up at most as big as GetLength() returns.
header.PayloadLen = p.maxPacketSize header.Length = p.maxPacketSize
switch encLevel { switch encLevel {
case protocol.EncryptionInitial: case protocol.EncryptionInitial:
header.Type = protocol.PacketTypeInitial header.Type = protocol.PacketTypeInitial
@ -433,20 +433,20 @@ func (p *packetPacker) writeAndSealPacket(
addPadding := p.perspective == protocol.PerspectiveClient && header.Type == protocol.PacketTypeInitial && !p.hasSentPacket addPadding := p.perspective == protocol.PerspectiveClient && header.Type == protocol.PacketTypeInitial && !p.hasSentPacket
// the payload length is only needed for Long Headers // the length is only needed for Long Headers
if header.IsLongHeader { if header.IsLongHeader {
if p.perspective == protocol.PerspectiveClient && header.Type == protocol.PacketTypeInitial { if p.perspective == protocol.PerspectiveClient && header.Type == protocol.PacketTypeInitial {
header.Token = p.token header.Token = p.token
} }
if addPadding { if addPadding {
headerLen := header.GetLength(p.version) headerLen := header.GetLength(p.version)
header.PayloadLen = protocol.ByteCount(protocol.MinInitialPacketSize) - headerLen header.Length = protocol.ByteCount(header.PacketNumberLen) + protocol.MinInitialPacketSize - headerLen
} else { } else {
payloadLen := protocol.ByteCount(sealer.Overhead()) length := protocol.ByteCount(sealer.Overhead()) + protocol.ByteCount(header.PacketNumberLen)
for _, frame := range frames { for _, frame := range frames {
payloadLen += frame.Length(p.version) length += frame.Length(p.version)
} }
header.PayloadLen = payloadLen header.Length = length
} }
} }

View file

@ -30,13 +30,13 @@ var _ = Describe("Packet packer", func() {
token []byte token []byte
) )
checkPayloadLen := func(data []byte) { checkLength := func(data []byte) {
r := bytes.NewReader(data) r := bytes.NewReader(data)
iHdr, err := wire.ParseInvariantHeader(r, 0) iHdr, err := wire.ParseInvariantHeader(r, 0)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
hdr, err := iHdr.Parse(r, protocol.PerspectiveServer, protocol.VersionWhatever) hdr, err := iHdr.Parse(r, protocol.PerspectiveServer, protocol.VersionWhatever)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
ExpectWithOffset(0, hdr.PayloadLen).To(BeEquivalentTo(r.Len())) ExpectWithOffset(0, hdr.Length).To(BeEquivalentTo(r.Len() + int(hdr.PacketNumberLen)))
} }
expectAppendStreamFrames := func(frames ...wire.Frame) { expectAppendStreamFrames := func(frames ...wire.Frame) {
@ -665,7 +665,7 @@ var _ = Describe("Packet packer", func() {
}) })
Context("packing crypto packets", func() { Context("packing crypto packets", func() {
It("sets the payload length", func() { It("sets the length", func() {
pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2)
pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42))
f := &wire.CryptoFrame{ f := &wire.CryptoFrame{
@ -678,7 +678,7 @@ var _ = Describe("Packet packer", func() {
sealingManager.EXPECT().GetSealerWithEncryptionLevel(protocol.EncryptionInitial).Return(sealer, nil) sealingManager.EXPECT().GetSealerWithEncryptionLevel(protocol.EncryptionInitial).Return(sealer, nil)
p, err := packer.PackPacket() p, err := packer.PackPacket()
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
checkPayloadLen(p.raw) checkLength(p.raw)
}) })
It("packs a maximum size crypto packet", func() { It("packs a maximum size crypto packet", func() {
@ -701,7 +701,7 @@ var _ = Describe("Packet packer", func() {
expectedPacketLen := packer.maxPacketSize expectedPacketLen := packer.maxPacketSize
Expect(p.raw).To(HaveLen(int(expectedPacketLen))) Expect(p.raw).To(HaveLen(int(expectedPacketLen)))
Expect(p.header.IsLongHeader).To(BeTrue()) Expect(p.header.IsLongHeader).To(BeTrue())
checkPayloadLen(p.raw) checkLength(p.raw)
}) })
It("pads Initial packets to the required minimum packet size", func() { It("pads Initial packets to the required minimum packet size", func() {
@ -723,7 +723,7 @@ var _ = Describe("Packet packer", func() {
Expect(cf.Data).To(Equal([]byte("foobar"))) Expect(cf.Data).To(Equal([]byte("foobar")))
}) })
It("sets the correct payload length for an Initial packet", func() { It("sets the correct length for an Initial packet", func() {
pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2)
pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42))
sealingManager.EXPECT().GetSealerWithEncryptionLevel(protocol.EncryptionInitial).Return(sealer, nil) sealingManager.EXPECT().GetSealerWithEncryptionLevel(protocol.EncryptionInitial).Return(sealer, nil)
@ -736,7 +736,7 @@ var _ = Describe("Packet packer", func() {
packer.perspective = protocol.PerspectiveClient packer.perspective = protocol.PerspectiveClient
packet, err := packer.PackPacket() packet, err := packer.PackPacket()
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
checkPayloadLen(packet.raw) checkLength(packet.raw)
}) })
It("adds an ACK frame", func() { It("adds an ACK frame", func() {