1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-20 02:21:48 +08:00

remove msgio double wrap

There was doublewrapping with an unneeded msgio. given that we
use a stream muxer now, msgio is only needed by secureConn -- to
signal the boundaries of an encrypted / mac-ed ciphertext.

Side note: i think including the varint length in the clear is
actually a bad idea that can be exploited by an attacker. it should
be encrypted, too. (TODO)

License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
This commit is contained in:
Juan Batiz-Benet
2015-04-12 15:39:04 -07:00
committed by Jeromy
parent aafebc58af
commit c48f456bdf
6 changed files with 39 additions and 59 deletions

View File

@ -6,7 +6,6 @@ import (
"net" "net"
"time" "time"
msgio "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio"
mpool "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio/mpool" mpool "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio/mpool"
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
manet "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net" manet "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
@ -32,7 +31,6 @@ type singleConn struct {
local peer.ID local peer.ID
remote peer.ID remote peer.ID
maconn manet.Conn maconn manet.Conn
msgrw msgio.ReadWriteCloser
event io.Closer event io.Closer
} }
@ -44,7 +42,6 @@ func newSingleConn(ctx context.Context, local, remote peer.ID, maconn manet.Conn
local: local, local: local,
remote: remote, remote: remote,
maconn: maconn, maconn: maconn,
msgrw: msgio.NewReadWriter(maconn),
event: log.EventBegin(ctx, "connLifetime", ml), event: log.EventBegin(ctx, "connLifetime", ml),
} }
@ -62,7 +59,7 @@ func (c *singleConn) Close() error {
}() }()
// close underlying connection // close underlying connection
return c.msgrw.Close() return c.maconn.Close()
} }
// ID is an identifier unique to this connection. // ID is an identifier unique to this connection.
@ -123,31 +120,12 @@ func (c *singleConn) RemotePeer() peer.ID {
// Read reads data, net.Conn style // Read reads data, net.Conn style
func (c *singleConn) Read(buf []byte) (int, error) { func (c *singleConn) Read(buf []byte) (int, error) {
return c.msgrw.Read(buf) return c.maconn.Read(buf)
} }
// Write writes data, net.Conn style // Write writes data, net.Conn style
func (c *singleConn) Write(buf []byte) (int, error) { func (c *singleConn) Write(buf []byte) (int, error) {
return c.msgrw.Write(buf) return c.maconn.Write(buf)
}
func (c *singleConn) NextMsgLen() (int, error) {
return c.msgrw.NextMsgLen()
}
// ReadMsg reads data, net.Conn style
func (c *singleConn) ReadMsg() ([]byte, error) {
return c.msgrw.ReadMsg()
}
// WriteMsg writes data, net.Conn style
func (c *singleConn) WriteMsg(buf []byte) error {
return c.msgrw.WriteMsg(buf)
}
// ReleaseMsg releases a buffer
func (c *singleConn) ReleaseMsg(m []byte) {
c.msgrw.ReleaseMsg(m)
} }
// ID returns the ID of a given Conn. // ID returns the ID of a given Conn.

View File

@ -8,17 +8,25 @@ import (
"testing" "testing"
"time" "time"
msgio "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
travis "github.com/ipfs/go-ipfs/util/testutil/ci/travis" travis "github.com/ipfs/go-ipfs/util/testutil/ci/travis"
) )
func msgioWrap(c Conn) msgio.ReadWriter {
return msgio.NewReadWriter(c)
}
func testOneSendRecv(t *testing.T, c1, c2 Conn) { func testOneSendRecv(t *testing.T, c1, c2 Conn) {
mc1 := msgioWrap(c1)
mc2 := msgioWrap(c2)
log.Debugf("testOneSendRecv from %s to %s", c1.LocalPeer(), c2.LocalPeer()) log.Debugf("testOneSendRecv from %s to %s", c1.LocalPeer(), c2.LocalPeer())
m1 := []byte("hello") m1 := []byte("hello")
if err := c1.WriteMsg(m1); err != nil { if err := mc1.WriteMsg(m1); err != nil {
t.Fatal(err) t.Fatal(err)
} }
m2, err := c2.ReadMsg() m2, err := mc2.ReadMsg()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -28,11 +36,14 @@ func testOneSendRecv(t *testing.T, c1, c2 Conn) {
} }
func testNotOneSendRecv(t *testing.T, c1, c2 Conn) { func testNotOneSendRecv(t *testing.T, c1, c2 Conn) {
mc1 := msgioWrap(c1)
mc2 := msgioWrap(c2)
m1 := []byte("hello") m1 := []byte("hello")
if err := c1.WriteMsg(m1); err == nil { if err := mc1.WriteMsg(m1); err == nil {
t.Fatal("write should have failed", err) t.Fatal("write should have failed", err)
} }
_, err := c2.ReadMsg() _, err := mc2.ReadMsg()
if err == nil { if err == nil {
t.Fatal("read should have failed", err) t.Fatal("read should have failed", err)
} }
@ -72,10 +83,13 @@ func TestCloseLeak(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
c1, c2, _, _ := setupSingleConn(t, ctx) c1, c2, _, _ := setupSingleConn(t, ctx)
mc1 := msgioWrap(c1)
mc2 := msgioWrap(c2)
for i := 0; i < num; i++ { for i := 0; i < num; i++ {
b1 := []byte(fmt.Sprintf("beep%d", i)) b1 := []byte(fmt.Sprintf("beep%d", i))
c1.WriteMsg(b1) mc1.WriteMsg(b1)
b2, err := c2.ReadMsg() b2, err := mc2.ReadMsg()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -84,8 +98,8 @@ func TestCloseLeak(t *testing.T) {
} }
b2 = []byte(fmt.Sprintf("boop%d", i)) b2 = []byte(fmt.Sprintf("boop%d", i))
c2.WriteMsg(b2) mc2.WriteMsg(b2)
b1, err = c1.ReadMsg() b1, err = mc1.ReadMsg()
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -187,10 +187,10 @@ func testDialer(t *testing.T, secure bool) {
} }
// fmt.Println("sending") // fmt.Println("sending")
c.WriteMsg([]byte("beep")) mc := msgioWrap(c)
c.WriteMsg([]byte("boop")) mc.WriteMsg([]byte("beep"))
mc.WriteMsg([]byte("boop"))
out, err := c.ReadMsg() out, err := mc.ReadMsg()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -201,7 +201,7 @@ func testDialer(t *testing.T, secure bool) {
t.Error("unexpected conn output", data) t.Error("unexpected conn output", data)
} }
out, err = c.ReadMsg() out, err = mc.ReadMsg()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -11,7 +11,6 @@ import (
transport "github.com/ipfs/go-ipfs/p2p/net/transport" transport "github.com/ipfs/go-ipfs/p2p/net/transport"
peer "github.com/ipfs/go-ipfs/p2p/peer" peer "github.com/ipfs/go-ipfs/p2p/peer"
msgio "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio"
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
) )
@ -46,8 +45,8 @@ type Conn interface {
SetReadDeadline(t time.Time) error SetReadDeadline(t time.Time) error
SetWriteDeadline(t time.Time) error SetWriteDeadline(t time.Time) error
msgio.Reader io.Reader
msgio.Writer io.Writer
} }
// Dialer is an object that can open connections. We could have a "convenience" // Dialer is an object that can open connections. We could have a "convenience"

View File

@ -119,20 +119,6 @@ func (c *secureConn) Write(buf []byte) (int, error) {
return c.secure.ReadWriter().Write(buf) return c.secure.ReadWriter().Write(buf)
} }
func (c *secureConn) NextMsgLen() (int, error) {
return c.secure.ReadWriter().NextMsgLen()
}
// ReadMsg reads data, net.Conn style
func (c *secureConn) ReadMsg() ([]byte, error) {
return c.secure.ReadWriter().ReadMsg()
}
// WriteMsg writes data, net.Conn style
func (c *secureConn) WriteMsg(buf []byte) error {
return c.secure.ReadWriter().WriteMsg(buf)
}
// ReleaseMsg releases a buffer // ReleaseMsg releases a buffer
func (c *secureConn) ReleaseMsg(m []byte) { func (c *secureConn) ReleaseMsg(m []byte) {
c.secure.ReadWriter().ReleaseMsg(m) c.secure.ReadWriter().ReleaseMsg(m)

View File

@ -145,13 +145,16 @@ func TestSecureCloseLeak(t *testing.T) {
} }
runPair := func(c1, c2 Conn, num int) { runPair := func(c1, c2 Conn, num int) {
mc1 := msgioWrap(c1)
mc2 := msgioWrap(c2)
log.Debugf("runPair %d", num) log.Debugf("runPair %d", num)
for i := 0; i < num; i++ { for i := 0; i < num; i++ {
log.Debugf("runPair iteration %d", i) log.Debugf("runPair iteration %d", i)
b1 := []byte("beep") b1 := []byte("beep")
c1.WriteMsg(b1) mc1.WriteMsg(b1)
b2, err := c2.ReadMsg() b2, err := mc2.ReadMsg()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -160,8 +163,8 @@ func TestSecureCloseLeak(t *testing.T) {
} }
b2 = []byte("beep") b2 = []byte("beep")
c2.WriteMsg(b2) mc2.WriteMsg(b2)
b1, err = c1.ReadMsg() b1, err = mc1.ReadMsg()
if err != nil { if err != nil {
panic(err) panic(err)
} }