mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 10:13:14 -04:00 
			
		
		
		
	Core: convert IP addresses to reverse zone (#838)
* Core: convert IP addresses to reverse zone If we see IP/mask syntax and the mask mod 8 == 0 we assume a reverse zone and convert to in-addr or .arpa. * typos * integration test * Addr is not used * core: clean up normalize Create a SplitHostPort function that can be used both from normalize.go and address.go. This removes some (not all!) duplication between the both and makes it work with reverse address notations. * More tests
This commit is contained in:
		| @@ -1,10 +1,10 @@ | ||||
| package dnsserver | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"net" | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/coredns/coredns/middleware" | ||||
|  | ||||
| 	"github.com/miekg/dns" | ||||
| ) | ||||
|  | ||||
| @@ -32,8 +32,6 @@ func Transport(s string) string { | ||||
|  | ||||
| // normalizeZone parses an zone string into a structured format with separate | ||||
| // host, and port portions, as well as the original input string. | ||||
| // | ||||
| // TODO(miek): possibly move this to middleware/normalize.go | ||||
| func normalizeZone(str string) (zoneAddr, error) { | ||||
| 	var err error | ||||
|  | ||||
| @@ -52,18 +50,9 @@ func normalizeZone(str string) (zoneAddr, error) { | ||||
| 		str = str[len(TransportGRPC+"://"):] | ||||
| 	} | ||||
|  | ||||
| 	host, port, err := net.SplitHostPort(str) | ||||
| 	host, port, err := middleware.SplitHostPort(str) | ||||
| 	if err != nil { | ||||
| 		host, port, err = net.SplitHostPort(str + ":") | ||||
| 		// no error check here; return err at end of function | ||||
| 	} | ||||
|  | ||||
| 	if len(host) > 255 { // TODO(miek): this should take escaping into account. | ||||
| 		return zoneAddr{}, fmt.Errorf("specified zone is too long: %d > 255", len(host)) | ||||
| 	} | ||||
| 	_, d := dns.IsDomainName(host) | ||||
| 	if !d { | ||||
| 		return zoneAddr{}, fmt.Errorf("zone is not a valid domain name: %s", host) | ||||
| 		return zoneAddr{}, err | ||||
| 	} | ||||
|  | ||||
| 	if port == "" { | ||||
| @@ -78,7 +67,7 @@ func normalizeZone(str string) (zoneAddr, error) { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return zoneAddr{Zone: strings.ToLower(dns.Fqdn(host)), Port: port, Transport: trans}, err | ||||
| 	return zoneAddr{Zone: dns.Fqdn(host), Port: port, Transport: trans}, nil | ||||
| } | ||||
|  | ||||
| // Supported transports. | ||||
|   | ||||
| @@ -12,6 +12,41 @@ func TestNormalizeZone(t *testing.T) { | ||||
| 		{".:54", "dns://.:54", false}, | ||||
| 		{"..", "://:", true}, | ||||
| 		{"..", "://:", true}, | ||||
| 		{".:", "://:", true}, | ||||
| 	} { | ||||
| 		addr, err := normalizeZone(test.input) | ||||
| 		actual := addr.String() | ||||
| 		if test.shouldErr && err == nil { | ||||
| 			t.Errorf("Test %d: Expected error, but there wasn't any", i) | ||||
| 		} | ||||
| 		if !test.shouldErr && err != nil { | ||||
| 			t.Errorf("Test %d: Expected no error, but there was one: %v", i, err) | ||||
| 		} | ||||
| 		if actual != test.expected { | ||||
| 			t.Errorf("Test %d: Expected %s but got %s", i, test.expected, actual) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestNormalizeZoneReverse(t *testing.T) { | ||||
| 	for i, test := range []struct { | ||||
| 		input     string | ||||
| 		expected  string | ||||
| 		shouldErr bool | ||||
| 	}{ | ||||
| 		{"2003::1/64", "dns://0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.3.0.0.2.ip6.arpa.:53", false}, | ||||
| 		{"2003::1/64.", "dns://2003::1/64.:53", false}, // OK, with closing dot the parse will fail. | ||||
| 		{"2003::1/64:53", "dns://0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.3.0.0.2.ip6.arpa.:53", false}, | ||||
| 		{"2003::1/64.:53", "dns://2003::1/64.:53", false}, | ||||
|  | ||||
| 		{"10.0.0.0/24", "dns://0.0.10.in-addr.arpa.:53", false}, | ||||
| 		{"10.0.0.0/24.", "dns://10.0.0.0/24.:53", false}, | ||||
| 		{"10.0.0.0/24:53", "dns://0.0.10.in-addr.arpa.:53", false}, | ||||
| 		{"10.0.0.0/24.:53", "dns://10.0.0.0/24.:53", false}, | ||||
|  | ||||
| 		// non %8==0 netmasks | ||||
| 		{"2003::53/67", "dns://2003::53/67.:53", false}, | ||||
| 		{"10.0.0.0/25.", "dns://10.0.0.0/25.:53", false}, | ||||
| 	} { | ||||
| 		addr, err := normalizeZone(test.input) | ||||
| 		actual := addr.String() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user