From 69344eee5fc5d3ed7569d066734ba087001a477f Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Wed, 4 Jul 2012 09:48:59 +0200 Subject: [PATCH] Add ReverseAddr functie --- defaults.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/defaults.go b/defaults.go index a19b8271..a6ab0234 100644 --- a/defaults.go +++ b/defaults.go @@ -1,5 +1,12 @@ package dns +import ( + "net" + "strconv" +) + +const hexDigit = "0123456789abcdef" + // Everything is assumed in the ClassINET class. If // you need other classes you are on your own. @@ -311,3 +318,32 @@ func Fqdn(s string) string { } return s + "." } + +// Copied from the official Go code + +// ReverseAddr returns the in-addr.arpa. or ip6.arpa. hostname of the IP +// address addr suitable for rDNS (PTR) record lookup or an error if it fails +// to parse the IP address. +func ReverseAddr(addr string) (arpa string, err error) { + ip := net.ParseIP(addr) + if ip == nil { + return "", &Error{Err: "unrecognized address", Name: addr} + } + if ip.To4() != nil { + return strconv.Itoa(int(ip[15])) + "." + strconv.Itoa(int(ip[14])) + "." + strconv.Itoa(int(ip[13])) + "." + + strconv.Itoa(int(ip[12])) + ".in-addr.arpa.", nil + } + // Must be IPv6 + buf := make([]byte, 0, len(ip)*4+len("ip6.arpa.")) + // Add it, in reverse, to the buffer + for i := len(ip) - 1; i >= 0; i-- { + v := ip[i] + buf = append(buf, hexDigit[v&0xF]) + buf = append(buf, '.') + buf = append(buf, hexDigit[v>>4]) + buf = append(buf, '.') + } + // Append "ip6.arpa." and return (buf already has the final .) + buf = append(buf, "ip6.arpa."...) + return string(buf), nil +}