Fix HostPortOrFile to support IPv6 addresses with zone (#3527)

1. The HostPortOrFile tests don't have any IPv6 tests. This adds some.
2. The HostPortOrFile breaks if any of the addresses have IPv6 zone
defined. ParseIP does not handle %zone anymore.

Signed-off-by: Brad P. Crochet <brad@redhat.com>
This commit is contained in:
Brad P. Crochet 2020-01-16 14:47:39 -05:00 committed by Miek Gieben
parent b7977402d6
commit aa8c325d4a
2 changed files with 35 additions and 4 deletions

View file

@ -4,12 +4,23 @@ import (
"fmt"
"net"
"os"
"strings"
"github.com/coredns/coredns/plugin/pkg/transport"
"github.com/miekg/dns"
)
// Strips the zone, but preserves any port that comes after the zone
func stripZone(host string) string {
if strings.Contains(host, "%") {
lastPercent := strings.LastIndex(host, "%")
newHost := host[:lastPercent]
return newHost
}
return host
}
// HostPortOrFile parses the strings in s, each string can either be a
// address, [scheme://]address:port or a filename. The address part is checked
// and in case of filename a resolv.conf like file is (assumed) and parsed and
@ -21,10 +32,11 @@ func HostPortOrFile(s ...string) ([]string, error) {
trans, host := Transport(h)
addr, _, err := net.SplitHostPort(host)
if err != nil {
// Parse didn't work, it is not a addr:port combo
if net.ParseIP(host) == nil {
// Not an IP address.
hostNoZone := stripZone(host)
if net.ParseIP(hostNoZone) == nil {
ss, err := tryFile(host)
if err == nil {
servers = append(servers, ss...)
@ -47,8 +59,7 @@ func HostPortOrFile(s ...string) ([]string, error) {
continue
}
if net.ParseIP(addr) == nil {
// Not an IP address.
if net.ParseIP(stripZone(addr)) == nil {
ss, err := tryFile(host)
if err == nil {
servers = append(servers, ss...)

View file

@ -34,6 +34,26 @@ func TestHostPortOrFile(t *testing.T) {
"127.0.0.1:53",
false,
},
{
"fe80::1",
"[fe80::1]:53",
false,
},
{
"fe80::1%ens3",
"[fe80::1%ens3]:53",
false,
},
{
"[fd01::1]:153",
"[fd01::1]:153",
false,
},
{
"[fd01::1%ens3]:153",
"[fd01::1%ens3]:153",
false,
},
}
err := ioutil.WriteFile("resolv.conf", []byte("nameserver 127.0.0.1\n"), 0600)