diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 9906f193b13b..dae346384758 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -125,6 +125,7 @@ var ( utils.TriesInMemoryFlag, utils.FDLimitFlag, utils.ListenPortFlag, + utils.DiscoveryPortFlag, utils.MaxPeersFlag, utils.MaxPendingPeersFlag, utils.MiningEnabledFlag, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index f2d55e536058..53cb7ae219b0 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -846,6 +846,12 @@ var ( Usage: "Sets DNS discovery entry points (use \"\" to disable DNS)", Category: flags.NetworkingCategory, } + DiscoveryPortFlag = &cli.IntFlag{ + Name: "discovery.port", + Usage: "Use a custom UDP port for P2P discovery", + Value: 30303, + Category: flags.NetworkingCategory, + } // Console JSpathFlag = &flags.DirectoryFlag{ @@ -1228,12 +1234,15 @@ func setBootstrapNodesV5(ctx *cli.Context, cfg *p2p.Config) { } } -// setListenAddress creates a TCP listening address string from set command -// line flags. +// setListenAddress creates TCP/UDP listening address strings from set command +// line flags func setListenAddress(ctx *cli.Context, cfg *p2p.Config) { if ctx.IsSet(ListenPortFlag.Name) { cfg.ListenAddr = fmt.Sprintf(":%d", ctx.Int(ListenPortFlag.Name)) } + if ctx.IsSet(DiscoveryPortFlag.Name) { + cfg.DiscAddr = fmt.Sprintf(":%d", ctx.Int(DiscoveryPortFlag.Name)) + } } // setNAT creates a port mapper from command line flags. diff --git a/p2p/server.go b/p2p/server.go index b3d5172e4a62..41249c1f8ab0 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -136,6 +136,10 @@ type Config struct { // the server is started. ListenAddr string + // If DiscAddr is set to a non-nil value, the server will use ListenAddr + // for TCP and DiscAddr for the UDP discovery protocol. + DiscAddr string + // If set to a non-nil value, the given NAT port mapper // is used to make the listening port available to the // Internet. @@ -549,7 +553,15 @@ func (srv *Server) setupDiscovery() error { return nil } - addr, err := net.ResolveUDPAddr("udp", srv.ListenAddr) + listenAddr := srv.ListenAddr + + // Use an alternate listening address for UDP if + // a custom discovery address is configured. + if srv.DiscAddr != "" { + listenAddr = srv.DiscAddr + } + + addr, err := net.ResolveUDPAddr("udp", listenAddr) if err != nil { return err }