1package dnsutil 2 3import ( 4 "errors" 5 6 "github.com/miekg/dns" 7) 8 9// TrimZone removes the zone component from q. It returns the trimmed 10// name or an error is zone is longer then qname. The trimmed name will be returned 11// without a trailing dot. 12func TrimZone(q string, z string) (string, error) { 13 zl := dns.CountLabel(z) 14 i, ok := dns.PrevLabel(q, zl) 15 if ok || i-1 < 0 { 16 return "", errors.New("trimzone: overshot qname: " + q + "for zone " + z) 17 } 18 // This includes the '.', remove on return 19 return q[:i-1], nil 20} 21