add version number to Frame.Write

This commit is contained in:
Lucas Clemente 2016-05-09 19:50:15 +02:00
parent a219b72968
commit 14b16a429c
21 changed files with 48 additions and 46 deletions

View file

@ -21,7 +21,7 @@ type AckFrame struct {
}
// Write writes an ACK frame.
func (f *AckFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen) error {
func (f *AckFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen, version protocol.VersionNumber) error {
typeByte := uint8(0x40 | 0x0C)
if f.HasNACK() {

View file

@ -238,7 +238,7 @@ var _ = Describe("AckFrame", func() {
Entropy: 2,
LargestObserved: 1,
}
err := frame.Write(b, 1, protocol.PacketNumberLen6)
err := frame.Write(b, 1, protocol.PacketNumberLen6, 32)
Expect(err).ToNot(HaveOccurred())
Expect(b.Bytes()).To(Equal([]byte{0x4c, 0x02, 0x01, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}))
})
@ -249,7 +249,7 @@ var _ = Describe("AckFrame", func() {
LargestObserved: 4,
NackRanges: []NackRange{NackRange{FirstPacketNumber: 2, LastPacketNumber: 2}},
}
err := frame.Write(b, 1, protocol.PacketNumberLen6)
err := frame.Write(b, 1, protocol.PacketNumberLen6, 32)
Expect(err).ToNot(HaveOccurred())
missingPacketBytes := b.Bytes()[b.Len()-8:]
Expect(missingPacketBytes[0]).To(Equal(uint8(1))) // numRanges
@ -267,7 +267,7 @@ var _ = Describe("AckFrame", func() {
LargestObserved: 7,
NackRanges: []NackRange{nackRange1, nackRange2},
}
err := frame.Write(b, 1, protocol.PacketNumberLen6)
err := frame.Write(b, 1, protocol.PacketNumberLen6, 32)
Expect(err).ToNot(HaveOccurred())
missingPacketBytes := b.Bytes()[b.Len()-(1+2*7):]
Expect(missingPacketBytes[0]).To(Equal(uint8(2))) // numRanges
@ -288,7 +288,7 @@ var _ = Describe("AckFrame", func() {
LargestObserved: 258,
NackRanges: []NackRange{NackRange{FirstPacketNumber: 2, LastPacketNumber: 257}},
}
err := frame.Write(b, 1, protocol.PacketNumberLen6)
err := frame.Write(b, 1, protocol.PacketNumberLen6, 32)
Expect(err).ToNot(HaveOccurred())
missingPacketBytes := b.Bytes()[b.Len()-(1+7):]
Expect(missingPacketBytes[0]).To(Equal(uint8(1))) // numRanges
@ -302,7 +302,7 @@ var _ = Describe("AckFrame", func() {
LargestObserved: 302,
NackRanges: []NackRange{NackRange{FirstPacketNumber: 2, LastPacketNumber: 301}},
}
err := frame.Write(b, 1, protocol.PacketNumberLen6)
err := frame.Write(b, 1, protocol.PacketNumberLen6, 32)
Expect(err).ToNot(HaveOccurred())
missingPacketBytes := b.Bytes()[b.Len()-(1+2*7):]
Expect(missingPacketBytes[0]).To(Equal(uint8(2))) // numRanges
@ -318,7 +318,7 @@ var _ = Describe("AckFrame", func() {
LargestObserved: 259,
NackRanges: []NackRange{NackRange{FirstPacketNumber: 2, LastPacketNumber: 258}},
}
err := frame.Write(b, 1, protocol.PacketNumberLen6)
err := frame.Write(b, 1, protocol.PacketNumberLen6, 32)
Expect(err).ToNot(HaveOccurred())
missingPacketBytes := b.Bytes()[b.Len()-(1+2*7):]
Expect(missingPacketBytes[0]).To(Equal(uint8(2))) // numRanges
@ -334,7 +334,7 @@ var _ = Describe("AckFrame", func() {
LargestObserved: 603,
NackRanges: []NackRange{NackRange{FirstPacketNumber: 2, LastPacketNumber: 601}},
}
err := frame.Write(b, 1, protocol.PacketNumberLen6)
err := frame.Write(b, 1, protocol.PacketNumberLen6, 32)
Expect(err).ToNot(HaveOccurred())
missingPacketBytes := b.Bytes()[b.Len()-(1+3*7):]
Expect(missingPacketBytes[0]).To(Equal(uint8(3))) // numRanges
@ -354,7 +354,7 @@ var _ = Describe("AckFrame", func() {
LargestObserved: 655,
NackRanges: []NackRange{nackRange2, nackRange1},
}
err := frame.Write(b, 1, protocol.PacketNumberLen6)
err := frame.Write(b, 1, protocol.PacketNumberLen6, 32)
Expect(err).ToNot(HaveOccurred())
missingPacketBytes := b.Bytes()[b.Len()-(1+4*7):]
Expect(missingPacketBytes[0]).To(Equal(uint8(4))) // numRanges
@ -375,7 +375,7 @@ var _ = Describe("AckFrame", func() {
Entropy: 2,
LargestObserved: 1,
}
f.Write(b, 1, protocol.PacketNumberLen6)
f.Write(b, 1, protocol.PacketNumberLen6, 32)
Expect(f.MinLength()).To(Equal(b.Len()))
})
@ -385,7 +385,7 @@ var _ = Describe("AckFrame", func() {
LargestObserved: 4,
NackRanges: []NackRange{NackRange{FirstPacketNumber: 2, LastPacketNumber: 2}},
}
err := f.Write(b, 1, protocol.PacketNumberLen6)
err := f.Write(b, 1, protocol.PacketNumberLen6, 32)
Expect(err).ToNot(HaveOccurred())
Expect(f.MinLength()).To(Equal(b.Len()))
})
@ -403,7 +403,7 @@ var _ = Describe("AckFrame", func() {
Entropy: 0xDE,
LargestObserved: 6789,
}
err := frameOrig.Write(b, 1, protocol.PacketNumberLen6)
err := frameOrig.Write(b, 1, protocol.PacketNumberLen6, 32)
Expect(err).ToNot(HaveOccurred())
frame, err := ParseAckFrame(bytes.NewReader(b.Bytes()))
Expect(err).ToNot(HaveOccurred())
@ -421,7 +421,7 @@ var _ = Describe("AckFrame", func() {
LargestObserved: 15,
NackRanges: nackRanges,
}
err := frameOrig.Write(b, 1, protocol.PacketNumberLen6)
err := frameOrig.Write(b, 1, protocol.PacketNumberLen6, 32)
Expect(err).ToNot(HaveOccurred())
r := bytes.NewReader(b.Bytes())
frame, err := ParseAckFrame(r)
@ -441,7 +441,7 @@ var _ = Describe("AckFrame", func() {
LargestObserved: 1600,
NackRanges: nackRanges,
}
err := frameOrig.Write(b, 1, protocol.PacketNumberLen6)
err := frameOrig.Write(b, 1, protocol.PacketNumberLen6, 32)
Expect(err).ToNot(HaveOccurred())
r := bytes.NewReader(b.Bytes())
frame, err := ParseAckFrame(r)

View file

@ -13,7 +13,7 @@ type BlockedFrame struct {
}
//Write writes a RST_STREAM frame
func (f *BlockedFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen) error {
func (f *BlockedFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen, version protocol.VersionNumber) error {
b.WriteByte(0x05)
if f.StreamID == 0 {

View file

@ -22,7 +22,7 @@ var _ = Describe("BlockedFrame", func() {
It("writes a sample frame", func() {
b := &bytes.Buffer{}
frame := BlockedFrame{StreamID: 0x1337}
frame.Write(b, 10, protocol.PacketNumberLen6)
frame.Write(b, 10, protocol.PacketNumberLen6, 0)
Expect(b.Bytes()).To(Equal([]byte{0x05, 0x37, 0x13, 0x0, 0x0}))
})

View file

@ -52,7 +52,7 @@ func (f *ConnectionCloseFrame) MinLength() int {
}
// Write writes an CONNECTION_CLOSE frame.
func (f *ConnectionCloseFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen) error {
func (f *ConnectionCloseFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen, version protocol.VersionNumber) error {
b.WriteByte(0x02)
utils.WriteUint32(b, uint32(f.ErrorCode))

View file

@ -35,7 +35,7 @@ var _ = Describe("ConnectionCloseFrame", func() {
frame := &ConnectionCloseFrame{
ErrorCode: 0xDEADBEEF,
}
err := frame.Write(b, 1, protocol.PacketNumberLen6)
err := frame.Write(b, 1, protocol.PacketNumberLen6, 0)
Expect(err).ToNot(HaveOccurred())
Expect(b.Len()).To(Equal(1 + 2 + 4))
Expect(b.Bytes()).To(Equal([]byte{0x02, 0xEF, 0xBE, 0xAD, 0xDE, 0x00, 0x00}))
@ -47,7 +47,7 @@ var _ = Describe("ConnectionCloseFrame", func() {
ErrorCode: 0xDEADBEEF,
ReasonPhrase: "foobar",
}
err := frame.Write(b, 1, protocol.PacketNumberLen6)
err := frame.Write(b, 1, protocol.PacketNumberLen6, 0)
Expect(err).ToNot(HaveOccurred())
Expect(b.Len()).To(Equal(1 + 2 + 4 + len(frame.ReasonPhrase)))
Expect(b.Bytes()[:5]).To(Equal([]byte{0x02, 0xEF, 0xBE, 0xAD, 0xDE}))
@ -67,7 +67,7 @@ var _ = Describe("ConnectionCloseFrame", func() {
ErrorCode: 0xDEADBEEF,
ReasonPhrase: reasonPhrase,
}
err := frame.Write(b, 1, protocol.PacketNumberLen6)
err := frame.Write(b, 1, protocol.PacketNumberLen6, 0)
Expect(err).To(HaveOccurred())
})
@ -77,7 +77,7 @@ var _ = Describe("ConnectionCloseFrame", func() {
ErrorCode: 0xDEADBEEF,
ReasonPhrase: "foobar",
}
f.Write(b, 1, protocol.PacketNumberLen6)
f.Write(b, 1, protocol.PacketNumberLen6, 0)
Expect(f.MinLength()).To(Equal(b.Len()))
})
})
@ -88,7 +88,7 @@ var _ = Describe("ConnectionCloseFrame", func() {
ErrorCode: 0xDEADBEEF,
ReasonPhrase: "Lorem ipsum dolor sit amet.",
}
err := frame.Write(b, 1, protocol.PacketNumberLen6)
err := frame.Write(b, 1, protocol.PacketNumberLen6, 0)
Expect(err).ToNot(HaveOccurred())
readframe, err := ParseConnectionCloseFrame(bytes.NewReader(b.Bytes()))
Expect(err).ToNot(HaveOccurred())

View file

@ -8,6 +8,6 @@ import (
// A Frame in QUIC
type Frame interface {
Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen) error
Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen, version protocol.VersionNumber) error
MinLength() int
}

View file

@ -21,7 +21,7 @@ func ParsePingFrame(r *bytes.Reader) (*PingFrame, error) {
return frame, nil
}
func (f *PingFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen) error {
func (f *PingFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen, version protocol.VersionNumber) error {
typeByte := uint8(0x07)
b.WriteByte(typeByte)
return nil

View file

@ -22,7 +22,7 @@ var _ = Describe("PingFrame", func() {
It("writes a sample frame", func() {
b := &bytes.Buffer{}
frame := PingFrame{}
frame.Write(b, 10, protocol.PacketNumberLen6)
frame.Write(b, 10, protocol.PacketNumberLen6, 0)
Expect(b.Bytes()).To(Equal([]byte{0x07}))
})

View file

@ -15,7 +15,7 @@ type RstStreamFrame struct {
}
//Write writes a RST_STREAM frame
func (f *RstStreamFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen) error {
func (f *RstStreamFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen, version protocol.VersionNumber) error {
panic("RstStreamFrame: Write not yet implemented")
}

View file

@ -14,7 +14,7 @@ type StopWaitingFrame struct {
LeastUnacked protocol.PacketNumber
}
func (f *StopWaitingFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen) error {
func (f *StopWaitingFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen, version protocol.VersionNumber) error {
// packetNumber is the packet number of the packet that this StopWaitingFrame will be sent with
typeByte := uint8(0x06)
b.WriteByte(typeByte)

View file

@ -34,7 +34,7 @@ var _ = Describe("StopWaitingFrame", func() {
LeastUnacked: 10,
Entropy: 0xE,
}
frame.Write(b, packetNumber, protocol.PacketNumberLen6)
frame.Write(b, packetNumber, protocol.PacketNumberLen6, 0)
Expect(b.Bytes()[0]).To(Equal(uint8(0x06)))
// todo: check more
})
@ -48,7 +48,7 @@ var _ = Describe("StopWaitingFrame", func() {
Entropy: 0xE,
}
b := &bytes.Buffer{}
frame.Write(b, packetNumber, protocol.PacketNumberLen6)
frame.Write(b, packetNumber, protocol.PacketNumberLen6, 0)
readframe, err := ParseStopWaitingFrame(bytes.NewReader(b.Bytes()), packetNumber, 6)
Expect(err).ToNot(HaveOccurred())
Expect(readframe.Entropy).To(Equal(frame.Entropy))

View file

@ -70,7 +70,7 @@ func ParseStreamFrame(r *bytes.Reader) (*StreamFrame, error) {
}
// WriteStreamFrame writes a stream frame.
func (f *StreamFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen) error {
func (f *StreamFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen, version protocol.VersionNumber) error {
typeByte := uint8(0x80)
if f.FinBit {
typeByte ^= 0x40

View file

@ -37,7 +37,7 @@ var _ = Describe("StreamFrame", func() {
(&StreamFrame{
StreamID: 1,
Data: []byte("foobar"),
}).Write(b, 1, protocol.PacketNumberLen6)
}).Write(b, 1, protocol.PacketNumberLen6, 0)
Expect(b.Bytes()).To(Equal([]byte{0xa3, 0x1, 0, 0, 0, 0x06, 0x00, 'f', 'o', 'o', 'b', 'a', 'r'}))
})
@ -47,7 +47,7 @@ var _ = Describe("StreamFrame", func() {
StreamID: 1,
Offset: 16,
Data: []byte("foobar"),
}).Write(b, 1, protocol.PacketNumberLen6)
}).Write(b, 1, protocol.PacketNumberLen6, 0)
Expect(b.Bytes()).To(Equal([]byte{0xbf, 0x1, 0, 0, 0, 0x10, 0, 0, 0, 0, 0, 0, 0, 0x06, 0x00, 'f', 'o', 'o', 'b', 'a', 'r'}))
})
@ -58,7 +58,7 @@ var _ = Describe("StreamFrame", func() {
Data: []byte("f"),
Offset: 1,
}
f.Write(b, 1, protocol.PacketNumberLen6)
f.Write(b, 1, protocol.PacketNumberLen6, 0)
Expect(f.MinLength()).To(Equal(b.Len()))
})
})

View file

@ -14,7 +14,7 @@ type WindowUpdateFrame struct {
}
//Write writes a RST_STREAM frame
func (f *WindowUpdateFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen) error {
func (f *WindowUpdateFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen, version protocol.VersionNumber) error {
panic("WindowUpdateFrame: Write not yet implemented")
}

View file

@ -20,6 +20,8 @@ type packedPacket struct {
type packetPacker struct {
connectionID protocol.ConnectionID
version protocol.VersionNumber
aead crypto.AEAD
connectionParametersManager *handshake.ConnectionParametersManager
@ -95,7 +97,7 @@ func (p *packetPacker) getPayload(frames []frames.Frame, currentPacketNumber pro
var payload bytes.Buffer
payload.WriteByte(0) // The entropy bit is set in sendPayload
for _, frame := range frames {
frame.Write(&payload, currentPacketNumber, 6)
frame.Write(&payload, currentPacketNumber, 6, p.version)
}
return payload.Bytes(), nil
}

View file

@ -37,7 +37,7 @@ var _ = Describe("Packet packer", func() {
Expect(p).ToNot(BeNil())
Expect(err).ToNot(HaveOccurred())
b := &bytes.Buffer{}
f.Write(b, 1, 6)
f.Write(b, 1, 6, 0)
Expect(len(p.frames)).To(Equal(1))
Expect(p.raw).To(ContainSubstring(string(b.Bytes())))
})
@ -80,7 +80,7 @@ var _ = Describe("Packet packer", func() {
It("packs many control frames into 1 packets", func() {
f := &frames.AckFrame{LargestObserved: 1}
b := &bytes.Buffer{}
f.Write(b, 3, 6)
f.Write(b, 3, 6, 32)
maxFramesPerPacket := protocol.MaxFrameSize / b.Len()
var controlFrames []frames.Frame
for i := 0; i < maxFramesPerPacket; i++ {
@ -146,8 +146,8 @@ var _ = Describe("Packet packer", func() {
Expect(p).ToNot(BeNil())
Expect(err).ToNot(HaveOccurred())
b := &bytes.Buffer{}
f1.Write(b, 2, 6)
f2.Write(b, 2, 6)
f1.Write(b, 2, 6, 0)
f2.Write(b, 2, 6, 0)
Expect(len(p.frames)).To(Equal(2))
Expect(p.raw).To(ContainSubstring(string(b.Bytes())))
})

View file

@ -50,7 +50,7 @@ var _ = Describe("Packet unpacker", func() {
StreamID: 1,
Data: []byte("foobar"),
}
err := f.Write(buf, 3, 6)
err := f.Write(buf, 3, 6, 0)
Expect(err).ToNot(HaveOccurred())
setReader(buf.Bytes())
packet, err := unpacker.Unpack(hdrBin, hdr, r)
@ -63,7 +63,7 @@ var _ = Describe("Packet unpacker", func() {
LargestObserved: 1,
DelayTime: time.Microsecond,
}
err := f.Write(buf, 3, 6)
err := f.Write(buf, 3, 6, 32)
Expect(err).ToNot(HaveOccurred())
setReader(buf.Bytes())
packet, err := unpacker.Unpack(hdrBin, hdr, r)
@ -99,7 +99,7 @@ var _ = Describe("Packet unpacker", func() {
It("unpacks CONNECTION_CLOSE frames", func() {
f := &frames.ConnectionCloseFrame{ReasonPhrase: "foo"}
err := f.Write(buf, 6, 6)
err := f.Write(buf, 6, 6, 0)
Expect(err).ToNot(HaveOccurred())
setReader(buf.Bytes())
packet, err := unpacker.Unpack(hdrBin, hdr, r)

View file

@ -11,7 +11,7 @@ type VersionNumber int
// SupportedVersions lists the versions that the server supports
var SupportedVersions = []VersionNumber{
32,
31, 32,
}
// SupportedVersionsAsTags is needed for the SHLO crypto message

View file

@ -19,7 +19,7 @@ var _ = Describe("Version", func() {
})
It("has proper tag list", func() {
Expect(protocol.SupportedVersionsAsTags).To(Equal([]byte("Q032")))
Expect(protocol.SupportedVersionsAsTags).To(Equal([]byte("Q031Q032")))
})
It("recognizes supported versions", func() {

View file

@ -95,7 +95,7 @@ func NewSession(conn connection, v protocol.VersionNumber, connectionID protocol
}
}()
session.packer = &packetPacker{aead: cryptoSetup, connectionParametersManager: session.connectionParametersManager, connectionID: connectionID}
session.packer = &packetPacker{aead: cryptoSetup, connectionParametersManager: session.connectionParametersManager, connectionID: connectionID, version: v}
session.unpacker = &packetUnpacker{aead: cryptoSetup}
session.congestion = congestion.NewCubicSender(