p2p/enode: simplify parseComplete

This commit is contained in:
Felix Lange 2019-08-22 11:18:18 +02:00 committed by GitHub
parent 9040b4c284
commit 7a21dc1265
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -119,25 +119,17 @@ func parseComplete(rawurl string) (*Node, error) {
return nil, fmt.Errorf("invalid node ID (%v)", err)
}
// Parse the IP address.
host, port, err := net.SplitHostPort(u.Host)
ips, err := net.LookupIP(u.Hostname())
if err != nil {
return nil, fmt.Errorf("invalid host: %v", err)
}
if ip = net.ParseIP(host); ip == nil {
// attempt to look up IP addresses if host is a FQDN
lookupIPs, err := net.LookupIP(host)
if err != nil {
return nil, errors.New("invalid IP address")
}
// set to first ip by default
ip = lookupIPs[0]
return nil, err
}
ip := ips[0]
// Ensure the IP is 4 bytes long for IPv4 addresses.
if ipv4 := ip.To4(); ipv4 != nil {
ip = ipv4
}
// 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")
}
udpPort = tcpPort