| 
									
										
										
										
											2016-04-02 16:56:16 +01:00
										 |  |  | package tree
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | import "github.com/miekg/dns"
 | 
					
						
							| 
									
										
										
										
											2016-04-02 16:56:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | // Elem is an element in the tree.
 | 
					
						
							| 
									
										
										
										
											2016-04-02 16:56:16 +01:00
										 |  |  | type Elem struct {
 | 
					
						
							| 
									
										
										
										
											2016-10-08 15:22:31 +01:00
										 |  |  | 	m    map[uint16][]dns.RR
 | 
					
						
							|  |  |  | 	name string // owner name
 | 
					
						
							| 
									
										
										
										
											2016-04-02 16:56:16 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // newElem returns a new elem.
 | 
					
						
							|  |  |  | func newElem(rr dns.RR) *Elem {
 | 
					
						
							|  |  |  | 	e := Elem{m: make(map[uint16][]dns.RR)}
 | 
					
						
							|  |  |  | 	e.m[rr.Header().Rrtype] = []dns.RR{rr}
 | 
					
						
							|  |  |  | 	return &e
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | // Types returns the types of the records in e. The returned list is not sorted.
 | 
					
						
							|  |  |  | func (e *Elem) Types() []uint16 {
 | 
					
						
							|  |  |  | 	t := make([]uint16, len(e.m))
 | 
					
						
							|  |  |  | 	i := 0
 | 
					
						
							|  |  |  | 	for ty := range e.m {
 | 
					
						
							|  |  |  | 		t[i] = ty
 | 
					
						
							|  |  |  | 		i++
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return t
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Type returns the RRs with type qtype from e.
 | 
					
						
							|  |  |  | func (e *Elem) Type(qtype uint16) []dns.RR { return e.m[qtype] }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TypeForWildcard returns the RRs with type qtype from e. The ownername returned is set to qname.
 | 
					
						
							|  |  |  | func (e *Elem) TypeForWildcard(qtype uint16, qname string) []dns.RR {
 | 
					
						
							| 
									
										
										
										
											2016-10-08 15:22:31 +01:00
										 |  |  | 	rrs := e.m[qtype]
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 	if rrs == nil {
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	copied := make([]dns.RR, len(rrs))
 | 
					
						
							|  |  |  | 	for i := range rrs {
 | 
					
						
							|  |  |  | 		copied[i] = dns.Copy(rrs[i])
 | 
					
						
							|  |  |  | 		copied[i].Header().Name = qname
 | 
					
						
							| 
									
										
										
										
											2016-04-02 16:56:16 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 	return copied
 | 
					
						
							| 
									
										
										
										
											2016-04-02 16:56:16 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // All returns all RRs from e, regardless of type.
 | 
					
						
							|  |  |  | func (e *Elem) All() []dns.RR {
 | 
					
						
							|  |  |  | 	list := []dns.RR{}
 | 
					
						
							|  |  |  | 	for _, rrs := range e.m {
 | 
					
						
							|  |  |  | 		list = append(list, rrs...)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return list
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Name returns the name for this node.
 | 
					
						
							|  |  |  | func (e *Elem) Name() string {
 | 
					
						
							| 
									
										
										
										
											2016-10-08 15:22:31 +01:00
										 |  |  | 	if e.name != "" {
 | 
					
						
							|  |  |  | 		return e.name
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-04-02 16:56:16 +01:00
										 |  |  | 	for _, rrs := range e.m {
 | 
					
						
							| 
									
										
										
										
											2016-10-08 15:22:31 +01:00
										 |  |  | 		e.name = rrs[0].Header().Name
 | 
					
						
							|  |  |  | 		return e.name
 | 
					
						
							| 
									
										
										
										
											2016-04-02 16:56:16 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	return ""
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | // Empty returns true is e does not contain any RRs, i.e. is an empty-non-terminal.
 | 
					
						
							|  |  |  | func (e *Elem) Empty() bool { return len(e.m) == 0 }
 | 
					
						
							| 
									
										
										
										
											2016-10-08 15:22:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | // Insert inserts rr into e. If rr is equal to existing RRs, the RR will be added anyway.
 | 
					
						
							| 
									
										
										
										
											2016-04-02 16:56:16 +01:00
										 |  |  | func (e *Elem) Insert(rr dns.RR) {
 | 
					
						
							|  |  |  | 	t := rr.Header().Rrtype
 | 
					
						
							|  |  |  | 	if e.m == nil {
 | 
					
						
							|  |  |  | 		e.m = make(map[uint16][]dns.RR)
 | 
					
						
							|  |  |  | 		e.m[t] = []dns.RR{rr}
 | 
					
						
							|  |  |  | 		return
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	rrs, ok := e.m[t]
 | 
					
						
							|  |  |  | 	if !ok {
 | 
					
						
							|  |  |  | 		e.m[t] = []dns.RR{rr}
 | 
					
						
							|  |  |  | 		return
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rrs = append(rrs, rr)
 | 
					
						
							|  |  |  | 	e.m[t] = rrs
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | // Delete removes all RRs of type rr.Header().Rrtype from e.
 | 
					
						
							|  |  |  | func (e *Elem) Delete(rr dns.RR) {
 | 
					
						
							| 
									
										
										
										
											2016-04-02 16:56:16 +01:00
										 |  |  | 	if e.m == nil {
 | 
					
						
							|  |  |  | 		return
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 	t := rr.Header().Rrtype
 | 
					
						
							|  |  |  | 	delete(e.m, t)
 | 
					
						
							| 
									
										
										
										
											2016-04-02 16:56:16 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | // Less is a tree helper function that calls less.
 | 
					
						
							|  |  |  | func Less(a *Elem, name string) int { return less(name, a.Name()) }
 |