| 1 |
package dnsutil |
| 2 |
|
| 3 |
import ( |
| 4 |
"net" |
| 5 |
"net/netip" |
| 6 |
"strconv" |
| 7 |
"strings" |
| 8 |
) |
| 9 |
|
| 10 |
const ( |
| 11 |
// IP4arpa is the reverse tree suffix for v4 IP addresses. |
| 12 |
IP4arpa = ".in-addr.arpa." |
| 13 |
// IP6arpa is the reverse tree suffix for v6 IP addresses. |
| 14 |
IP6arpa = ".ip6.arpa." |
| 15 |
) |
| 16 |
|
| 17 |
// IsReverse returns 0 if name is not a reverse zone. Anything > 0 indicates |
| 18 |
// name is in a reverse zone. The returned integer will be [IPv4Family] for in-addr.arpa, (IPv4), |
| 19 |
// and [IPv6Family] for ip6.arpa, (IPv6). See [Family]. A valid name is assumed. |
| 20 |
func IsReverse(s string) int { |
| 21 |
if strings.HasSuffix(s, IP4arpa) { |
| 22 |
return IPv4Family |
| 23 |
} |
| 24 |
if strings.HasSuffix(s, IP6arpa) { |
| 25 |
return IPv6Family |
| 26 |
} |
| 27 |
return 0 |
| 28 |
} |
| 29 |
|
| 30 |
// ReverseAddr returns the in-addr.arpa. [IP4arpa] or ip6.arpa. [IP6arpa] hostname of the IP |
| 31 |
// address suitable for reverse DNS ([dns.PTR]) record lookups. Also see [AddrReverse]. |
| 32 |
func ReverseAddr(ip netip.Addr) (arpa string) { |
| 33 |
const hexDigit = "0123456789abcdef" |
| 34 |
|
| 35 |
if ip.Is4() { |
| 36 |
v4 := ip.As4() |
| 37 |
buf := make([]byte, 0, net.IPv4len*4+len(IP4arpa)) |
| 38 |
// Add it, in reverse, to the buffer |
| 39 |
for i := len(v4) - 1; i >= 0; i-- { |
| 40 |
buf = strconv.AppendInt(buf, int64(v4[i]), 10) |
| 41 |
buf = append(buf, '.') |
| 42 |
} |
| 43 |
// Append "in-addr.arpa." and return (buf already has the final .) |
| 44 |
buf = append(buf, IP4arpa[1:]...) |
| 45 |
return string(buf) |
| 46 |
} |
| 47 |
// Must be IPv6 |
| 48 |
buf := make([]byte, 0, net.IPv6len*4+len(IP6arpa)) |
| 49 |
v6 := ip.As16() |
| 50 |
// Add it, in reverse, to the buffer |
| 51 |
for i := len(v6) - 1; i >= 0; i-- { |
| 52 |
v := v6[i] |
| 53 |
buf = append(buf, hexDigit[v&0xF], '.', hexDigit[v>>4], '.') |
| 54 |
} |
| 55 |
// Append "ip6.arpa." and return (buf already has the final .) |
| 56 |
buf = append(buf, IP6arpa[1:]...) |
| 57 |
return string(buf) |
| 58 |
} |
| 59 |
|
| 60 |
// AddrReverse turns a standard [dns.PTR] reverse record name into an IP address. |
| 61 |
// 54.119.58.176.in-addr.arpa. becomes 176.58.119.54. Both in-addr.arpa [IP4arpa] and ip6.arpa [IP6arpa] |
| 62 |
// are supported. If the conversion fails nil is returned. Also see [ReverseAddr]. |
| 63 |
func AddrReverse(s string) (ip netip.Addr) { |
| 64 |
switch IsReverse(s) { |
| 65 |
case IPv4Family: |
| 66 |
var v4 [4]byte |
| 67 |
idx := 0 |
| 68 |
// Loop backwards through the bytes of the IPv4 address (d, c, b, a) |
| 69 |
// which appear in forward order in the reverse name (a.b.c.d). |
| 70 |
// e.g. 54.119.58.176.in-addr.arpa. -> 176.58.119.54 |
| 71 |
// 54 (byte 3) is first, 176 (byte 0) is last. |
| 72 |
for i := 3; i >= 0; i-- { |
| 73 |
if idx >= len(s) { |
| 74 |
return netip.Addr{} |
| 75 |
} |
| 76 |
if s[idx] < '0' || s[idx] > '9' { |
| 77 |
return netip.Addr{} |
| 78 |
} |
| 79 |
n := 0 |
| 80 |
for idx < len(s) && s[idx] >= '0' && s[idx] <= '9' { |
| 81 |
n = n*10 + int(s[idx]-'0') |
| 82 |
if n > 255 { |
| 83 |
return netip.Addr{} |
| 84 |
} |
| 85 |
idx++ |
| 86 |
} |
| 87 |
v4[i] = byte(n) |
| 88 |
|
| 89 |
// Consumed number, expect a dot. |
| 90 |
if idx >= len(s) || s[idx] != '.' { |
| 91 |
return netip.Addr{} |
| 92 |
} |
| 93 |
idx++ |
| 94 |
} |
| 95 |
// The remainder must be exactly "in-addr.arpa." |
| 96 |
if s[idx:] != "in-addr.arpa." { |
| 97 |
return netip.Addr{} |
| 98 |
} |
| 99 |
return netip.AddrFrom4(v4) |
| 100 |
|
| 101 |
case IPv6Family: |
| 102 |
var v6 [16]byte |
| 103 |
idx := 0 |
| 104 |
// 32 nibbles. |
| 105 |
// Reverse name: low nibble of byte 15, high nibble of byte 15, ... |
| 106 |
for i := range 32 { |
| 107 |
if idx >= len(s) { |
| 108 |
return netip.Addr{} |
| 109 |
} |
| 110 |
c := s[idx] |
| 111 |
var val byte |
| 112 |
switch { |
| 113 |
case c >= '0' && c <= '9': |
| 114 |
val = c - '0' |
| 115 |
case c >= 'a' && c <= 'f': |
| 116 |
val = c - 'a' + 10 |
| 117 |
case c >= 'A' && c <= 'F': |
| 118 |
val = c - 'A' + 10 |
| 119 |
default: |
| 120 |
return netip.Addr{} |
| 121 |
} |
| 122 |
|
| 123 |
// i=0 -> byte 15, low part |
| 124 |
// i=1 -> byte 15, high part |
| 125 |
// i=2 -> byte 14, low part |
| 126 |
pos := 15 - (i / 2) |
| 127 |
if i%2 == 0 { |
| 128 |
v6[pos] |= val |
| 129 |
} else { |
| 130 |
v6[pos] |= val << 4 |
| 131 |
} |
| 132 |
|
| 133 |
idx++ |
| 134 |
if idx >= len(s) || s[idx] != '.' { |
| 135 |
return netip.Addr{} |
| 136 |
} |
| 137 |
idx++ |
| 138 |
} |
| 139 |
if s[idx:] != "ip6.arpa." { |
| 140 |
return netip.Addr{} |
| 141 |
} |
| 142 |
return netip.AddrFrom16(v6) |
| 143 |
default: |
| 144 |
return netip.Addr{} |
| 145 |
} |
| 146 |
} |