Skip to content

Commit 1529945

Browse files
sukunrtMarcoPolo
andcommitted
tcpreuse: error on using tcpreuse with pnet (#3129)
Co-authored-by: Marco Munizaga <[email protected]>
1 parent 7397e65 commit 1529945

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

config/config.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -446,12 +446,9 @@ func (cfg *Config) newBasicHost(swrm *swarm.Swarm, eventBus event.Bus) (*bhost.B
446446
return h, nil
447447
}
448448

449-
// NewNode constructs a new libp2p Host from the Config.
450-
//
451-
// This function consumes the config. Do not reuse it (really!).
452-
func (cfg *Config) NewNode() (host.Host, error) {
449+
func (cfg *Config) validate() error {
453450
if cfg.EnableAutoRelay && !cfg.Relay {
454-
return nil, fmt.Errorf("cannot enable autorelay; relay is not enabled")
451+
return fmt.Errorf("cannot enable autorelay; relay is not enabled")
455452
}
456453
// If possible check that the resource manager conn limit is higher than the
457454
// limit set in the conn manager.
@@ -462,6 +459,33 @@ func (cfg *Config) NewNode() (host.Host, error) {
462459
}
463460
}
464461

462+
if len(cfg.PSK) > 0 && cfg.ShareTCPListener {
463+
return errors.New("cannot use shared TCP listener with PSK")
464+
}
465+
466+
return nil
467+
}
468+
469+
// NewNode constructs a new libp2p Host from the Config.
470+
//
471+
// This function consumes the config. Do not reuse it (really!).
472+
func (cfg *Config) NewNode() (host.Host, error) {
473+
474+
validateErr := cfg.validate()
475+
if validateErr != nil {
476+
if cfg.ResourceManager != nil {
477+
cfg.ResourceManager.Close()
478+
}
479+
if cfg.ConnManager != nil {
480+
cfg.ConnManager.Close()
481+
}
482+
if cfg.Peerstore != nil {
483+
cfg.Peerstore.Close()
484+
}
485+
486+
return nil, validateErr
487+
}
488+
465489
if !cfg.DisableMetrics {
466490
rcmgr.MustRegisterWith(cfg.PrometheusRegisterer)
467491
}

libp2p_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/libp2p/go-libp2p/core/network"
2727
"github.com/libp2p/go-libp2p/core/peer"
2828
"github.com/libp2p/go-libp2p/core/peerstore"
29+
"github.com/libp2p/go-libp2p/core/pnet"
2930
"github.com/libp2p/go-libp2p/core/routing"
3031
"github.com/libp2p/go-libp2p/core/transport"
3132
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
@@ -761,6 +762,7 @@ func TestSharedTCPAddr(t *testing.T) {
761762
ListenAddrStrings("/ip4/0.0.0.0/tcp/8888/ws"),
762763
)
763764
require.NoError(t, err)
765+
defer h.Close()
764766
sawTCP := false
765767
sawWS := false
766768
for _, addr := range h.Addrs() {
@@ -773,5 +775,12 @@ func TestSharedTCPAddr(t *testing.T) {
773775
}
774776
require.True(t, sawTCP)
775777
require.True(t, sawWS)
776-
h.Close()
778+
779+
_, err = New(
780+
ShareTCPListener(),
781+
Transport(tcp.NewTCPTransport),
782+
Transport(websocket.New),
783+
PrivateNetwork(pnet.PSK([]byte{1, 2, 3})),
784+
)
785+
require.ErrorContains(t, err, "cannot use shared TCP listener with PSK")
777786
}

0 commit comments

Comments
 (0)