p2p/enode: allow DNS names in enode URLs (#18524)

pull/19999/head
alexwang 5 years ago committed by Felix Lange
parent 4d358b9fc0
commit b90cdbaa79
  1. 12
      p2p/enode/urlv4.go
  2. 6
      p2p/enode/urlv4_test.go

@ -125,15 +125,17 @@ func parseComplete(rawurl string) (*Node, error) {
return nil, fmt.Errorf("invalid public key (%v)", err) return nil, fmt.Errorf("invalid public key (%v)", err)
} }
// Parse the IP address. // Parse the IP address.
host, port, err := net.SplitHostPort(u.Host) ips, err := net.LookupIP(u.Hostname())
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid host: %v", err) return nil, err
} }
if ip = net.ParseIP(host); ip == nil { ip = ips[0]
return nil, errors.New("invalid IP address") // Ensure the IP is 4 bytes long for IPv4 addresses.
if ipv4 := ip.To4(); ipv4 != nil {
ip = ipv4
} }
// Parse the port numbers. // Parse the port numbers.
if tcpPort, err = strconv.ParseUint(port, 10, 16); err != nil { if tcpPort, err = strconv.ParseUint(u.Port(), 10, 16); err != nil {
return nil, errors.New("invalid port") return nil, errors.New("invalid port")
} }
udpPort = tcpPort udpPort = tcpPort

@ -61,8 +61,8 @@ var parseNodeTests = []struct {
}, },
// Complete node URLs with IP address and ports // Complete node URLs with IP address and ports
{ {
input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@hostname:3", input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@invalid.:3",
wantError: `invalid IP address`, wantError: `no such host`,
}, },
{ {
input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:foo", input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:foo",
@ -76,7 +76,7 @@ var parseNodeTests = []struct {
input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:52150", input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:52150",
wantResult: NewV4( wantResult: NewV4(
hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
net.IP{0x7f, 0x0, 0x0, 0x1}, net.IP{127, 0, 0, 1},
52150, 52150,
52150, 52150,
), ),

Loading…
Cancel
Save