-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
48 lines (43 loc) · 936 Bytes
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package recursive
import (
"math"
"net/netip"
"strconv"
"github.com/miekg/dns"
)
func DnsTypeToString(qtype uint16) string {
if s, ok := dns.TypeToString[qtype]; ok {
return s
}
return strconv.Itoa(int(qtype))
}
func AddrFromRR(rr dns.RR) netip.Addr {
switch v := rr.(type) {
case *dns.A:
if ip, ok := netip.AddrFromSlice(v.A); ok {
return ip.Unmap()
}
case *dns.AAAA:
if ip, ok := netip.AddrFromSlice(v.AAAA); ok {
return ip
}
}
return netip.Addr{}
}
// MinTTL returns the lowest resource record TTL in the message, or -1 if there are no records.
func MinTTL(msg *dns.Msg) int {
minTTL := math.MaxInt
for _, rr := range msg.Answer {
minTTL = min(minTTL, int(rr.Header().Ttl))
}
for _, rr := range msg.Ns {
minTTL = min(minTTL, int(rr.Header().Ttl))
}
for _, rr := range msg.Extra {
minTTL = min(minTTL, int(rr.Header().Ttl))
}
if minTTL == math.MaxInt {
minTTL = -1
}
return minTTL
}