Skip to content

Commit

Permalink
use netip to determine ipv4 and ipv6
Browse files Browse the repository at this point in the history
  • Loading branch information
matkam committed Jan 7, 2025
1 parent a6851a2 commit 664ea01
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,25 @@ func suitableType(target string) string {
return endpoint.RecordTypeCNAME
}

// isIPv6String reports whether the target string is an IPv6 address,
// including IPv4-mapped IPv6 addresses.
func isIPv6String(target string) bool {
netIP, err := netip.ParseAddr(target)
if err != nil {
return false
}
return netIP.Is6()
}

// isIPv4String reports whether the target string is an IPv4 address.
func isIPv4String(target string) bool {
netIP, err := netip.ParseAddr(target)
if err != nil {
return false
}
return netIP.Is4()
}

// endpointsForHostname returns the endpoint objects for each host-target combination.
func endpointsForHostname(hostname string, targets endpoint.Targets, ttl endpoint.TTL, providerSpecific endpoint.ProviderSpecific, setIdentifier string, resource string) []*endpoint.Endpoint {
var endpoints []*endpoint.Endpoint
Expand Down Expand Up @@ -385,14 +404,3 @@ func waitForDynamicCacheSync(ctx context.Context, factory dynamicInformerFactory
}
return nil
}

// isIPv6String returns if ip is IPv6.
func isIPv6String(ip string) bool {
netIP := net.ParseIP(ip)
return netIP != nil && netIP.To4() == nil
}

func isIPv4String(input string) bool {
netIP := net.ParseIP(input)
return netIP != nil && netIP.To4() != nil && !strings.Contains(input, ":")
}

0 comments on commit 664ea01

Please sign in to comment.