| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | package file
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import "github.com/miekg/dns"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Result is the result of a Lookup
 | 
					
						
							|  |  |  | type Result int
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const (
 | 
					
						
							|  |  |  | 	Success Result = iota
 | 
					
						
							|  |  |  | 	NameError
 | 
					
						
							|  |  |  | 	NoData // aint no offical NoData return code.
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Lookup looks up qname and qtype in the zone, when do is true DNSSEC are included as well.
 | 
					
						
							|  |  |  | // Two sets of records are returned, one for the answer and one for the additional section.
 | 
					
						
							|  |  |  | func (z *Zone) Lookup(qname string, qtype uint16, do bool) ([]dns.RR, []dns.RR, Result) {
 | 
					
						
							|  |  |  | 	var rr dns.RR
 | 
					
						
							|  |  |  | 	mk, known := dns.TypeToRR[qtype]
 | 
					
						
							|  |  |  | 	if !known {
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 		an, ad, _ := z.lookupSOA(do)
 | 
					
						
							|  |  |  | 		return an, ad, NameError
 | 
					
						
							|  |  |  | 		// Uhm...? rr = new(RFC3597) ??
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	} else {
 | 
					
						
							|  |  |  | 		rr = mk()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 	rr.Header().Rrtype = qtype // this is pretty nonobvious
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	if qtype == dns.TypeSOA {
 | 
					
						
							|  |  |  | 		return z.lookupSOA(do)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rr.Header().Name = qname
 | 
					
						
							|  |  |  | 	elem := z.Tree.Get(rr)
 | 
					
						
							|  |  |  | 	if elem == nil {
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 		an, ad, _ := z.lookupSOA(do)
 | 
					
						
							|  |  |  | 		return an, ad, NameError
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	rrs := elem.Types(dns.TypeCNAME)
 | 
					
						
							|  |  |  | 	if len(rrs) > 0 { // should only ever be 1 actually; TODO(miek) check for this?
 | 
					
						
							|  |  |  | 		rr.Header().Name = rrs[0].(*dns.CNAME).Target
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 		return z.lookupCNAME(rrs, rr, do)
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rrs = elem.Types(qtype)
 | 
					
						
							|  |  |  | 	if len(rrs) == 0 {
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 		an, ad, _ := z.lookupSOA(do)
 | 
					
						
							|  |  |  | 		return an, ad, NoData
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if do {
 | 
					
						
							|  |  |  | 		sigs := elem.Types(dns.TypeRRSIG)
 | 
					
						
							|  |  |  | 		sigs = signatureForSubType(sigs, qtype)
 | 
					
						
							|  |  |  | 		if len(sigs) > 0 {
 | 
					
						
							|  |  |  | 			rrs = append(rrs, sigs...)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	return rrs, nil, Success
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (z *Zone) lookupSOA(do bool) ([]dns.RR, []dns.RR, Result) {
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 	if do {
 | 
					
						
							|  |  |  | 		ret := append([]dns.RR{z.SOA}, z.SIG...)
 | 
					
						
							|  |  |  | 		return ret, nil, Success
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	return []dns.RR{z.SOA}, nil, Success
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | func (z *Zone) lookupCNAME(rrs []dns.RR, rr dns.RR, do bool) ([]dns.RR, []dns.RR, Result) {
 | 
					
						
							|  |  |  | 	elem := z.Tree.Get(rr)
 | 
					
						
							|  |  |  | 	if elem == nil {
 | 
					
						
							|  |  |  | 		return rrs, nil, Success
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	extra := cnameForType(elem.All(), rr.Header().Rrtype)
 | 
					
						
							|  |  |  | 	if do {
 | 
					
						
							|  |  |  | 		sigs := elem.Types(dns.TypeRRSIG)
 | 
					
						
							|  |  |  | 		sigs = signatureForSubType(sigs, rr.Header().Rrtype)
 | 
					
						
							|  |  |  | 		if len(sigs) > 0 {
 | 
					
						
							|  |  |  | 			extra = append(extra, sigs...)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return rrs, extra, Success
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func cnameForType(targets []dns.RR, origQtype uint16) []dns.RR {
 | 
					
						
							|  |  |  | 	ret := []dns.RR{}
 | 
					
						
							|  |  |  | 	for _, target := range targets {
 | 
					
						
							|  |  |  | 		if target.Header().Rrtype == origQtype {
 | 
					
						
							|  |  |  | 			ret = append(ret, target)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return ret
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | // signatureForSubType range through the signature and return the correct
 | 
					
						
							|  |  |  | // ones for the subtype.
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | func signatureForSubType(rrs []dns.RR, subtype uint16) []dns.RR {
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	sigs := []dns.RR{}
 | 
					
						
							|  |  |  | 	for _, sig := range rrs {
 | 
					
						
							|  |  |  | 		if s, ok := sig.(*dns.RRSIG); ok {
 | 
					
						
							|  |  |  | 			if s.TypeCovered == subtype {
 | 
					
						
							|  |  |  | 				sigs = append(sigs, s)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return sigs
 | 
					
						
							|  |  |  | }
 |