dnsxl/reverse.go

35 lines
756 B
Go

package dnsxl
import (
"net"
"strconv"
"strings"
)
// reverseName converts an IPv4 or IPv6 address into the reversed name
// notation as described in RFC 1034 (for IPv4) or RFC 3596 (for IPv6).
func reverseName(ip net.IP) string {
const hexdig = "0123456789abcdef"
var b strings.Builder
if ip4 := ip.To4(); ip4 != nil {
b.WriteString(strconv.Itoa(int(ip4[3])))
b.WriteByte('.')
b.WriteString(strconv.Itoa(int(ip4[2])))
b.WriteByte('.')
b.WriteString(strconv.Itoa(int(ip4[1])))
b.WriteByte('.')
b.WriteString(strconv.Itoa(int(ip4[0])))
} else {
for i := 15; i >= 0; i-- {
x := ip[i]
b.WriteByte(hexdig[x&15])
b.WriteByte('.')
b.WriteByte(hexdig[x>>4])
if i > 0 {
b.WriteByte('.')
}
}
}
return b.String()
}