Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
Change transport address to not include /p2p part
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars Gierth committed Oct 2, 2018
1 parent eca2b86 commit 4865319
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 29 deletions.
12 changes: 7 additions & 5 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ func (c *Conn) RemoteAddr() net.Addr {
}
}

// TODO: is it okay to cast c.Conn().RemotePeer() into a multiaddr? might be "user input"
func (c *Conn) RemoteMultiaddr() ma.Multiaddr {
a, err := ma.NewMultiaddr(fmt.Sprintf("/ipfs/%s/p2p-circuit/ipfs/%s", c.Conn().RemotePeer().Pretty(), c.remote.ID.Pretty()))
if err != nil {
panic(err)
}
return a
proto := ma.ProtocolWithCode(ma.P_P2P).Name
peerid := c.Conn().RemotePeer().Pretty()
p2paddr := ma.StringCast(fmt.Sprintf("/%s/%s", proto, peerid))

circaddr := ma.Cast(ma.CodeToVarint(P_CIRCUIT))
return p2paddr.Encapsulate(circaddr)
}

func (c *Conn) LocalMultiaddr() ma.Multiaddr {
Expand Down
16 changes: 8 additions & 8 deletions dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ import (
)

func (d *RelayTransport) Dial(ctx context.Context, a ma.Multiaddr, p peer.ID) (tpt.Conn, error) {
c, err := d.Relay().Dial(ctx, a)
c, err := d.Relay().Dial(ctx, a, p)
if err != nil {
return nil, err
}
return d.upgrader.UpgradeOutbound(ctx, d, c, p)
}

func (r *Relay) Dial(ctx context.Context, a ma.Multiaddr) (*Conn, error) {
func (r *Relay) Dial(ctx context.Context, a ma.Multiaddr, p peer.ID) (*Conn, error) {
if !r.Matches(a) {
return nil, fmt.Errorf("%s is not a relay address", a)
}
parts := ma.Split(a)

spl, _ := ma.NewMultiaddr("/p2p-circuit")
spl := ma.Cast(ma.CodeToVarint(P_CIRCUIT))

var relayaddr, destaddr ma.Multiaddr
for i, p := range parts {
Expand All @@ -36,9 +36,9 @@ func (r *Relay) Dial(ctx context.Context, a ma.Multiaddr) (*Conn, error) {
}
}

dinfo, err := pstore.InfoFromP2pAddr(destaddr)
if err != nil {
return nil, err
dinfo := &pstore.PeerInfo{ID: p, Addrs: []ma.Multiaddr{}}
if len(destaddr.Bytes()) > 0 {
dinfo.Addrs = append(dinfo.Addrs, destaddr)
}

if len(relayaddr.Bytes()) == 0 {
Expand All @@ -47,9 +47,9 @@ func (r *Relay) Dial(ctx context.Context, a ma.Multiaddr) (*Conn, error) {
}

var rinfo *pstore.PeerInfo
rinfo, err = pstore.InfoFromP2pAddr(relayaddr)
rinfo, err := pstore.InfoFromP2pAddr(relayaddr)
if err != nil {
return nil, err
return nil, fmt.Errorf("error parsing multiaddr '%s': %s", relayaddr.String(), err)
}

return r.DialPeer(ctx, *rinfo, *dinfo)
Expand Down
7 changes: 1 addition & 6 deletions listen.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package relay

import (
"fmt"
"net"

pb "github.com/libp2p/go-libp2p-circuit/pb"
Expand Down Expand Up @@ -50,11 +49,7 @@ func (l *RelayListener) Addr() net.Addr {
}

func (l *RelayListener) Multiaddr() ma.Multiaddr {
a, err := ma.NewMultiaddr(fmt.Sprintf("/p2p-circuit/ipfs/%s", l.self.Pretty()))
if err != nil {
panic(err)
}
return a
return ma.Cast(ma.CodeToVarint(P_CIRCUIT))
}

func (l *RelayListener) Close() error {
Expand Down
14 changes: 4 additions & 10 deletions relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,12 @@ func TestBasicRelayDial(t *testing.T) {
con.Close()
}()

addr, err := ma.NewMultiaddr(fmt.Sprintf("/ipfs/%s/p2p-circuit/ipfs/%s", hosts[1].ID().Pretty(), hosts[2].ID().Pretty()))
if err != nil {
t.Fatal(err)
}
addr := ma.StringCast(fmt.Sprintf("/ipfs/%s/p2p-circuit", hosts[1].ID().Pretty()))

rctx, rcancel := context.WithTimeout(ctx, time.Second)
defer rcancel()

con, err := r1.Dial(rctx, addr)
con, err := r1.Dial(rctx, addr, hosts[2].ID())
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -262,15 +259,12 @@ func TestUnspecificRelayDial(t *testing.T) {
con.Close()
}()

addr, err := ma.NewMultiaddr(fmt.Sprintf("/p2p-circuit/ipfs/%s", hosts[2].ID().Pretty()))
if err != nil {
t.Fatal(err)
}
addr := ma.StringCast(fmt.Sprintf("/p2p-circuit"))

rctx, rcancel := context.WithTimeout(ctx, time.Second)
defer rcancel()

con, err := r1.Dial(rctx, addr)
con, err := r1.Dial(rctx, addr, hosts[2].ID())
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 4865319

Please sign in to comment.