diff --git a/internal/ackhandler/interfaces.go b/internal/ackhandler/interfaces.go index 8545a93c..52599355 100644 --- a/internal/ackhandler/interfaces.go +++ b/internal/ackhandler/interfaces.go @@ -30,6 +30,9 @@ type SentPacketHandler interface { GetLowestPacketNotConfirmedAcked() protocol.PacketNumber DequeuePacketForRetransmission() *Packet DequeueProbePacket() (*Packet, error) + + PeekPacketNumber() protocol.PacketNumber + PopPacketNumber() protocol.PacketNumber GetPacketNumberLen(protocol.PacketNumber) protocol.PacketNumberLen GetAlarmTimeout() time.Time diff --git a/packet_number_generator.go b/internal/ackhandler/packet_number_generator.go similarity index 98% rename from packet_number_generator.go rename to internal/ackhandler/packet_number_generator.go index e06d3dba..ddd4a563 100644 --- a/packet_number_generator.go +++ b/internal/ackhandler/packet_number_generator.go @@ -1,4 +1,4 @@ -package quic +package ackhandler import ( "crypto/rand" diff --git a/packet_number_generator_test.go b/internal/ackhandler/packet_number_generator_test.go similarity index 99% rename from packet_number_generator_test.go rename to internal/ackhandler/packet_number_generator_test.go index 7a2a42a6..73bd914f 100644 --- a/packet_number_generator_test.go +++ b/internal/ackhandler/packet_number_generator_test.go @@ -1,4 +1,4 @@ -package quic +package ackhandler import ( "math" diff --git a/internal/ackhandler/sent_packet_handler.go b/internal/ackhandler/sent_packet_handler.go index 356c8581..43af8c50 100644 --- a/internal/ackhandler/sent_packet_handler.go +++ b/internal/ackhandler/sent_packet_handler.go @@ -30,7 +30,9 @@ const ( ) type sentPacketHandler struct { - lastSentPacketNumber protocol.PacketNumber + lastSentPacketNumber protocol.PacketNumber + packetNumberGenerator *packetNumberGenerator + lastSentRetransmittablePacketTime time.Time lastSentHandshakePacketTime time.Time @@ -89,11 +91,12 @@ func NewSentPacketHandler(rttStats *congestion.RTTStats, logger utils.Logger, ve ) return &sentPacketHandler{ - packetHistory: newSentPacketHistory(), - rttStats: rttStats, - congestion: congestion, - logger: logger, - version: version, + packetNumberGenerator: newPacketNumberGenerator(1, protocol.SkipPacketAveragePeriodLength), + packetHistory: newSentPacketHistory(), + rttStats: rttStats, + congestion: congestion, + logger: logger, + version: version, } } @@ -518,6 +521,14 @@ func (h *sentPacketHandler) DequeueProbePacket() (*Packet, error) { return h.DequeuePacketForRetransmission(), nil } +func (h *sentPacketHandler) PeekPacketNumber() protocol.PacketNumber { + return h.packetNumberGenerator.Peek() +} + +func (h *sentPacketHandler) PopPacketNumber() protocol.PacketNumber { + return h.packetNumberGenerator.Pop() +} + func (h *sentPacketHandler) GetPacketNumberLen(p protocol.PacketNumber) protocol.PacketNumberLen { return protocol.GetPacketNumberLengthForHeader(p, h.lowestUnacked(), h.version) } diff --git a/internal/mocks/ackhandler/sent_packet_handler.go b/internal/mocks/ackhandler/sent_packet_handler.go index 45d4b000..481837f0 100644 --- a/internal/mocks/ackhandler/sent_packet_handler.go +++ b/internal/mocks/ackhandler/sent_packet_handler.go @@ -110,6 +110,30 @@ func (mr *MockSentPacketHandlerMockRecorder) OnAlarm() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OnAlarm", reflect.TypeOf((*MockSentPacketHandler)(nil).OnAlarm)) } +// PeekPacketNumber mocks base method +func (m *MockSentPacketHandler) PeekPacketNumber() protocol.PacketNumber { + ret := m.ctrl.Call(m, "PeekPacketNumber") + ret0, _ := ret[0].(protocol.PacketNumber) + return ret0 +} + +// PeekPacketNumber indicates an expected call of PeekPacketNumber +func (mr *MockSentPacketHandlerMockRecorder) PeekPacketNumber() *gomock.Call { + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PeekPacketNumber", reflect.TypeOf((*MockSentPacketHandler)(nil).PeekPacketNumber)) +} + +// PopPacketNumber mocks base method +func (m *MockSentPacketHandler) PopPacketNumber() protocol.PacketNumber { + ret := m.ctrl.Call(m, "PopPacketNumber") + ret0, _ := ret[0].(protocol.PacketNumber) + return ret0 +} + +// PopPacketNumber indicates an expected call of PopPacketNumber +func (mr *MockSentPacketHandlerMockRecorder) PopPacketNumber() *gomock.Call { + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PopPacketNumber", reflect.TypeOf((*MockSentPacketHandler)(nil).PopPacketNumber)) +} + // ReceivedAck mocks base method func (m *MockSentPacketHandler) ReceivedAck(arg0 *wire.AckFrame, arg1 protocol.PacketNumber, arg2 protocol.EncryptionLevel, arg3 time.Time) error { ret := m.ctrl.Call(m, "ReceivedAck", arg0, arg1, arg2, arg3) diff --git a/packet_packer.go b/packet_packer.go index cadcac54..338cc37a 100644 --- a/packet_packer.go +++ b/packet_packer.go @@ -59,6 +59,12 @@ func getMaxPacketSize(addr net.Addr) protocol.ByteCount { return maxSize } +type packetNumberManager interface { + PeekPacketNumber() protocol.PacketNumber + PopPacketNumber() protocol.PacketNumber + GetPacketNumberLen(protocol.PacketNumber) protocol.PacketNumberLen +} + type sealingManager interface { GetSealer() (protocol.EncryptionLevel, handshake.Sealer) GetSealerWithEncryptionLevel(protocol.EncryptionLevel) (handshake.Sealer, error) @@ -86,10 +92,9 @@ type packetPacker struct { token []byte - packetNumberGenerator *packetNumberGenerator - getPacketNumberLen func(protocol.PacketNumber) protocol.PacketNumberLen - framer frameSource - acks ackFrameSource + pnManager packetNumberManager + framer frameSource + acks ackFrameSource maxPacketSize protocol.ByteCount hasSentPacket bool // has the packetPacker already sent a packet @@ -103,7 +108,7 @@ func newPacketPacker( srcConnID protocol.ConnectionID, initialStream cryptoStream, handshakeStream cryptoStream, - getPacketNumberLen func(protocol.PacketNumber) protocol.PacketNumberLen, + packetNumberManager packetNumberManager, remoteAddr net.Addr, // only used for determining the max packet size token []byte, cryptoSetup sealingManager, @@ -113,19 +118,18 @@ func newPacketPacker( version protocol.VersionNumber, ) *packetPacker { return &packetPacker{ - cryptoSetup: cryptoSetup, - token: token, - destConnID: destConnID, - srcConnID: srcConnID, - initialStream: initialStream, - handshakeStream: handshakeStream, - perspective: perspective, - version: version, - framer: framer, - acks: acks, - getPacketNumberLen: getPacketNumberLen, - packetNumberGenerator: newPacketNumberGenerator(1, protocol.SkipPacketAveragePeriodLength), - maxPacketSize: getMaxPacketSize(remoteAddr), + cryptoSetup: cryptoSetup, + token: token, + destConnID: destConnID, + srcConnID: srcConnID, + initialStream: initialStream, + handshakeStream: handshakeStream, + perspective: perspective, + version: version, + framer: framer, + acks: acks, + pnManager: packetNumberManager, + maxPacketSize: getMaxPacketSize(remoteAddr), } } @@ -395,8 +399,8 @@ func (p *packetPacker) composeNextPacket( } func (p *packetPacker) getHeader(encLevel protocol.EncryptionLevel) *wire.Header { - pnum := p.packetNumberGenerator.Peek() - packetNumberLen := p.getPacketNumberLen(pnum) + pnum := p.pnManager.PeekPacketNumber() + packetNumberLen := p.pnManager.GetPacketNumberLen(pnum) header := &wire.Header{ PacketNumber: pnum, @@ -481,7 +485,7 @@ func (p *packetPacker) writeAndSealPacket( _ = sealer.Seal(raw[payloadStartIndex:payloadStartIndex], raw[payloadStartIndex:], header.PacketNumber, raw[:payloadStartIndex]) raw = raw[0 : buffer.Len()+sealer.Overhead()] - num := p.packetNumberGenerator.Pop() + num := p.pnManager.PopPacketNumber() if num != header.PacketNumber { return nil, errors.New("packetPacker BUG: Peeked and Popped packet numbers do not match") } diff --git a/packet_packer_test.go b/packet_packer_test.go index 29a293f1..8e2c54d2 100644 --- a/packet_packer_test.go +++ b/packet_packer_test.go @@ -9,6 +9,7 @@ import ( "github.com/lucas-clemente/quic-go/internal/ackhandler" "github.com/lucas-clemente/quic-go/internal/handshake" "github.com/lucas-clemente/quic-go/internal/mocks" + "github.com/lucas-clemente/quic-go/internal/mocks/ackhandler" "github.com/lucas-clemente/quic-go/internal/protocol" "github.com/lucas-clemente/quic-go/internal/wire" . "github.com/onsi/ginkgo" @@ -25,6 +26,7 @@ var _ = Describe("Packet packer", func() { handshakeStream *MockCryptoStream sealingManager *MockSealingManager sealer *mocks.MockSealer + pnManager *mockackhandler.MockSentPacketHandler token []byte ) @@ -63,6 +65,7 @@ var _ = Describe("Packet packer", func() { framer = NewMockFrameSource(mockCtrl) ackFramer = NewMockAckFrameSource(mockCtrl) sealingManager = NewMockSealingManager(mockCtrl) + pnManager = mockackhandler.NewMockSentPacketHandler(mockCtrl) sealer = mocks.NewMockSealer(mockCtrl) sealer.EXPECT().Overhead().Return(7).AnyTimes() sealer.EXPECT().Seal(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(dst, src []byte, pn protocol.PacketNumber, associatedData []byte) []byte { @@ -76,7 +79,7 @@ var _ = Describe("Packet packer", func() { protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, initialStream, handshakeStream, - func(protocol.PacketNumber) protocol.PacketNumberLen { return protocol.PacketNumberLen2 }, + pnManager, &net.TCPAddr{}, token, // token sealingManager, @@ -109,12 +112,18 @@ var _ = Describe("Packet packer", func() { Context("generating a packet header", func() { It("uses the Long Header format", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) h := packer.getHeader(protocol.EncryptionHandshake) Expect(h.IsLongHeader).To(BeTrue()) + Expect(h.PacketNumber).To(Equal(protocol.PacketNumber(0x42))) + Expect(h.PacketNumberLen).To(Equal(protocol.PacketNumberLen2)) Expect(h.Version).To(Equal(packer.version)) }) It("sets source and destination connection ID", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) srcConnID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8} destConnID := protocol.ConnectionID{8, 7, 6, 5, 4, 3, 2, 1} packer.srcConnID = srcConnID @@ -125,6 +134,8 @@ var _ = Describe("Packet packer", func() { }) It("changes the destination connection ID", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)).Times(2) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2).Times(2) srcConnID := protocol.ConnectionID{1, 1, 1, 1, 1, 1, 1, 1} packer.srcConnID = srcConnID dest1 := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8} @@ -140,9 +151,12 @@ var _ = Describe("Packet packer", func() { }) It("uses the Short Header format for 1-RTT packets", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x1337)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen4) h := packer.getHeader(protocol.Encryption1RTT) Expect(h.IsLongHeader).To(BeFalse()) - Expect(h.PacketNumberLen).To(BeNumerically(">", 0)) + Expect(h.PacketNumber).To(Equal(protocol.PacketNumber(0x1337))) + Expect(h.PacketNumberLen).To(Equal(protocol.PacketNumberLen4)) }) }) @@ -153,6 +167,9 @@ var _ = Describe("Packet packer", func() { }) It("returns nil when no packet is queued", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + // don't expect any calls to PopPacketNumber sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer) ackFramer.EXPECT().GetAckFrame() framer.EXPECT().AppendControlFrames(nil, gomock.Any()) @@ -163,6 +180,9 @@ var _ = Describe("Packet packer", func() { }) It("packs single packets", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer) ackFramer.EXPECT().GetAckFrame() expectAppendControlFrames() @@ -181,6 +201,9 @@ var _ = Describe("Packet packer", func() { }) It("stores the encryption level a packet was sealed with", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer) ackFramer.EXPECT().GetAckFrame() expectAppendControlFrames() @@ -194,6 +217,9 @@ var _ = Describe("Packet packer", func() { }) It("packs a single ACK", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Largest: 42, Smallest: 1}}} ackFramer.EXPECT().GetAckFrame().Return(ack) sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer) @@ -206,6 +232,10 @@ var _ = Describe("Packet packer", func() { }) It("packs a CONNECTION_CLOSE", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) + // expect no framer.PopStreamFrames ccf := wire.ConnectionCloseFrame{ ErrorCode: 0x1337, ReasonPhrase: "foobar", @@ -217,19 +247,10 @@ var _ = Describe("Packet packer", func() { Expect(p.frames[0]).To(Equal(&ccf)) }) - It("doesn't send any other frames when sending a CONNECTION_CLOSE", func() { - // expect no framer.PopStreamFrames - ccf := &wire.ConnectionCloseFrame{ - ErrorCode: 0x1337, - ReasonPhrase: "foobar", - } - sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer) - p, err := packer.PackConnectionClose(ccf) - Expect(err).ToNot(HaveOccurred()) - Expect(p.frames).To(Equal([]wire.Frame{ccf})) - }) - It("packs control frames", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer) ackFramer.EXPECT().GetAckFrame() frames := []wire.Frame{&wire.RstStreamFrame{}, &wire.MaxDataFrame{}} @@ -242,23 +263,9 @@ var _ = Describe("Packet packer", func() { Expect(p.raw).NotTo(BeEmpty()) }) - It("increases the packet number", func() { - sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer).Times(2) - ackFramer.EXPECT().GetAckFrame().Times(2) - expectAppendControlFrames() - expectAppendStreamFrames(&wire.StreamFrame{Data: []byte("foobar")}) - expectAppendControlFrames() - expectAppendStreamFrames(&wire.StreamFrame{Data: []byte("raboof")}) - p1, err := packer.PackPacket() - Expect(err).ToNot(HaveOccurred()) - Expect(p1).ToNot(BeNil()) - p2, err := packer.PackPacket() - Expect(err).ToNot(HaveOccurred()) - Expect(p2).ToNot(BeNil()) - Expect(p2.header.PacketNumber).To(BeNumerically(">", p1.header.PacketNumber)) - }) - It("accounts for the space consumed by control frames", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer) ackFramer.EXPECT().GetAckFrame() var maxSize protocol.ByteCount @@ -276,25 +283,6 @@ var _ = Describe("Packet packer", func() { Expect(err).ToNot(HaveOccurred()) }) - It("only increases the packet number when there is an actual packet to send", func() { - sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer).Times(2) - ackFramer.EXPECT().GetAckFrame().Times(2) - expectAppendStreamFrames() - expectAppendControlFrames() - packer.packetNumberGenerator.nextToSkip = 1000 - p, err := packer.PackPacket() - Expect(p).To(BeNil()) - Expect(err).ToNot(HaveOccurred()) - Expect(packer.packetNumberGenerator.Peek()).To(Equal(protocol.PacketNumber(1))) - expectAppendControlFrames() - expectAppendStreamFrames(&wire.StreamFrame{Data: []byte("foobar")}) - p, err = packer.PackPacket() - Expect(err).ToNot(HaveOccurred()) - Expect(p).ToNot(BeNil()) - Expect(p.header.PacketNumber).To(Equal(protocol.PacketNumber(1))) - Expect(packer.packetNumberGenerator.Peek()).To(Equal(protocol.PacketNumber(2))) - }) - Context("packing ACK packets", func() { It("doesn't pack a packet if there's no ACK to send", func() { ackFramer.EXPECT().GetAckFrame() @@ -304,6 +292,9 @@ var _ = Describe("Packet packer", func() { }) It("packs ACK packets", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer) ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 10}}} ackFramer.EXPECT().GetAckFrame().Return(ack) @@ -316,6 +307,9 @@ var _ = Describe("Packet packer", func() { Context("making ACK packets retransmittable", func() { sendMaxNumNonRetransmittableAcks := func() { for i := 0; i < protocol.MaxNonRetransmittableAcks; i++ { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer) ackFramer.EXPECT().GetAckFrame().Return(&wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}}) expectAppendControlFrames() @@ -329,6 +323,9 @@ var _ = Describe("Packet packer", func() { It("adds a PING frame when it's supposed to send a retransmittable packet", func() { sendMaxNumNonRetransmittableAcks() + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer) ackFramer.EXPECT().GetAckFrame().Return(&wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}}) expectAppendControlFrames() @@ -338,6 +335,9 @@ var _ = Describe("Packet packer", func() { Expect(err).ToNot(HaveOccurred()) Expect(p.frames).To(ContainElement(&wire.PingFrame{})) // make sure the next packet doesn't contain another PING + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer) ackFramer.EXPECT().GetAckFrame().Return(&wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}}) expectAppendControlFrames() @@ -351,6 +351,8 @@ var _ = Describe("Packet packer", func() { It("waits until there's something to send before adding a PING frame", func() { sendMaxNumNonRetransmittableAcks() // nothing to send + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer) expectAppendControlFrames() expectAppendStreamFrames() @@ -361,6 +363,9 @@ var _ = Describe("Packet packer", func() { // now add some frame to send expectAppendControlFrames() expectAppendStreamFrames() + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer) ackFramer.EXPECT().GetAckFrame().Return(&wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}}) p, err = packer.PackPacket() @@ -371,19 +376,25 @@ var _ = Describe("Packet packer", func() { It("doesn't send a PING if it already sent another retransmittable frame", func() { sendMaxNumNonRetransmittableAcks() + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer) ackFramer.EXPECT().GetAckFrame() expectAppendStreamFrames() expectAppendControlFrames(&wire.MaxDataFrame{}) p, err := packer.PackPacket() - Expect(p).ToNot(BeNil()) Expect(err).ToNot(HaveOccurred()) + Expect(p).ToNot(BeNil()) Expect(p.frames).ToNot(ContainElement(&wire.PingFrame{})) }) }) Context("STREAM frame handling", func() { It("does not split a STREAM frame with maximum size", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) ackFramer.EXPECT().GetAckFrame() sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer) expectAppendControlFrames() @@ -420,6 +431,9 @@ var _ = Describe("Packet packer", func() { Data: []byte("frame 3"), DataLenPresent: true, } + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer) ackFramer.EXPECT().GetAckFrame() expectAppendControlFrames() @@ -437,6 +451,8 @@ var _ = Describe("Packet packer", func() { }) It("doesn't send unencrypted stream data on a data stream", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) sealingManager.EXPECT().GetSealer().Return(protocol.EncryptionInitial, sealer) ackFramer.EXPECT().GetAckFrame() expectAppendControlFrames() @@ -449,7 +465,9 @@ var _ = Describe("Packet packer", func() { Context("retransmissions", func() { It("retransmits a small packet", func() { - packer.packetNumberGenerator.next = 10 + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) sealingManager.EXPECT().GetSealerWithEncryptionLevel(protocol.Encryption1RTT).Return(sealer, nil) frames := []wire.Frame{ &wire.MaxDataFrame{ByteOffset: 0x1234}, @@ -467,6 +485,9 @@ var _ = Describe("Packet packer", func() { }) It("packs two packets for retransmission if the original packet contained many control frames", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)).Times(2) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2).Times(2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)).Times(2) sealingManager.EXPECT().GetSealerWithEncryptionLevel(protocol.Encryption1RTT).Return(sealer, nil) var frames []wire.Frame var totalLen protocol.ByteCount @@ -479,7 +500,6 @@ var _ = Describe("Packet packer", func() { frames = append(frames, f) totalLen += f.Length(packer.version) } - packer.packetNumberGenerator.next = 10 packets, err := packer.PackRetransmission(&ackhandler.Packet{ EncryptionLevel: protocol.Encryption1RTT, Frames: frames, @@ -494,6 +514,9 @@ var _ = Describe("Packet packer", func() { }) It("splits a STREAM frame that doesn't fit", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)).Times(2) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2).Times(2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)).Times(2) sealingManager.EXPECT().GetSealerWithEncryptionLevel(protocol.Encryption1RTT).Return(sealer, nil) packets, err := packer.PackRetransmission(&ackhandler.Packet{ EncryptionLevel: protocol.Encryption1RTT, @@ -520,6 +543,9 @@ var _ = Describe("Packet packer", func() { }) It("splits STREAM frames, if necessary", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)).AnyTimes() + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2).AnyTimes() + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)).AnyTimes() for i := 0; i < 100; i++ { sealingManager.EXPECT().GetSealerWithEncryptionLevel(protocol.Encryption1RTT).Return(sealer, nil).MaxTimes(2) sf1 := &wire.StreamFrame{ @@ -555,6 +581,9 @@ var _ = Describe("Packet packer", func() { }) It("packs two packets for retransmission if the original packet contained many STREAM frames", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)).Times(2) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2).Times(2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)).Times(2) sealingManager.EXPECT().GetSealerWithEncryptionLevel(protocol.Encryption1RTT).Return(sealer, nil) var frames []wire.Frame var totalLen protocol.ByteCount @@ -582,6 +611,9 @@ var _ = Describe("Packet packer", func() { }) It("correctly sets the DataLenPresent on STREAM frames", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) sealingManager.EXPECT().GetSealerWithEncryptionLevel(protocol.Encryption1RTT).Return(sealer, nil) frames := []wire.Frame{ &wire.StreamFrame{StreamID: 4, Data: []byte("foobar"), DataLenPresent: true}, @@ -608,6 +640,8 @@ var _ = Describe("Packet packer", func() { Context("max packet size", func() { It("sets the maximum packet size", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)).Times(2) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2).Times(2) sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer).Times(2) ackFramer.EXPECT().GetAckFrame().Times(2) var initialMaxPacketSize protocol.ByteCount @@ -632,6 +666,8 @@ var _ = Describe("Packet packer", func() { }) It("doesn't increase the max packet size", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)).Times(2) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2).Times(2) sealingManager.EXPECT().GetSealer().Return(protocol.Encryption1RTT, sealer).Times(2) ackFramer.EXPECT().GetAckFrame().Times(2) var initialMaxPacketSize protocol.ByteCount @@ -659,6 +695,9 @@ var _ = Describe("Packet packer", func() { Context("packing crypto packets", func() { It("sets the payload length", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) f := &wire.CryptoFrame{ Offset: 0x1337, Data: []byte("foobar"), @@ -674,6 +713,9 @@ var _ = Describe("Packet packer", func() { It("packs a maximum size crypto packet", func() { var f *wire.CryptoFrame + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) sealingManager.EXPECT().GetSealerWithEncryptionLevel(protocol.EncryptionHandshake).Return(sealer, nil) ackFramer.EXPECT().GetAckFrame() initialStream.EXPECT().HasData() @@ -695,6 +737,9 @@ var _ = Describe("Packet packer", func() { It("pads Initial packets to the required minimum packet size", func() { f := &wire.CryptoFrame{Data: []byte("foobar")} + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) sealingManager.EXPECT().GetSealerWithEncryptionLevel(protocol.EncryptionInitial).Return(sealer, nil) ackFramer.EXPECT().GetAckFrame() initialStream.EXPECT().HasData().Return(true) @@ -711,6 +756,9 @@ var _ = Describe("Packet packer", func() { }) It("sets the correct payload length for an Initial packet", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) sealingManager.EXPECT().GetSealerWithEncryptionLevel(protocol.EncryptionInitial).Return(sealer, nil) ackFramer.EXPECT().GetAckFrame() initialStream.EXPECT().HasData().Return(true) @@ -727,6 +775,9 @@ var _ = Describe("Packet packer", func() { It("adds an ACK frame", func() { f := &wire.CryptoFrame{Data: []byte("foobar")} ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 42, Largest: 1337}}} + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) sealingManager.EXPECT().GetSealerWithEncryptionLevel(protocol.EncryptionInitial).Return(sealer, nil) ackFramer.EXPECT().GetAckFrame().Return(ack) initialStream.EXPECT().HasData().Return(true) @@ -746,6 +797,9 @@ var _ = Describe("Packet packer", func() { sf := &wire.StreamFrame{Data: []byte("foobar")} It("packs a retransmission with the right encryption level", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) sealingManager.EXPECT().GetSealerWithEncryptionLevel(protocol.EncryptionInitial).Return(sealer, nil) packet := &ackhandler.Packet{ PacketType: protocol.PacketTypeHandshake, @@ -762,6 +816,8 @@ var _ = Describe("Packet packer", func() { // this should never happen, since non forward-secure packets are limited to a size smaller than MaxPacketSize, such that it is always possible to retransmit them without splitting the StreamFrame It("refuses to send a packet larger than MaxPacketSize", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) sealingManager.EXPECT().GetSealerWithEncryptionLevel(gomock.Any()).Return(sealer, nil) packet := &ackhandler.Packet{ EncryptionLevel: protocol.EncryptionHandshake, @@ -778,6 +834,9 @@ var _ = Describe("Packet packer", func() { }) It("packs a retransmission for an Initial packet", func() { + pnManager.EXPECT().PeekPacketNumber().Return(protocol.PacketNumber(0x42)) + pnManager.EXPECT().GetPacketNumberLen(gomock.Any()).Return(protocol.PacketNumberLen2) + pnManager.EXPECT().PopPacketNumber().Return(protocol.PacketNumber(0x42)) sealingManager.EXPECT().GetSealerWithEncryptionLevel(protocol.EncryptionInitial).Return(sealer, nil) packer.perspective = protocol.PerspectiveClient packet := &ackhandler.Packet{ diff --git a/session.go b/session.go index ebd4d3a8..7cf6fc54 100644 --- a/session.go +++ b/session.go @@ -183,7 +183,7 @@ var newSession = func( s.srcConnID, initialStream, handshakeStream, - s.sentPacketHandler.GetPacketNumberLen, + s.sentPacketHandler, s.RemoteAddr(), nil, // no token cs, @@ -256,7 +256,7 @@ var newClientSession = func( s.srcConnID, initialStream, handshakeStream, - s.sentPacketHandler.GetPacketNumberLen, + s.sentPacketHandler, s.RemoteAddr(), token, cs,