| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | package etcd
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							| 
									
										
										
										
											2016-03-22 22:44:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | 	"golang.org/x/net/context"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (e Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							|  |  |  | 	state := middleware.State{W: w, Req: r}
 | 
					
						
							| 
									
										
										
										
											2016-03-22 11:04:56 +00:00
										 |  |  | 	zone := middleware.Zones(e.Zones).Matches(state.Name())
 | 
					
						
							|  |  |  | 	if zone == "" {
 | 
					
						
							|  |  |  | 		return e.Next.ServeDNS(ctx, w, r)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	m := state.AnswerMessage()
 | 
					
						
							| 
									
										
										
										
											2016-03-22 11:04:56 +00:00
										 |  |  | 	m.Authoritative, m.RecursionAvailable, m.Compress = true, true, true
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 11:04:56 +00:00
										 |  |  | 	var (
 | 
					
						
							|  |  |  | 		records, extra []dns.RR
 | 
					
						
							|  |  |  | 		err            error
 | 
					
						
							|  |  |  | 	)
 | 
					
						
							| 
									
										
										
										
											2016-03-22 10:29:48 +00:00
										 |  |  | 	switch state.Type() {
 | 
					
						
							|  |  |  | 	case "A":
 | 
					
						
							| 
									
										
										
										
											2016-03-22 11:04:56 +00:00
										 |  |  | 		records, err = e.A(zone, state, nil)
 | 
					
						
							| 
									
										
										
										
											2016-03-22 10:29:48 +00:00
										 |  |  | 	case "AAAA":
 | 
					
						
							| 
									
										
										
										
											2016-03-22 11:04:56 +00:00
										 |  |  | 		records, err = e.AAAA(zone, state, nil)
 | 
					
						
							| 
									
										
										
										
											2016-03-22 10:29:48 +00:00
										 |  |  | 	case "TXT":
 | 
					
						
							| 
									
										
										
										
											2016-03-22 11:04:56 +00:00
										 |  |  | 		records, err = e.TXT(zone, state)
 | 
					
						
							| 
									
										
										
										
											2016-03-22 10:29:48 +00:00
										 |  |  | 	case "CNAME":
 | 
					
						
							| 
									
										
										
										
											2016-03-22 11:04:56 +00:00
										 |  |  | 		records, err = e.CNAME(zone, state)
 | 
					
						
							| 
									
										
										
										
											2016-03-22 10:29:48 +00:00
										 |  |  | 	case "MX":
 | 
					
						
							| 
									
										
										
										
											2016-03-22 11:04:56 +00:00
										 |  |  | 		records, extra, err = e.MX(zone, state)
 | 
					
						
							| 
									
										
										
										
											2016-03-22 10:29:48 +00:00
										 |  |  | 	case "SRV":
 | 
					
						
							| 
									
										
										
										
											2016-03-22 11:04:56 +00:00
										 |  |  | 		records, extra, err = e.SRV(zone, state)
 | 
					
						
							| 
									
										
										
										
											2016-03-22 10:29:48 +00:00
										 |  |  | 	default:
 | 
					
						
							| 
									
										
										
										
											2016-03-22 22:44:50 +00:00
										 |  |  | 		// For SOA and NS we might still want this
 | 
					
						
							|  |  |  | 		// and use dns.<zones> as the name to put these
 | 
					
						
							|  |  |  | 		// also for stub
 | 
					
						
							| 
									
										
										
										
											2016-03-22 11:04:56 +00:00
										 |  |  | 		// rwrite and return
 | 
					
						
							| 
									
										
										
										
											2016-03-22 10:29:48 +00:00
										 |  |  | 		// Nodata response
 | 
					
						
							|  |  |  | 		// also catch other types, so that they return NODATA
 | 
					
						
							| 
									
										
										
										
											2016-03-22 11:04:56 +00:00
										 |  |  | 		return 0, nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if isEtcdNameError(err) {
 | 
					
						
							|  |  |  | 		NameError(zone, state)
 | 
					
						
							|  |  |  | 		return dns.RcodeNameError, nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return dns.RcodeServerFailure, err
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-22 11:04:56 +00:00
										 |  |  | 	if len(records) > 0 {
 | 
					
						
							|  |  |  | 		m.Answer = append(m.Answer, records...)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if len(extra) > 0 {
 | 
					
						
							|  |  |  | 		m.Extra = append(m.Extra, extra...)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	state.W.WriteMsg(m)
 | 
					
						
							|  |  |  | 	return 0, nil
 | 
					
						
							| 
									
										
										
										
											2016-03-22 10:29:48 +00:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 10:29:48 +00:00
										 |  |  | // NameError writes a name error to the client.
 | 
					
						
							|  |  |  | func NameError(zone string, state middleware.State) {
 | 
					
						
							|  |  |  | 	m := new(dns.Msg)
 | 
					
						
							|  |  |  | 	m.SetRcode(state.Req, dns.RcodeNameError)
 | 
					
						
							| 
									
										
										
										
											2016-03-22 11:04:56 +00:00
										 |  |  | 	m.Ns = []dns.RR{SOA(zone)}
 | 
					
						
							| 
									
										
										
										
											2016-03-22 10:29:48 +00:00
										 |  |  | 	state.W.WriteMsg(m)
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 10:29:48 +00:00
										 |  |  | // NoData write a nodata response to the client.
 | 
					
						
							|  |  |  | func NoData(zone string, state middleware.State) {
 | 
					
						
							|  |  |  | 	// TODO(miek): write it
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:36:55 +00:00
										 |  |  | }
 |