dns/sanitize.go

79 lines
1.8 KiB
Go
Raw Normal View History

package dns
// Dedup removes identical RRs from rrs. It preserves the original ordering.
2015-08-25 05:52:56 +00:00
// The lowest TTL of any duplicates is used in the remaining one.
func Dedup(rrs []RR) []RR {
m := make(map[string]RR)
2015-08-25 05:52:56 +00:00
keys := make([]string, 0, len(rrs))
2015-08-25 09:32:08 +00:00
for _, r := range rrs {
2015-08-26 13:13:14 +00:00
key := normalizedString(r)
keys = append(keys, key)
if _, ok := m[key]; ok {
// Shortest TTL wins.
if m[key].Header().Ttl > r.Header().Ttl {
m[key].Header().Ttl = r.Header().Ttl
}
continue
}
2015-08-25 21:35:10 +00:00
m[key] = r
}
// If the length of the result map equals the amount of RRs we got,
2015-08-25 05:52:56 +00:00
// it means they were all different. We can then just return the original rrset.
2015-08-26 13:13:14 +00:00
if len(m) == len(rrs) {
return rrs
}
2015-08-25 21:35:10 +00:00
ret := make([]RR, 0, len(rrs))
for i, r := range rrs {
2015-08-26 13:13:14 +00:00
// If keys[i] lives in the map, we should copy and remove it.
2015-08-25 21:35:10 +00:00
if _, ok := m[keys[i]]; ok {
delete(m, keys[i])
ret = append(ret, r)
}
if len(m) == 0 {
break
}
}
2015-08-25 21:35:10 +00:00
return ret
}
// normalizedString returns a normalized string from r. The TTL
2015-08-26 13:13:14 +00:00
// is removed and the domain name is lowercased. We go from this:
// DomainName<TAB>TTL<TAB>CLASS<TAB>TYPE<TAB>RDATA to:
// lowercasename<TAB>CLASS<TAB>TYPE...
func normalizedString(r RR) string {
// A string Go DNS makes has: domainname<TAB>TTL<TAB>...
b := []byte(r.String())
2015-08-26 13:13:14 +00:00
// find the first non-escaped tab, then another, so we capture where the TTL lives.
esc := false
ttlStart, ttlEnd := 0, 0
2015-08-26 13:13:14 +00:00
for i := 0; i < len(b) && ttlEnd == 0; i++ {
switch {
case b[i] == '\\':
esc = !esc
case b[i] == '\t' && !esc:
if ttlStart == 0 {
ttlStart = i
continue
}
if ttlEnd == 0 {
ttlEnd = i
}
2015-08-26 13:13:14 +00:00
case b[i] >= 'A' && b[i] <= 'Z' && !esc:
b[i] += 32
default:
esc = false
}
}
// remove TTL.
copy(b[ttlStart:], b[ttlEnd:])
cut := ttlEnd - ttlStart
2015-08-26 13:13:14 +00:00
return string(b[:len(b)-cut])
2015-08-25 12:52:53 +00:00
}