add a fallback to an ipv4 address if the the range can't find one

This commit is contained in:
Joshua Foster 2020-06-17 01:46:30 -04:00
parent aaf9103330
commit 7fc2ea8422
1 changed files with 11 additions and 0 deletions

View File

@ -43,6 +43,7 @@ func getHostIP(s string, network *net.IPNet) (string, error) {
return "", err
}
// look for an IP that is contained in the ip_wait_address range
for _, a := range addrs {
ipnet, ok := a.(*net.IPNet)
if ok && !ipnet.IP.IsLoopback() {
@ -51,5 +52,15 @@ func getHostIP(s string, network *net.IPNet) (string, error) {
}
}
}
// fallback to an ipv4 address if an IP is not found in the range
for _, a := range addrs {
ipnet, ok := a.(*net.IPNet)
if ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String(), nil
}
}
}
return "", fmt.Errorf("IP not found")
}