Skip to content

Commit

Permalink
fix(p2p): issue ipfs#5523
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Overbool <overbool.xu@gmail.com>
  • Loading branch information
overbool committed Sep 27, 2018
1 parent 727bf49 commit 8fe7465
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
39 changes: 39 additions & 0 deletions core/commands/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ Example:
return
}

// port can't be 0
if err := checkPort(target); err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

allowCustom, _, err := req.Option("allow-custom-protocol").Bool()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
Expand All @@ -196,6 +202,39 @@ Example:
},
}

// checkPort checks whether target multiaddr contains tcp or udp protocol
// and whether the port is equal to 0
func checkPort(target ma.Multiaddr) error {
addr := target.String()
var sport string
if strings.Contains(addr, "tcp") {
s, err := target.ValueForProtocol(ma.P_TCP)
if err != nil {
return err
}
sport = s
} else if strings.Contains(addr ,"udp") {
s, err := target.ValueForProtocol(ma.P_UDP)
if err != nil {
return err
}
sport = s
} else {
return fmt.Errorf("address does not contain tcp or udp protocol")
}

port, err := strconv.Atoi(sport)
if err != nil {
return err
}

if port == 0 {
return fmt.Errorf("port can't be 0")
}

return nil
}

// forwardRemote forwards libp2p service connections to a manet address
func forwardRemote(ctx context.Context, p *p2p.P2P, proto protocol.ID, target ma.Multiaddr) error {
// TODO: return some info
Expand Down
6 changes: 2 additions & 4 deletions p2p/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,18 @@ type localListener struct {
func (p2p *P2P) ForwardLocal(ctx context.Context, peer peer.ID, proto protocol.ID, bindAddr ma.Multiaddr) (Listener, error) {
listener := &localListener{
ctx: ctx,

p2p: p2p,

proto: proto,
laddr: bindAddr,
peer: peer,
}

maListener, err := manet.Listen(listener.laddr)
maListener, err := manet.Listen(bindAddr)
if err != nil {
return nil, err
}

listener.listener = maListener
listener.laddr = maListener.Multiaddr()

if err := p2p.ListenersLocal.Register(listener); err != nil {
return nil, err
Expand Down

0 comments on commit 8fe7465

Please sign in to comment.