mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-16 09:50:45 +00:00
cmd/devp2p: support dual-stack discovery listener (#35220)
`devp2p discv4 listen` / `discv5 listen` is the supported replacement for the removed bootnode tool, but it bound IPv4-only and `-extaddr` took a single address, so it couldn't run a dual-stack bootnode. This binds the listener dual-stack (falling back to IPv4-only where IPv6 is unavailable) and lets `-extaddr` take a comma-separated IPv4/IPv6 pair. A single node can then advertise both `ip` and `ip6` in its ENR over one UDP port: ``` devp2p discv4 listen --nodekey <key> --addr [::]:30301 \ --extaddr 203.0.113.10:30301,[2001:db8::1]:30301 ``` The fallback IP is only derived from the listener when no `-extaddr` is given, so a v4- or v6-only `-extaddr` no longer leaks a loopback entry. All addresses must share one UDP port (single socket).
This commit is contained in:
parent
a0568b1dbf
commit
9700b5b10e
1 changed files with 42 additions and 18 deletions
|
|
@ -126,7 +126,7 @@ var (
|
||||||
}
|
}
|
||||||
extAddrFlag = &cli.StringFlag{
|
extAddrFlag = &cli.StringFlag{
|
||||||
Name: "extaddr",
|
Name: "extaddr",
|
||||||
Usage: "UDP endpoint announced in ENR. You can provide a bare IP address or IP:port as the value of this flag.",
|
Usage: "UDP endpoint announced in ENR. You can provide a bare IP address or IP:port as the value of this flag. Provide a comma-separated pair to announce both an IPv4 and an IPv6 endpoint.",
|
||||||
}
|
}
|
||||||
crawlTimeoutFlag = &cli.DurationFlag{
|
crawlTimeoutFlag = &cli.DurationFlag{
|
||||||
Name: "timeout",
|
Name: "timeout",
|
||||||
|
|
@ -344,36 +344,60 @@ func parseExtAddr(spec string) (ip net.IP, port int, ok bool) {
|
||||||
|
|
||||||
func listen(ctx *cli.Context, ln *enode.LocalNode) *net.UDPConn {
|
func listen(ctx *cli.Context, ln *enode.LocalNode) *net.UDPConn {
|
||||||
addr := ctx.String(listenAddrFlag.Name)
|
addr := ctx.String(listenAddrFlag.Name)
|
||||||
|
extAddr := ctx.String(extAddrFlag.Name)
|
||||||
|
var (
|
||||||
|
socket net.PacketConn
|
||||||
|
err error
|
||||||
|
)
|
||||||
if addr == "" {
|
if addr == "" {
|
||||||
addr = "0.0.0.0:0"
|
// Dual-stack socket, falling back to IPv4-only where IPv6 is unavailable.
|
||||||
|
if socket, err = net.ListenPacket("udp", "[::]:0"); err != nil {
|
||||||
|
socket, err = net.ListenPacket("udp", "0.0.0.0:0")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
socket, err = net.ListenPacket("udp", addr)
|
||||||
}
|
}
|
||||||
socket, err := net.ListenPacket("udp4", addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exit(err)
|
exit(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure UDP endpoint in ENR from listener address.
|
// Configure the ENR endpoint from the listener address, but only without an
|
||||||
|
// explicit -extaddr: otherwise we'd announce a fallback IP for an address
|
||||||
|
// family the user didn't specify (e.g. loopback IPv4 on an IPv6-only node).
|
||||||
usocket := socket.(*net.UDPConn)
|
usocket := socket.(*net.UDPConn)
|
||||||
uaddr := socket.LocalAddr().(*net.UDPAddr)
|
uaddr := socket.LocalAddr().(*net.UDPAddr)
|
||||||
if uaddr.IP.IsUnspecified() {
|
if extAddr == "" {
|
||||||
ln.SetFallbackIP(net.IP{127, 0, 0, 1})
|
if uaddr.IP.IsUnspecified() {
|
||||||
} else {
|
ln.SetFallbackIP(net.IP{127, 0, 0, 1})
|
||||||
ln.SetFallbackIP(uaddr.IP)
|
} else {
|
||||||
|
ln.SetFallbackIP(uaddr.IP)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ln.SetFallbackUDP(uaddr.Port)
|
ln.SetFallbackUDP(uaddr.Port)
|
||||||
|
|
||||||
// If an ENR endpoint is set explicitly on the command-line, override
|
// Override with explicit -extaddr address(es). A static IP is set per family,
|
||||||
// the information from the listening address. Note this is careful not
|
// and all specs share one UDP port because the node has a single socket.
|
||||||
// to set the UDP port if the external address doesn't have it.
|
|
||||||
extAddr := ctx.String(extAddrFlag.Name)
|
|
||||||
if extAddr != "" {
|
if extAddr != "" {
|
||||||
ip, port, ok := parseExtAddr(extAddr)
|
var extPort int
|
||||||
if !ok {
|
for spec := range strings.SplitSeq(extAddr, ",") {
|
||||||
exit(fmt.Errorf("-%s: invalid external address %q", extAddrFlag.Name, extAddr))
|
spec = strings.TrimSpace(spec)
|
||||||
|
if spec == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ip, port, ok := parseExtAddr(spec)
|
||||||
|
if !ok {
|
||||||
|
exit(fmt.Errorf("-%s: invalid external address %q", extAddrFlag.Name, spec))
|
||||||
|
}
|
||||||
|
ln.SetStaticIP(ip)
|
||||||
|
if port != 0 {
|
||||||
|
if extPort != 0 && port != extPort {
|
||||||
|
exit(fmt.Errorf("-%s: all addresses must announce the same UDP port, got %d and %d", extAddrFlag.Name, extPort, port))
|
||||||
|
}
|
||||||
|
extPort = port
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ln.SetStaticIP(ip)
|
if extPort != 0 {
|
||||||
if port != 0 {
|
ln.SetFallbackUDP(extPort)
|
||||||
ln.SetFallbackUDP(port)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue