forked from MetaCubeX/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5102294
commit 82508f1
Showing
144 changed files
with
20,077 additions
and
10,110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,65 @@ | ||
package quic | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/lucas-clemente/quic-go/internal/flowcontrol" | ||
"github.com/lucas-clemente/quic-go/internal/protocol" | ||
"github.com/lucas-clemente/quic-go/internal/utils" | ||
"github.com/lucas-clemente/quic-go/internal/wire" | ||
) | ||
|
||
type cryptoStream interface { | ||
StreamID() protocol.StreamID | ||
io.Reader | ||
// for receiving data | ||
HandleCryptoFrame(*wire.CryptoFrame) error | ||
GetCryptoData() []byte | ||
// for sending data | ||
io.Writer | ||
handleStreamFrame(*wire.StreamFrame) error | ||
hasData() bool | ||
popStreamFrame(protocol.ByteCount) (*wire.StreamFrame, bool) | ||
closeForShutdown(error) | ||
setReadOffset(protocol.ByteCount) | ||
// methods needed for flow control | ||
getWindowUpdate() protocol.ByteCount | ||
handleMaxStreamDataFrame(*wire.MaxStreamDataFrame) | ||
HasData() bool | ||
PopCryptoFrame(protocol.ByteCount) *wire.CryptoFrame | ||
} | ||
|
||
type cryptoStreamImpl struct { | ||
*stream | ||
queue *frameSorter | ||
|
||
writeOffset protocol.ByteCount | ||
writeBuf []byte | ||
} | ||
|
||
func newCryptoStream() cryptoStream { | ||
return &cryptoStreamImpl{ | ||
queue: newFrameSorter(), | ||
} | ||
} | ||
|
||
func (s *cryptoStreamImpl) HandleCryptoFrame(f *wire.CryptoFrame) error { | ||
if maxOffset := f.Offset + protocol.ByteCount(len(f.Data)); maxOffset > protocol.MaxCryptoStreamOffset { | ||
return fmt.Errorf("received invalid offset %d on crypto stream, maximum allowed %d", maxOffset, protocol.MaxCryptoStreamOffset) | ||
} | ||
return s.queue.Push(f.Data, f.Offset, false) | ||
} | ||
|
||
// GetCryptoData retrieves data that was received in CRYPTO frames | ||
func (s *cryptoStreamImpl) GetCryptoData() []byte { | ||
data, _ := s.queue.Pop() | ||
return data | ||
} | ||
|
||
var _ cryptoStream = &cryptoStreamImpl{} | ||
// Writes writes data that should be sent out in CRYPTO frames | ||
func (s *cryptoStreamImpl) Write(p []byte) (int, error) { | ||
s.writeBuf = append(s.writeBuf, p...) | ||
return len(p), nil | ||
} | ||
|
||
func newCryptoStream(sender streamSender, flowController flowcontrol.StreamFlowController, version protocol.VersionNumber) cryptoStream { | ||
str := newStream(version.CryptoStreamID(), sender, flowController, version) | ||
return &cryptoStreamImpl{str} | ||
func (s *cryptoStreamImpl) HasData() bool { | ||
return len(s.writeBuf) > 0 | ||
} | ||
|
||
// SetReadOffset sets the read offset. | ||
// It is only needed for the crypto stream. | ||
// It must not be called concurrently with any other stream methods, especially Read and Write. | ||
func (s *cryptoStreamImpl) setReadOffset(offset protocol.ByteCount) { | ||
s.receiveStream.readOffset = offset | ||
s.receiveStream.frameQueue.readPos = offset | ||
func (s *cryptoStreamImpl) PopCryptoFrame(maxLen protocol.ByteCount) *wire.CryptoFrame { | ||
f := &wire.CryptoFrame{Offset: s.writeOffset} | ||
n := utils.MinByteCount(f.MaxDataLen(maxLen), protocol.ByteCount(len(s.writeBuf))) | ||
f.Data = s.writeBuf[:n] | ||
s.writeBuf = s.writeBuf[n:] | ||
s.writeOffset += n | ||
return f | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package quic | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/lucas-clemente/quic-go/internal/protocol" | ||
"github.com/lucas-clemente/quic-go/internal/wire" | ||
) | ||
|
||
type cryptoDataHandler interface { | ||
HandleData([]byte, protocol.EncryptionLevel) error | ||
} | ||
|
||
type cryptoStreamManager struct { | ||
cryptoHandler cryptoDataHandler | ||
|
||
initialStream cryptoStream | ||
handshakeStream cryptoStream | ||
} | ||
|
||
func newCryptoStreamManager( | ||
cryptoHandler cryptoDataHandler, | ||
initialStream cryptoStream, | ||
handshakeStream cryptoStream, | ||
) *cryptoStreamManager { | ||
return &cryptoStreamManager{ | ||
cryptoHandler: cryptoHandler, | ||
initialStream: initialStream, | ||
handshakeStream: handshakeStream, | ||
} | ||
} | ||
|
||
func (m *cryptoStreamManager) HandleCryptoFrame(frame *wire.CryptoFrame, encLevel protocol.EncryptionLevel) error { | ||
var str cryptoStream | ||
switch encLevel { | ||
case protocol.EncryptionInitial: | ||
str = m.initialStream | ||
case protocol.EncryptionHandshake: | ||
str = m.handshakeStream | ||
default: | ||
return fmt.Errorf("received CRYPTO frame with unexpected encryption level: %s", encLevel) | ||
} | ||
if err := str.HandleCryptoFrame(frame); err != nil { | ||
return err | ||
} | ||
for { | ||
data := str.GetCryptoData() | ||
if data == nil { | ||
return nil | ||
} | ||
if err := m.cryptoHandler.HandleData(data, encLevel); err != nil { | ||
return err | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package quic | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/lucas-clemente/quic-go/internal/protocol" | ||
"github.com/lucas-clemente/quic-go/internal/wire" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Crypto Stream Manager", func() { | ||
var ( | ||
csm *cryptoStreamManager | ||
cs *MockCryptoDataHandler | ||
) | ||
|
||
BeforeEach(func() { | ||
initialStream := newCryptoStream() | ||
handshakeStream := newCryptoStream() | ||
cs = NewMockCryptoDataHandler(mockCtrl) | ||
csm = newCryptoStreamManager(cs, initialStream, handshakeStream) | ||
}) | ||
|
||
It("handles in in-order crypto frame", func() { | ||
f := &wire.CryptoFrame{Data: []byte("foobar")} | ||
cs.EXPECT().HandleData([]byte("foobar"), protocol.EncryptionInitial) | ||
Expect(csm.HandleCryptoFrame(f, protocol.EncryptionInitial)).To(Succeed()) | ||
}) | ||
|
||
It("errors for unknown encryption levels", func() { | ||
err := csm.HandleCryptoFrame(&wire.CryptoFrame{}, protocol.Encryption1RTT) | ||
Expect(err).To(MatchError("received CRYPTO frame with unexpected encryption level: 1-RTT")) | ||
}) | ||
|
||
It("handles out-of-order crypto frames", func() { | ||
f1 := &wire.CryptoFrame{Data: []byte("foo")} | ||
f2 := &wire.CryptoFrame{ | ||
Offset: 3, | ||
Data: []byte("bar"), | ||
} | ||
gomock.InOrder( | ||
cs.EXPECT().HandleData([]byte("foo"), protocol.EncryptionInitial), | ||
cs.EXPECT().HandleData([]byte("bar"), protocol.EncryptionInitial), | ||
) | ||
Expect(csm.HandleCryptoFrame(f1, protocol.EncryptionInitial)).To(Succeed()) | ||
Expect(csm.HandleCryptoFrame(f2, protocol.EncryptionInitial)).To(Succeed()) | ||
}) | ||
|
||
It("handles handshake data", func() { | ||
f := &wire.CryptoFrame{Data: []byte("foobar")} | ||
cs.EXPECT().HandleData([]byte("foobar"), protocol.EncryptionHandshake) | ||
Expect(csm.HandleCryptoFrame(f, protocol.EncryptionHandshake)).To(Succeed()) | ||
}) | ||
|
||
It("returns the error if handling crypto data fails", func() { | ||
testErr := errors.New("test error") | ||
f := &wire.CryptoFrame{Data: []byte("foobar")} | ||
cs.EXPECT().HandleData([]byte("foobar"), protocol.EncryptionHandshake).Return(testErr) | ||
err := csm.HandleCryptoFrame(f, protocol.EncryptionHandshake) | ||
Expect(err).To(MatchError(testErr)) | ||
}) | ||
}) |
Oops, something went wrong.