From 21b3f66b8b97f13985a2a2eaa05947df35b67186 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Thu, 6 Dec 2018 20:11:45 +0100 Subject: [PATCH] fix IP parsing in local dns client --- features/dns/localdns/client.go | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/features/dns/localdns/client.go b/features/dns/localdns/client.go index fcd955bb..143d9297 100644 --- a/features/dns/localdns/client.go +++ b/features/dns/localdns/client.go @@ -21,7 +21,18 @@ func (*Client) Close() error { return nil } // LookupIP implements Client. func (*Client) LookupIP(host string) ([]net.IP, error) { - return net.LookupIP(host) + ips, err := net.LookupIP(host) + if err != nil { + return nil, err + } + parsedIPs := make([]net.IP, 0, len(ips)) + for _, ip := range parsedIPs { + parsed := net.IPAddress(ip) + if parsed != nil { + parsedIPs = append(parsedIPs, parsed.IP()) + } + } + return parsedIPs, nil } // LookupIPv4 implements IPv4Lookup. @@ -30,11 +41,10 @@ func (c *Client) LookupIPv4(host string) ([]net.IP, error) { if err != nil { return nil, err } - var ipv4 []net.IP + ipv4 := make([]net.IP, 0, len(ips)) for _, ip := range ips { - parsed := net.IPAddress(ip) - if parsed.Family().IsIPv4() { - ipv4 = append(ipv4, parsed.IP()) + if len(ip) == net.IPv4len { + ipv4 = append(ipv4, ip) } } return ipv4, nil @@ -46,11 +56,10 @@ func (c *Client) LookupIPv6(host string) ([]net.IP, error) { if err != nil { return nil, err } - var ipv6 []net.IP + ipv6 := make([]net.IP, 0, len(ips)) for _, ip := range ips { - parsed := net.IPAddress(ip) - if parsed.Family().IsIPv6() { - ipv6 = append(ipv6, parsed.IP()) + if len(ip) == net.IPv6len { + ipv6 = append(ipv6, ip) } } return ipv6, nil