From 539097fc6e5977039845bfd9a29d267957734768 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sat, 4 Jan 2020 15:55:58 +0700 Subject: [PATCH] return a quic.ConnectionState from Session.ConnectionState() --- interface.go | 7 +++++-- internal/handshake/crypto_setup.go | 11 ++--------- internal/handshake/interface.go | 6 ++++-- internal/handshake/unsafe.go | 3 --- internal/mocks/crypto_setup.go | 6 +++--- internal/mocks/quic/early_session.go | 6 +++--- mock_quic_session_test.go | 6 +++--- session.go | 4 ++-- 8 files changed, 22 insertions(+), 27 deletions(-) diff --git a/interface.go b/interface.go index cff4c77f8..660034f8f 100644 --- a/interface.go +++ b/interface.go @@ -2,11 +2,11 @@ package quic import ( "context" - "crypto/tls" "io" "net" "time" + "github.com/lucas-clemente/quic-go/internal/handshake" "github.com/lucas-clemente/quic-go/internal/protocol" "github.com/lucas-clemente/quic-go/quictrace" ) @@ -139,6 +139,8 @@ type StreamError interface { ErrorCode() ErrorCode } +type ConnectionState = handshake.ConnectionState + // A Session is a QUIC connection between two peers. type Session interface { // AcceptStream returns the next stream opened by the peer, blocking until one is available. @@ -182,8 +184,9 @@ type Session interface { // Warning: This API should not be considered stable and might change soon. Context() context.Context // ConnectionState returns basic details about the QUIC connection. + // It blocks until the handshake completes. // Warning: This API should not be considered stable and might change soon. - ConnectionState() tls.ConnectionState + ConnectionState() ConnectionState } // An EarlySession is a session that is handshaking. diff --git a/internal/handshake/crypto_setup.go b/internal/handshake/crypto_setup.go index 7e209aff4..efd0d1dd1 100644 --- a/internal/handshake/crypto_setup.go +++ b/internal/handshake/crypto_setup.go @@ -8,7 +8,6 @@ import ( "io" "net" "sync" - "unsafe" "github.com/lucas-clemente/quic-go/internal/congestion" "github.com/lucas-clemente/quic-go/internal/protocol" @@ -782,12 +781,6 @@ func (h *cryptoSetup) Get1RTTOpener() (ShortHeaderOpener, error) { return h.aead, nil } -func (h *cryptoSetup) ConnectionState() tls.ConnectionState { - cs := h.conn.ConnectionState() - // h.conn is a qtls.Conn, which returns a qtls.ConnectionState. - // qtls.ConnectionState is identical to the tls.ConnectionState. - // It contains an unexported field which is used ExportKeyingMaterial(). - // The only way to return a tls.ConnectionState is to use unsafe. - // In unsafe.go we check that the two objects are actually identical. - return *(*tls.ConnectionState)(unsafe.Pointer(&cs)) +func (h *cryptoSetup) ConnectionState() ConnectionState { + return h.conn.ConnectionState() } diff --git a/internal/handshake/interface.go b/internal/handshake/interface.go index 90aa41975..7183a11ac 100644 --- a/internal/handshake/interface.go +++ b/internal/handshake/interface.go @@ -1,7 +1,6 @@ package handshake import ( - "crypto/tls" "errors" "io" "time" @@ -22,6 +21,9 @@ var ( ErrDecryptionFailed = errors.New("decryption failed") ) +// ConnectionState contains information about the state of the connection. +type ConnectionState = qtls.ConnectionState + type headerDecryptor interface { DecryptHeader(sample []byte, firstByte *byte, pnBytes []byte) } @@ -74,7 +76,7 @@ type CryptoSetup interface { HandleMessage([]byte, protocol.EncryptionLevel) bool SetLargest1RTTAcked(protocol.PacketNumber) DropHandshakeKeys() - ConnectionState() tls.ConnectionState + ConnectionState() ConnectionState GetInitialOpener() (LongHeaderOpener, error) GetHandshakeOpener() (LongHeaderOpener, error) diff --git a/internal/handshake/unsafe.go b/internal/handshake/unsafe.go index 8c0d3c41d..8238a5c70 100644 --- a/internal/handshake/unsafe.go +++ b/internal/handshake/unsafe.go @@ -13,9 +13,6 @@ import ( ) func init() { - if !structsEqual(&tls.ConnectionState{}, &qtls.ConnectionState{}) { - panic("qtls.ConnectionState not compatible with tls.ConnectionState") - } if !structsEqual(&tls.ClientSessionState{}, &qtls.ClientSessionState{}) { panic("qtls.ClientSessionState not compatible with tls.ClientSessionState") } diff --git a/internal/mocks/crypto_setup.go b/internal/mocks/crypto_setup.go index 6565bd965..382c1dc0a 100644 --- a/internal/mocks/crypto_setup.go +++ b/internal/mocks/crypto_setup.go @@ -5,12 +5,12 @@ package mocks import ( - tls "crypto/tls" reflect "reflect" gomock "github.com/golang/mock/gomock" handshake "github.com/lucas-clemente/quic-go/internal/handshake" protocol "github.com/lucas-clemente/quic-go/internal/protocol" + qtls "github.com/marten-seemann/qtls" ) // MockCryptoSetup is a mock of CryptoSetup interface @@ -63,10 +63,10 @@ func (mr *MockCryptoSetupMockRecorder) Close() *gomock.Call { } // ConnectionState mocks base method -func (m *MockCryptoSetup) ConnectionState() tls.ConnectionState { +func (m *MockCryptoSetup) ConnectionState() qtls.ConnectionState { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ConnectionState") - ret0, _ := ret[0].(tls.ConnectionState) + ret0, _ := ret[0].(qtls.ConnectionState) return ret0 } diff --git a/internal/mocks/quic/early_session.go b/internal/mocks/quic/early_session.go index 864150fd6..e5fcf0ae5 100644 --- a/internal/mocks/quic/early_session.go +++ b/internal/mocks/quic/early_session.go @@ -6,13 +6,13 @@ package mockquic import ( context "context" - tls "crypto/tls" net "net" reflect "reflect" gomock "github.com/golang/mock/gomock" quic "github.com/lucas-clemente/quic-go" protocol "github.com/lucas-clemente/quic-go/internal/protocol" + qtls "github.com/marten-seemann/qtls" ) // MockEarlySession is a mock of EarlySession interface @@ -83,10 +83,10 @@ func (mr *MockEarlySessionMockRecorder) CloseWithError(arg0, arg1 interface{}) * } // ConnectionState mocks base method -func (m *MockEarlySession) ConnectionState() tls.ConnectionState { +func (m *MockEarlySession) ConnectionState() qtls.ConnectionState { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ConnectionState") - ret0, _ := ret[0].(tls.ConnectionState) + ret0, _ := ret[0].(qtls.ConnectionState) return ret0 } diff --git a/mock_quic_session_test.go b/mock_quic_session_test.go index e1d4bb695..23e4f9c45 100644 --- a/mock_quic_session_test.go +++ b/mock_quic_session_test.go @@ -6,12 +6,12 @@ package quic import ( context "context" - tls "crypto/tls" net "net" reflect "reflect" gomock "github.com/golang/mock/gomock" protocol "github.com/lucas-clemente/quic-go/internal/protocol" + qtls "github.com/marten-seemann/qtls" ) // MockQuicSession is a mock of QuicSession interface @@ -82,10 +82,10 @@ func (mr *MockQuicSessionMockRecorder) CloseWithError(arg0, arg1 interface{}) *g } // ConnectionState mocks base method -func (m *MockQuicSession) ConnectionState() tls.ConnectionState { +func (m *MockQuicSession) ConnectionState() qtls.ConnectionState { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ConnectionState") - ret0, _ := ret[0].(tls.ConnectionState) + ret0, _ := ret[0].(qtls.ConnectionState) return ret0 } diff --git a/session.go b/session.go index 2d57bf286..085a86df1 100644 --- a/session.go +++ b/session.go @@ -53,7 +53,7 @@ type cryptoStreamHandler interface { SetLargest1RTTAcked(protocol.PacketNumber) DropHandshakeKeys() io.Closer - ConnectionState() tls.ConnectionState + ConnectionState() handshake.ConnectionState } type receivedPacket struct { @@ -579,7 +579,7 @@ func (s *session) Context() context.Context { return s.ctx } -func (s *session) ConnectionState() tls.ConnectionState { +func (s *session) ConnectionState() ConnectionState { return s.cryptoStreamHandler.ConnectionState() }