diff options
author | 2016-09-07 11:10:16 +0100 | |
---|---|---|
committer | 2016-09-07 11:10:16 +0100 | |
commit | d1f17fa7e061d91aa0af7e1fb959a68db899c812 (patch) | |
tree | 0f828a0e7bd8ca86051a721dae48e8c19a0a2f0e /middleware/pkg/dnsutil/reverse.go | |
parent | 684330fd28f65a7a8d1ad01fb4c6c5db78240f29 (diff) | |
download | coredns-d1f17fa7e061d91aa0af7e1fb959a68db899c812.tar.gz coredns-d1f17fa7e061d91aa0af7e1fb959a68db899c812.tar.zst coredns-d1f17fa7e061d91aa0af7e1fb959a68db899c812.zip |
Cleanup: put middleware helper functions in pkgs (#245)
Move all (almost all) Go files in middleware into their
own packages. This makes for better naming and discoverability.
Lot of changes elsewhere to make this change.
The middleware.State was renamed to request.Request which is better,
but still does not cover all use-cases. It was also moved out middleware
because it is used by `dnsserver` as well.
A pkg/dnsutil packages was added for shared, handy, dns util functions.
All normalize functions are now put in normalize.go
Diffstat (limited to 'middleware/pkg/dnsutil/reverse.go')
-rw-r--r-- | middleware/pkg/dnsutil/reverse.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/middleware/pkg/dnsutil/reverse.go b/middleware/pkg/dnsutil/reverse.go new file mode 100644 index 000000000..a360432f3 --- /dev/null +++ b/middleware/pkg/dnsutil/reverse.go @@ -0,0 +1,40 @@ +package dnsutil + +import "strings" + +// ExtractAddressFromReverse turns a standard PTR reverse record name +// into an IP address. This works for ipv4 or ipv6. +// +// 54.119.58.176.in-addr.arpa. becomes 176.58.119.54. If the conversion +// failes the empty string is returned. +func ExtractAddressFromReverse(reverseName string) string { + search := "" + + switch { + case strings.HasSuffix(reverseName, v4arpaSuffix): + search = strings.TrimSuffix(reverseName, v4arpaSuffix) + case strings.HasSuffix(reverseName, v6arpaSuffix): + search = strings.TrimSuffix(reverseName, v6arpaSuffix) + default: + return "" + } + + // Reverse the segments and then combine them. + segments := reverse(strings.Split(search, ".")) + return strings.Join(segments, ".") +} + +func reverse(slice []string) []string { + for i := 0; i < len(slice)/2; i++ { + j := len(slice) - i - 1 + slice[i], slice[j] = slice[j], slice[i] + } + return slice +} + +const ( + // v4arpaSuffix is the reverse tree suffix for v4 IP addresses. + v4arpaSuffix = ".in-addr.arpa." + // v6arpaSuffix is the reverse tree suffix for v6 IP addresses. + v6arpaSuffix = ".ip6.arpa." +) |