More cleanup - needs to think a little about NewSOA()

This commit is contained in:
Miek Gieben 2016-03-22 10:29:48 +00:00
parent 22dade9e12
commit 1a7f0deadd
5 changed files with 138 additions and 114 deletions

View file

@ -16,90 +16,59 @@ func (e Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (i
m.RecursionAvailable = true
m.Compress = true
return 0, nil
}
// TODO(miek): get current zone when serving multiple
zone := "."
// only needs state and current zone name we are auth for.
/*
func (s *server) ServeDNS(w dns.ResponseWriter, req *dns.Msg) {
q := req.Question[0]
name := strings.ToLower(q.Name)
switch q.Qtype {
case dns.TypeNS:
records, extra, err := s.NSRecords(q, s.config.dnsDomain)
if isEtcdNameError(err, s) {
m = s.NameError(req)
return
switch state.Type() {
case "A":
records, err := e.A(zone, state, nil)
case "AAAA":
records, err := e.AAAA(zone, state, nil)
fallthrough
case "TXT":
records, err := e.TXT(zone, state)
fallthrough
case "CNAME":
records, err := e.CNAME(zone, state)
fallthrough
case "MX":
records, extra, err := e.MX(zone, state)
fallthrough
case "SRV":
records, extra, err := e.SRV(zone, state)
if isEtcdNameError(err) {
NameError(zone, state)
return dns.RcodeNameError, nil
}
m.Answer = append(m.Answer, records...)
m.Extra = append(m.Extra, extra...)
case dns.TypeA, dns.TypeAAAA:
records, err := s.AddressRecords(q, name, nil, bufsize, dnssec, false)
if isEtcdNameError(err, s) {
m = s.NameError(req)
return
}
m.Answer = append(m.Answer, records...)
case dns.TypeTXT:
records, err := s.TXTRecords(q, name)
if isEtcdNameError(err, s) {
m = s.NameError(req)
return
}
m.Answer = append(m.Answer, records...)
case dns.TypeCNAME:
records, err := s.CNAMERecords(q, name)
if isEtcdNameError(err, s) {
m = s.NameError(req)
return
}
m.Answer = append(m.Answer, records...)
case dns.TypeMX:
records, extra, err := s.MXRecords(q, name, bufsize, dnssec)
if isEtcdNameError(err, s) {
m = s.NameError(req)
return
}
m.Answer = append(m.Answer, records...)
m.Extra = append(m.Extra, extra...)
default:
fallthrough // also catch other types, so that they return NODATA
case dns.TypeSRV:
records, extra, err := s.SRVRecords(q, name, bufsize, dnssec)
if err != nil {
if isEtcdNameError(err, s) {
m = s.NameError(req)
return
}
logf("got error from backend: %s", err)
if q.Qtype == dns.TypeSRV { // Otherwise NODATA
m = s.ServerFailure(req)
return
}
// TODO(miek): err or nil in this case?
return dns.RcodeServerFailure, err
}
// if we are here again, check the types, because an answer may only
// be given for SRV. All other types should return NODATA, the
// NXDOMAIN part is handled in the above code. TODO(miek): yes this
// can be done in a more elegant manor.
if q.Qtype == dns.TypeSRV {
if len(records) > 0 {
m.Answer = append(m.Answer, records...)
}
if len(extra) > 0 {
m.Extra = append(m.Extra, extra...)
}
default:
// Nodata response
// also catch other types, so that they return NODATA
}
if len(m.Answer) == 0 { // NODATA response
m.Ns = []dns.RR{s.NewSOA()}
m.Ns[0].Header().Ttl = s.config.MinTtl
}
return e.Next.ServeDNS(ctx, w, r)
}
// etcNameError checks if the error is ErrorCodeKeyNotFound from etcd.
func isEtcdNameError(err error, s *server) bool {
if e, ok := err.(etcd.Error); ok && e.Code == etcd.ErrorCodeKeyNotFound {
return true
}
return false
// 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)
m.Ns = []dns.RR{NewSOA()}
m.Ns[0].Header().Ttl = minTtl
state.W.WriteMsg(m)
}
// NoData write a nodata response to the client.
func NoData(zone string, state middleware.State) {
// TODO(miek): write it
}
*/