diff --git a/core/core.go b/core/core.go index 427c33c29..9aaa6e504 100644 --- a/core/core.go +++ b/core/core.go @@ -177,14 +177,14 @@ func initIdentity(cfg *config.Config, online bool) (*peer.Peer, error) { } // address is optional - var addresses []*ma.Multiaddr + var addresses []ma.Multiaddr if len(cfg.Addresses.Swarm) > 0 { maddr, err := ma.NewMultiaddr(cfg.Addresses.Swarm) if err != nil { return nil, err } - addresses = []*ma.Multiaddr{maddr} + addresses = []ma.Multiaddr{maddr} } var ( diff --git a/daemon/daemon.go b/daemon/daemon.go index 02fe49023..45ac49e68 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -39,7 +39,7 @@ type Command struct { Opts map[string]interface{} } -func NewDaemonListener(ipfsnode *core.IpfsNode, addr *ma.Multiaddr, confdir string) (*DaemonListener, error) { +func NewDaemonListener(ipfsnode *core.IpfsNode, addr ma.Multiaddr, confdir string) (*DaemonListener, error) { var err error confdir, err = u.TildeExpansion(confdir) if err != nil { @@ -51,7 +51,7 @@ func NewDaemonListener(ipfsnode *core.IpfsNode, addr *ma.Multiaddr, confdir stri return nil, err } - network, host, err := addr.DialArgs() + network, host, err := ma.DialArgs(addr) if err != nil { return nil, err } @@ -62,12 +62,7 @@ func NewDaemonListener(ipfsnode *core.IpfsNode, addr *ma.Multiaddr, confdir stri return nil, err } - mstr, err := addr.String() - if err != nil { - return nil, err - } - - _, err = ofi.Write([]byte(mstr)) + _, err = ofi.Write([]byte(addr.String())) if err != nil { log.Warning("Could not write to rpcaddress file: %s", err) return nil, err diff --git a/daemon/daemon_client.go b/daemon/daemon_client.go index 17d1b2c26..478b7fd7d 100644 --- a/daemon/daemon_client.go +++ b/daemon/daemon_client.go @@ -73,7 +73,7 @@ func SendCommand(command *Command, confdir string) error { return err } - network, host, err := maddr.DialArgs() + network, host, err := ma.DialArgs(maddr) conn, err := net.Dial(network, host) if err != nil { diff --git a/net/conn/conn.go b/net/conn/conn.go index 645264b8d..25ecc44d6 100644 --- a/net/conn/conn.go +++ b/net/conn/conn.go @@ -21,7 +21,7 @@ const MaxMessageSize = 1 << 20 // Conn represents a connection to another Peer (IPFS Node). type Conn struct { Peer *peer.Peer - Addr *ma.Multiaddr + Addr ma.Multiaddr Conn net.Conn Closed chan bool @@ -34,7 +34,7 @@ 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, nconn net.Conn) (*Conn, error) { conn := &Conn{ Peer: peer, Addr: addr, @@ -56,7 +56,7 @@ func Dial(network string, peer *peer.Peer) (*Conn, error) { return nil, fmt.Errorf("No address for network %s", network) } - network, host, err := addr.DialArgs() + network, host, err := ma.DialArgs(addr) if err != nil { return nil, err } @@ -104,6 +104,6 @@ func (c *Conn) Close() error { // 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) { +func NetConnMultiaddr(nconn net.Conn) (ma.Multiaddr, error) { return ma.FromNetAddr(nconn.RemoteAddr()) } diff --git a/net/swarm/conn.go b/net/swarm/conn.go index 0713ccf0b..6b2dce32b 100644 --- a/net/swarm/conn.go +++ b/net/swarm/conn.go @@ -37,8 +37,8 @@ func (s *Swarm) listen() error { } // Listen for new connections on the given multiaddr -func (s *Swarm) connListen(maddr *ma.Multiaddr) error { - netstr, addr, err := maddr.DialArgs() +func (s *Swarm) connListen(maddr ma.Multiaddr) error { + netstr, addr, err := ma.DialArgs(maddr) if err != nil { return err } diff --git a/net/swarm/swarm.go b/net/swarm/swarm.go index df84e5a94..9941c7086 100644 --- a/net/swarm/swarm.go +++ b/net/swarm/swarm.go @@ -143,7 +143,7 @@ func (s *Swarm) Dial(peer *peer.Peer) (*conn.Conn, error) { // DialAddr is for connecting to a peer when you know their addr but not their ID. // Should only be used when sure that not connected to peer in question // TODO(jbenet) merge with Dial? need way to patch back. -func (s *Swarm) DialAddr(addr *ma.Multiaddr) (*conn.Conn, error) { +func (s *Swarm) DialAddr(addr ma.Multiaddr) (*conn.Conn, error) { if addr == nil { return nil, errors.New("addr must be a non-nil Multiaddr") } diff --git a/net/swarm/swarm_test.go b/net/swarm/swarm_test.go index b2747481c..7d7c13802 100644 --- a/net/swarm/swarm_test.go +++ b/net/swarm/swarm_test.go @@ -95,7 +95,7 @@ func TestSwarm(t *testing.T) { if a == nil { t.Fatal("error setting up peer (addr is nil)", peer) } - n, h, err := a.DialArgs() + n, h, err := ma.DialArgs(a) if err != nil { t.Fatal("error getting dial args from addr") } diff --git a/peer/peer.go b/peer/peer.go index 69d73c2d4..8b0bda8aa 100644 --- a/peer/peer.go +++ b/peer/peer.go @@ -38,7 +38,7 @@ type Map map[u.Key]*Peer // ID, and relevant Addresses. type Peer struct { ID ID - Addresses []*ma.Multiaddr + Addresses []ma.Multiaddr PrivKey ic.PrivKey PubKey ic.PubKey @@ -54,7 +54,7 @@ func (p *Peer) Key() u.Key { } // AddAddress adds the given Multiaddr address to Peer's addresses. -func (p *Peer) AddAddress(a *ma.Multiaddr) { +func (p *Peer) AddAddress(a ma.Multiaddr) { p.Lock() defer p.Unlock() @@ -67,17 +67,12 @@ func (p *Peer) AddAddress(a *ma.Multiaddr) { } // NetAddress returns the first Multiaddr found for a given network. -func (p *Peer) NetAddress(n string) *ma.Multiaddr { +func (p *Peer) NetAddress(n string) ma.Multiaddr { p.RLock() defer p.RUnlock() for _, a := range p.Addresses { - ps, err := a.Protocols() - if err != nil { - continue // invalid addr - } - - for _, p := range ps { + for _, p := range a.Protocols() { if p.Name == n { return a } diff --git a/routing/dht/Message.go b/routing/dht/Message.go index 1be9a3b80..84d323c37 100644 --- a/routing/dht/Message.go +++ b/routing/dht/Message.go @@ -20,11 +20,7 @@ func peerToPBPeer(p *peer.Peer) *Message_Peer { if len(p.Addresses) == 0 || p.Addresses[0] == nil { pbp.Addr = proto.String("") } else { - addr, err := p.Addresses[0].String() - if err != nil { - //Temp: what situations could cause this? - panic(err) - } + addr := p.Addresses[0].String() pbp.Addr = &addr } pid := string(p.ID) diff --git a/routing/dht/dht_test.go b/routing/dht/dht_test.go index 1bbc62cdc..f5d391387 100644 --- a/routing/dht/dht_test.go +++ b/routing/dht/dht_test.go @@ -43,8 +43,8 @@ func setupDHT(t *testing.T, p *peer.Peer) *IpfsDHT { return d } -func setupDHTS(n int, t *testing.T) ([]*ma.Multiaddr, []*peer.Peer, []*IpfsDHT) { - var addrs []*ma.Multiaddr +func setupDHTS(n int, t *testing.T) ([]ma.Multiaddr, []*peer.Peer, []*IpfsDHT) { + var addrs []ma.Multiaddr for i := 0; i < n; i++ { a, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 5000+i)) if err != nil { @@ -67,7 +67,7 @@ func setupDHTS(n int, t *testing.T) ([]*ma.Multiaddr, []*peer.Peer, []*IpfsDHT) return addrs, peers, dhts } -func makePeer(addr *ma.Multiaddr) *peer.Peer { +func makePeer(addr ma.Multiaddr) *peer.Peer { p := new(peer.Peer) p.AddAddress(addr) sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512) diff --git a/routing/dht/ext_test.go b/routing/dht/ext_test.go index f8b9293a8..df8f26ff3 100644 --- a/routing/dht/ext_test.go +++ b/routing/dht/ext_test.go @@ -184,7 +184,7 @@ func TestGetFailures(t *testing.T) { func _randPeer() *peer.Peer { p := new(peer.Peer) p.ID = make(peer.ID, 16) - p.Addresses = []*ma.Multiaddr{nil} + p.Addresses = []ma.Multiaddr{nil} crand.Read(p.ID) return p } diff --git a/server/http/http.go b/server/http/http.go index 75623d5f2..14eff9cd2 100644 --- a/server/http/http.go +++ b/server/http/http.go @@ -17,14 +17,14 @@ type handler struct { } // Serve starts the http server -func Serve(address *ma.Multiaddr, node *core.IpfsNode) error { +func Serve(address ma.Multiaddr, node *core.IpfsNode) error { r := mux.NewRouter() handler := &handler{&ipfsHandler{node}} r.HandleFunc("/ipfs/", handler.postHandler).Methods("POST") r.PathPrefix("/ipfs/").Handler(handler).Methods("GET") http.Handle("/", r) - _, host, err := address.DialArgs() + _, host, err := ma.DialArgs(address) if err != nil { return err }