From 03ad3b87ff8523ae178690b10b1f65e0757bb2de Mon Sep 17 00:00:00 2001 From: Overbool Date: Thu, 27 Sep 2018 12:58:59 +0800 Subject: [PATCH 1/2] fix(p2p): issue #5523 License: MIT Signed-off-by: Overbool --- core/commands/p2p.go | 40 ++++++++++++++++++++++++++++++++++++++++ p2p/local.go | 10 ++++------ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/core/commands/p2p.go b/core/commands/p2p.go index 38521eeab9f..be5d8702e98 100644 --- a/core/commands/p2p.go +++ b/core/commands/p2p.go @@ -219,6 +219,12 @@ Example: return } + // port can't be 0 + if err := checkPort(target); err != nil { + res.SetError(err, cmdkit.ErrNormal) + return + } + allowCustom, _, err := req.Option(allowCustomProtocolOptionName).Bool() if err != nil { res.SetError(err, cmdkit.ErrNormal) @@ -239,6 +245,40 @@ 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 { + // get tcp or udp port from multiaddr + getPort := func() (string, error) { + sport, _ := target.ValueForProtocol(ma.P_TCP) + if sport != "" { + return sport, nil + } + + sport, _ = target.ValueForProtocol(ma.P_UDP) + if sport != "" { + return sport, nil + } + return "", fmt.Errorf("address does not contain tcp or udp protocol") + } + + sport, err := getPort() + if err != nil { + return err + } + + 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 diff --git a/p2p/local.go b/p2p/local.go index 0a51b55e93f..3587b42b4e0 100644 --- a/p2p/local.go +++ b/p2p/local.go @@ -28,21 +28,19 @@ type localListener struct { // ForwardLocal creates new P2P stream to a remote listener func (p2p *P2P) ForwardLocal(ctx context.Context, peer peer.ID, proto protocol.ID, bindAddr ma.Multiaddr) (Listener, error) { listener := &localListener{ - ctx: ctx, - - p2p: p2p, - + 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 From 717c0c83a1c409280dc93a9da5634814528dadaa Mon Sep 17 00:00:00 2001 From: Overbool Date: Fri, 28 Sep 2018 08:20:08 +0800 Subject: [PATCH 2/2] fix(p2p): add test for issue #5523 ;wq Signed-off-by: Overbool License: MIT Signed-off-by: Overbool --- core/commands/p2p.go | 2 +- test/sharness/t0180-p2p.sh | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/core/commands/p2p.go b/core/commands/p2p.go index be5d8702e98..69f35c55638 100644 --- a/core/commands/p2p.go +++ b/core/commands/p2p.go @@ -273,7 +273,7 @@ func checkPort(target ma.Multiaddr) error { } if port == 0 { - return fmt.Errorf("port can't be 0") + return fmt.Errorf("port can not be 0") } return nil diff --git a/test/sharness/t0180-p2p.sh b/test/sharness/t0180-p2p.sh index 0d676746c5f..62587a42bba 100755 --- a/test/sharness/t0180-p2p.sh +++ b/test/sharness/t0180-p2p.sh @@ -6,7 +6,7 @@ test_description="Test experimental p2p commands" # start iptb + wait for peering test_expect_success 'init iptb' ' - iptb init -n 2 --bootstrap=none --port=0 + iptb init -n 3 --bootstrap=none --port=0 ' test_expect_success 'generate test data' ' @@ -14,7 +14,7 @@ test_expect_success 'generate test data' ' echo "012345" > test1.bin ' -startup_cluster 2 +startup_cluster 3 test_expect_success 'peer ids' ' PEERID_0=$(iptb get id 0) && @@ -37,6 +37,7 @@ test_expect_success 'fail without config option being enabled' ' test_expect_success "enable filestore config setting" ' ipfsi 0 config --json Experimental.Libp2pStreamMounting true ipfsi 1 config --json Experimental.Libp2pStreamMounting true + ipfsi 2 config --json Experimental.Libp2pStreamMounting true ' test_expect_success 'start p2p listener' ' @@ -147,6 +148,23 @@ test_expect_success 'C->S Close local listener' ' check_test_ports +# Checking port + +test_expect_success "cannot accept 0 port in 'ipfs p2p listen'" ' + test_must_fail ipfsi 2 p2p listen /x/p2p-test/0 /ip4/127.0.0.1/tcp/0 +' + +test_expect_success "'ipfs p2p forward' accept 0 port" ' + ipfsi 2 p2p forward /x/p2p-test/0 /ip4/127.0.0.1/tcp/0 /ipfs/$PEERID_0 +' + +test_expect_success "'ipfs p2p ls' output looks good" ' + echo "true" > forward_0_expected && + ipfsi 2 p2p ls | awk '\''{print $2}'\'' | sed "s/.*\///" | awk -F: '\''{if($1>0)print"true"}'\'' > forward_0_actual && + ipfsi 2 p2p close -p /x/p2p-test/0 && + test_cmp forward_0_expected forward_0_actual +' + # Listing streams test_expect_success "'ipfs p2p ls' succeeds" '