diff --git a/daemon/daemon.go b/daemon/daemon.go index 875779ed0686dafd6aebe5801c004fcb7f3eb151..5156e741d9894957a575a2db7553935b80709186 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io" - "net" "os" "path" "sync" @@ -27,7 +26,7 @@ const LockFile = "daemon.lock" // starting up a new set of connections type DaemonListener struct { node *core.IpfsNode - list net.Listener + list manet.Listener closed bool wg sync.WaitGroup lk io.Closer @@ -52,11 +51,6 @@ func NewDaemonListener(ipfsnode *core.IpfsNode, addr ma.Multiaddr, confdir strin return nil, err } - network, host, err := manet.DialArgs(addr) - if err != nil { - return nil, err - } - ofi, err := os.Create(confdir + "/rpcaddress") if err != nil { log.Warning("Could not create rpcaddress file: %s", err) @@ -70,7 +64,7 @@ func NewDaemonListener(ipfsnode *core.IpfsNode, addr ma.Multiaddr, confdir strin } ofi.Close() - list, err := net.Listen(network, host) + list, err := manet.Listen(addr) if err != nil { return nil, err } @@ -111,7 +105,7 @@ func (dl *DaemonListener) Listen() { } } -func (dl *DaemonListener) handleConnection(conn net.Conn) { +func (dl *DaemonListener) handleConnection(conn manet.Conn) { defer conn.Close() dec := json.NewDecoder(conn) diff --git a/daemon/daemon_client.go b/daemon/daemon_client.go index 557aa2b1b7de69aa5c253d3a19e12b6fb2fe5665..8db8615358fc31ff7e69011fa623a0916c2a2f00 100644 --- a/daemon/daemon_client.go +++ b/daemon/daemon_client.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "io" - "net" "os" ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" @@ -74,9 +73,7 @@ func SendCommand(command *Command, confdir string) error { return err } - network, host, err := manet.DialArgs(maddr) - - conn, err := net.Dial(network, host) + conn, err := manet.Dial(maddr) if err != nil { return err } diff --git a/net/conn/conn.go b/net/conn/conn.go index 2bf6bb95ea0de14121d7c33036caaa7db9f29343..dcf6c9231161cc326f7455d6799020a6b754bddb 100644 --- a/net/conn/conn.go +++ b/net/conn/conn.go @@ -2,7 +2,6 @@ package conn import ( "fmt" - "net" msgio "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio" ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" @@ -25,7 +24,7 @@ const MaxMessageSize = 1 << 20 type Conn struct { Peer *peer.Peer Addr ma.Multiaddr - Conn net.Conn + Conn manet.Conn Closed chan bool Outgoing *msgio.Chan @@ -37,11 +36,11 @@ type Conn struct { type Map map[u.Key]*Conn // NewConn constructs a new connection -func NewConn(peer *peer.Peer, addr ma.Multiaddr, nconn net.Conn) (*Conn, error) { +func NewConn(peer *peer.Peer, addr ma.Multiaddr, mconn manet.Conn) (*Conn, error) { conn := &Conn{ Peer: peer, Addr: addr, - Conn: nconn, + Conn: mconn, } if err := conn.newChans(); err != nil { @@ -59,12 +58,7 @@ func Dial(network string, peer *peer.Peer) (*Conn, error) { return nil, fmt.Errorf("No address for network %s", network) } - network, host, err := manet.DialArgs(addr) - if err != nil { - return nil, err - } - - nconn, err := net.Dial(network, host) + nconn, err := manet.Dial(addr) if err != nil { return nil, err } @@ -104,9 +98,3 @@ func (c *Conn) Close() error { c.Closed <- true return err } - -// NetConnMultiaddr returns the net.Conn's address, recast as a multiaddr. -// (consider moving this directly into the multiaddr package) -func NetConnMultiaddr(nconn net.Conn) (ma.Multiaddr, error) { - return manet.FromNetAddr(nconn.RemoteAddr()) -} diff --git a/net/conn/conn_test.go b/net/conn/conn_test.go index 219004be8fbd0a2796d4c910c35431ea4c51b12c..95d5833dfa2ac3b8e96dd5fe88238f7776e5c5b4 100644 --- a/net/conn/conn_test.go +++ b/net/conn/conn_test.go @@ -1,12 +1,12 @@ package conn import ( - "net" "testing" peer "github.com/jbenet/go-ipfs/peer" ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" + manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net" mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" ) @@ -26,7 +26,7 @@ func setupPeer(id string, addr string) (*peer.Peer, error) { return p, nil } -func echoListen(listener *net.TCPListener) { +func echoListen(listener manet.Listener) { for { c, err := listener.Accept() if err == nil { @@ -36,7 +36,7 @@ func echoListen(listener *net.TCPListener) { } } -func echo(c net.Conn) { +func echo(c manet.Conn) { for { data := make([]byte, 1024) i, err := c.Read(data) @@ -55,11 +55,15 @@ func echo(c net.Conn) { func TestDial(t *testing.T) { - listener, err := net.Listen("tcp", "127.0.0.1:1234") + maddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1234") + if err != nil { + t.Fatal("failure to parse multiaddr") + } + listener, err := manet.Listen(maddr) if err != nil { t.Fatal("error setting up listener", err) } - go echoListen(listener.(*net.TCPListener)) + go echoListen(listener) p, err := setupPeer("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33", "/ip4/127.0.0.1/tcp/1234") if err != nil { diff --git a/net/swarm/conn.go b/net/swarm/conn.go index 86c8df5d356fed35ab26c077e62ae6ef3d3c83a9..7d5c47b5c9f57e554d1f49a0e2f8e8e91d14a358 100644 --- a/net/swarm/conn.go +++ b/net/swarm/conn.go @@ -3,7 +3,6 @@ package swarm import ( "errors" "fmt" - "net" spipe "github.com/jbenet/go-ipfs/crypto/spipe" conn "github.com/jbenet/go-ipfs/net/conn" @@ -26,7 +25,7 @@ func (s *Swarm) listen() error { if err != nil { hasErr = true retErr.Errors[i] = err - log.Error("Failed to listen on: %s [%s]", addr, err) + log.Error("Failed to listen on: %s - %s", addr, err) } } @@ -38,12 +37,7 @@ func (s *Swarm) listen() error { // Listen for new connections on the given multiaddr func (s *Swarm) connListen(maddr ma.Multiaddr) error { - netstr, addr, err := manet.DialArgs(maddr) - if err != nil { - return err - } - - list, err := net.Listen(netstr, addr) + list, err := manet.Listen(maddr) if err != nil { return err } @@ -56,8 +50,7 @@ func (s *Swarm) connListen(maddr ma.Multiaddr) error { for { nconn, err := list.Accept() if err != nil { - e := fmt.Errorf("Failed to accept connection: %s - %s [%s]", - netstr, addr, err) + e := fmt.Errorf("Failed to accept connection: %s - %s", maddr, err) s.errChan <- e // if cancel is nil, we're closed. @@ -74,13 +67,9 @@ func (s *Swarm) connListen(maddr ma.Multiaddr) error { } // Handle getting ID from this peer, handshake, and adding it into the map -func (s *Swarm) handleIncomingConn(nconn net.Conn) { +func (s *Swarm) handleIncomingConn(nconn manet.Conn) { - addr, err := conn.NetConnMultiaddr(nconn) - if err != nil { - s.errChan <- err - return - } + addr := nconn.RemoteMultiaddr() // Construct conn with nil peer for now, because we don't know its ID yet. // connSetup will figure this out, and pull out / construct the peer. diff --git a/net/swarm/swarm.go b/net/swarm/swarm.go index fffc1fcefbd72525f9c1d795632c8ba712c99407..057e4ad260976329d4117db2f604bbb1ee094dbf 100644 --- a/net/swarm/swarm.go +++ b/net/swarm/swarm.go @@ -3,7 +3,6 @@ package swarm import ( "errors" "fmt" - "net" "sync" conn "github.com/jbenet/go-ipfs/net/conn" @@ -13,6 +12,7 @@ import ( context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" + manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net" ) var log = u.Logger("swarm") @@ -62,7 +62,7 @@ type Swarm struct { connsLock sync.RWMutex // listeners for each network address - listeners []net.Listener + listeners []manet.Listener // cancel is an internal function used to stop the Swarm's processing. cancel context.CancelFunc diff --git a/net/swarm/swarm_test.go b/net/swarm/swarm_test.go index 5effe2909c1488b1084a8de7e0cb0d2af154ae93..88de9198d2e7d8dee67d7df9495175c2c7b739a3 100644 --- a/net/swarm/swarm_test.go +++ b/net/swarm/swarm_test.go @@ -2,7 +2,6 @@ package swarm import ( "fmt" - "net" "testing" msg "github.com/jbenet/go-ipfs/net/message" @@ -16,7 +15,7 @@ import ( mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" ) -func pingListen(t *testing.T, listener *net.TCPListener, peer *peer.Peer) { +func pingListen(t *testing.T, listener manet.Listener, peer *peer.Peer) { for { c, err := listener.Accept() if err == nil { @@ -25,7 +24,7 @@ func pingListen(t *testing.T, listener *net.TCPListener, peer *peer.Peer) { } } -func pong(t *testing.T, c net.Conn, peer *peer.Peer) { +func pong(t *testing.T, c manet.Conn, peer *peer.Peer) { mrw := msgio.NewReadWriter(c) for { data := make([]byte, 1024) @@ -79,7 +78,7 @@ func TestSwarm(t *testing.T) { t.Error(err) } var peers []*peer.Peer - var listeners []net.Listener + var listeners []manet.Listener peerNames := map[string]string{ "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31": "/ip4/127.0.0.1/tcp/2345", "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a32": "/ip4/127.0.0.1/tcp/3456", @@ -96,15 +95,11 @@ func TestSwarm(t *testing.T) { if a == nil { t.Fatal("error setting up peer (addr is nil)", peer) } - n, h, err := manet.DialArgs(a) - if err != nil { - t.Fatal("error getting dial args from addr") - } - listener, err := net.Listen(n, h) + listener, err := manet.Listen(a) if err != nil { t.Fatal("error setting up listener", err) } - go pingListen(t, listener.(*net.TCPListener), peer) + go pingListen(t, listener, peer) _, err = swarm.Dial(peer) if err != nil { @@ -147,6 +142,6 @@ func TestSwarm(t *testing.T) { swarm.Close() for _, listener := range listeners { - listener.(*net.TCPListener).Close() + listener.Close() } }