Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

p2p: custom discovery UDP port #24979

Merged
merged 7 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ var (
utils.CachePreimagesFlag,
utils.FDLimitFlag,
utils.ListenPortFlag,
utils.DiscoveryPortFlag,
utils.MaxPeersFlag,
utils.MaxPendingPeersFlag,
utils.MiningEnabledFlag,
Expand Down
13 changes: 11 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,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{
Expand Down Expand Up @@ -1116,12 +1122,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.
Expand Down
14 changes: 13 additions & 1 deletion p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down