-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
160 lines (146 loc) · 4.05 KB
/
functions.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package resolver
import (
"github.com/miekg/dns"
)
var dnsRecordTypes = map[uint16]string{
1: "A",
2: "NS",
5: "CNAME",
6: "SOA",
12: "PTR",
15: "MX",
16: "TXT",
17: "RP",
18: "AFSDB",
24: "SIG", // Predecessor to RRSIG, included for completeness
25: "KEY", // Predecessor to DNSKEY
28: "AAAA",
29: "LOC",
33: "SRV",
35: "NAPTR",
36: "KX",
37: "CERT",
39: "DNAME",
41: "OPT", // Pseudo-record for EDNS (Extended DNS)
43: "DS",
44: "SSHFP", // SSH Public Key Fingerprint
45: "IPSECKEY", // IPsec Key
46: "RRSIG",
47: "NSEC",
48: "DNSKEY",
49: "DHCID", // DHCP Identifier
50: "NSEC3",
51: "NSEC3PARAM",
52: "TLSA",
53: "SMIMEA", // S/MIME certificate association
55: "HIP", // Host Identity Protocol
57: "NINFO", // (Experimental, rarely used)
59: "CDS", // Child DS, related to DNSSEC delegation
60: "CDNSKEY", // Child DNSKEY, related to DNSSEC delegation
61: "OPENPGPKEY", // OpenPGP public key
62: "CSYNC", // Child-To-Parent Synchronization
63: "ZONEMD", // Zone Message Digest
64: "SVCB", // Service Binding
65: "HTTPS", // HTTPS-specific Service Binding
99: "SPF", // Sender Policy Framework, typically TXT now
100: "UINFO", // User Information
101: "UID", // User ID
102: "GID", // Group ID
103: "UNSPEC", // Unspecified Information
108: "EUI48", // Extended Unique Identifier (48-bit)
109: "EUI64", // Extended Unique Identifier (64-bit)
249: "TKEY", // Transaction Key, for DNS security
250: "TSIG", // Transaction Signature, for DNS security
251: "IXFR", // Incremental Zone Transfer
252: "AXFR", // Full Zone Transfer
255: "ANY", // Query for all record types
256: "URI", // URI record
257: "CAA",
32768: "TA", // Trust Anchor, experimental
32769: "DLV", // DNSSEC Lookaside Validation, obsolete
}
func TypeToString(rrtype uint16) string {
if name, ok := dnsRecordTypes[rrtype]; ok {
return name
} else {
return "unknown"
}
}
//---
var dnsRCodes = map[int]string{
0: "NoError", // RcodeSuccess
1: "FormErr", // RcodeFormatError
2: "ServFail", // RcodeServerFailure
3: "NXDomain", // RcodeNameError
4: "NotImp", // RcodeNotImplemented
5: "Refused", // RcodeRefused
6: "YXDomain", // RcodeYXDomain
7: "YXRRSet", // RcodeYXRrset
8: "NXRRSet", // RcodeNXRrset
9: "NotAuth", // RcodeNotAuth
10: "NotZone", // RcodeNotZone
16: "BADSIG", // RcodeBadSig and RcodeBadVers
17: "BADKEY", // RcodeBadKey
18: "BADTIME", // RcodeBadTime
19: "BADMODE", // RcodeBadMode
20: "BADNAME", // RcodeBadName
21: "BADALG", // RcodeBadAlg
22: "BADTRUNC", // RcodeBadTrunc
23: "BADCOOKIE", // RcodeBadCookie
}
func RcodeToString(rcode int) string {
if name, ok := dnsRCodes[rcode]; ok {
return name
} else {
return "unknown"
}
}
//---
func isSetDO(msg *dns.Msg) bool {
for _, extra := range msg.Extra {
if opt, ok := extra.(*dns.OPT); ok {
return opt.Do()
}
}
return false
}
func canonicalName(name string) string {
return dns.CanonicalName(name)
}
func extractRecords[T dns.RR](rr []dns.RR) []T {
result := make([]T, 0, len(rr))
for _, record := range rr {
if typedRecord, ok := record.(T); ok {
result = append(result, typedRecord)
}
}
return result
}
func recordsOfTypeExist(rr []dns.RR, t uint16) bool {
for _, record := range rr {
if record.Header().Rrtype == t {
return true
}
}
return false
}
func extractRecordsOfType(rr []dns.RR, t uint16) []dns.RR {
r := make([]dns.RR, 0, len(rr))
for _, record := range rr {
if record.Header().Rrtype == t {
r = append(r, record)
}
}
return r
}
func recordsOfNameAndTypeExist(rr []dns.RR, name string, t uint16) bool {
for _, record := range rr {
if record.Header().Rrtype == t && record.Header().Name == name {
return true
}
}
return false
}
func namesEqual(s1, s2 string) bool {
return dns.CanonicalName(s1) == dns.CanonicalName(s2)
}