Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Commit

Permalink
remove the MultiplexConn type assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Oct 27, 2017
1 parent dff3ff6 commit b2606fa
Show file tree
Hide file tree
Showing 9 changed files with 536 additions and 763 deletions.
59 changes: 21 additions & 38 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,43 +38,39 @@ func newSingleConn(ctx context.Context, local, remote peer.ID, privKey ci.PrivKe

var streamConn smux.Conn
var secSession secio.Session
switch conn := tptConn.(type) {
case tpt.DuplexConn:
c := conn
// 1. secure the connection
if privKey != nil && iconn.EncryptConnections {
var err error
secSession, err = setupSecureSession(ctx, local, privKey, conn)
if err != nil {
return nil, err
}
c = &secureDuplexConn{
insecure: conn,
secure: secSession,
}
} else {
log.Warning("creating INSECURE connection %s at %s", tptConn.LocalMultiaddr(), tptConn.RemoteMultiaddr())
}

// 2. start stream multipling
c := tptConn
// 1. secure the connection
if privKey != nil && iconn.EncryptConnections {
var err error
streamConn, err = pstpt.NewConn(c, isServer)
secSession, err = setupSecureSession(ctx, local, privKey, tptConn)
if err != nil {
return nil, err
}
case tpt.MultiplexConn:
panic("not implemented yet")
c = &secureConn{
insecure: tptConn,
secure: secSession,
}
} else {
log.Warning("creating INSECURE connection %s at %s", tptConn.LocalMultiaddr(), tptConn.RemoteMultiaddr())
}

conn := &singleConn{
// 2. start stream multipling
var err error
streamConn, err = pstpt.NewConn(c, isServer)
if err != nil {
return nil, err
}

sconn := &singleConn{
streamConn: streamConn,
tptConn: tptConn,
secSession: secSession,
event: log.EventBegin(ctx, "connLifetime", ml),
}

log.Debugf("newSingleConn %p: %v to %v", conn, local, remote)
return conn, nil
log.Debugf("newSingleConn %p: %v to %v", sconn, local, remote)
return sconn, nil
}

func setupSecureSession(ctx context.Context, local peer.ID, privKey ci.PrivKey, ch io.ReadWriteCloser) (secio.Session, error) {
Expand All @@ -88,20 +84,7 @@ func setupSecureSession(ctx context.Context, local peer.ID, privKey ci.PrivKey,
LocalID: local,
PrivateKey: privKey,
}
secSession, err := sessgen.NewSession(ctx, ch)
if err != nil {
return nil, err
}
// force the handshake right now
// TODO: find a better solution for this
b := []byte("handshake")
if _, err := secSession.ReadWriter().Write(b); err != nil {
return nil, err
}
if _, err := io.ReadFull(secSession.ReadWriter(), b); err != nil {
return nil, err
}
return secSession, nil
return sessgen.NewSession(ctx, ch)
}

// close is the internal close function, called by ContextCloser.Close
Expand Down
39 changes: 3 additions & 36 deletions conn_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ import (
tpt "github.com/libp2p/go-libp2p-transport"
tcpt "github.com/libp2p/go-tcp-transport"
tu "github.com/libp2p/go-testutil"
quict "github.com/marten-seemann/libp2p-quic-transport"
ma "github.com/multiformats/go-multiaddr"
yamux "github.com/whyrusleeping/go-smux-yamux"
grc "github.com/whyrusleeping/gorocheck"
"github.com/whyrusleeping/mafmt"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -40,45 +38,18 @@ var _ = AfterEach(func() {
// the stream muxer used for tests using the single stream connection
var streamMuxer = yamux.DefaultTransport

type transportType uint8

const (
duplexTransport transportType = 1 + iota
multiplexTransport
)

var transportTypes = []transportType{duplexTransport}

func (t transportType) String() string {
if t == duplexTransport {
return "duplex transport"
}
return "multiplex transport"
}

// dialRawConn dials a tpt.Conn
// but it stops there. It doesn't do protocol selection and handshake
func dialRawConn(laddr, raddr ma.Multiaddr) tpt.Conn {
var d tpt.Dialer
if mafmt.QUIC.Matches(laddr) {
var err error
d, err = quict.NewQuicTransport().Dialer(laddr)
Expect(err).ToNot(HaveOccurred())
} else {
var err error
d, err = tcpt.NewTCPTransport().Dialer(laddr)
Expect(err).ToNot(HaveOccurred())
}
d, err := tcpt.NewTCPTransport().Dialer(laddr)
Expect(err).ToNot(HaveOccurred())
c, err := d.Dial(raddr)
Expect(err).ToNot(HaveOccurred())
return c
}

// getTransport gets the right transport for a multiaddr
func getTransport(a ma.Multiaddr) tpt.Transport {
if mafmt.QUIC.Matches(a) {
return quict.NewQuicTransport()
}
return tcpt.NewTCPTransport()
}

Expand All @@ -104,12 +75,8 @@ func getDialer(localPeer peer.ID, privKey ci.PrivKey, addr ma.Multiaddr) *Dialer

// randPeerNetParams works like testutil.RandPeerNetParams
// if called for a multi-stream transport, it replaces the address with a QUIC address
func randPeerNetParams(tr transportType) *tu.PeerNetParams {
func randPeerNetParams() *tu.PeerNetParams {
p, err := tu.RandPeerNetParams()
Expect(err).ToNot(HaveOccurred())
if tr == multiplexTransport {
p.Addr, err = ma.NewMultiaddr("/ip4/127.0.0.1/udp/0/quic")
Expect(err).ToNot(HaveOccurred())
}
return p
}
Loading

0 comments on commit b2606fa

Please sign in to comment.