Skip to content

Commit

Permalink
Add SUCCESS with empty Answer-section as negative NoData response han…
Browse files Browse the repository at this point in the history
…dling
  • Loading branch information
AliveDevil committed May 23, 2024
1 parent 4b16e00 commit 31c04dd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
22 changes: 22 additions & 0 deletions alternate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ type Alternate struct {
handlers []HandlerWithCallbacks
}

// Use RCode that is out-of-range for any DNS response
// RCodes can be any of 4 to 16 bits long
const RcodeNoData int = -1

type rule struct {
original bool
handler HandlerWithCallbacks
Expand Down Expand Up @@ -55,6 +59,11 @@ func (f Alternate) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
rulesIndex = nw.Msg.Rcode
}

// if rcode is SUCCESS, and no answer is given, use RcodeNoData as hint for negative response.
if rulesIndex == dns.RcodeSuccess && isEmpty(r) {
rulesIndex = RcodeNoData
}

if u, ok := f.rules[rulesIndex]; ok {
if u.original && originalRequest != nil {
return u.handler.ServeDNS(ctx, w, originalRequest)
Expand All @@ -69,3 +78,16 @@ func (f Alternate) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms

// Name implements the Handler interface.
func (f Alternate) Name() string { return "alternate" }

func isEmpty(r *dns.Msg) bool {
if len(r.Answer) == 0 {
return true
}

for _, r := range r.Answer {
if r != nil {
return false
}
}
return true
}
4 changes: 3 additions & 1 deletion setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ func getRCodes(c *caddy.Controller) ([]int, error) {
var rc int
var ok bool

if rc, ok = dns.StringToRcode[strings.ToUpper(rcode)]; !ok {
if rcode = strings.ToUpper(rcode); rcode == "NODATA" {
rc = RcodeNoData
} else if rc, ok = dns.StringToRcode[rcode]; !ok {
return nil, fmt.Errorf("%s is not a valid rcode", rcode)
}

Expand Down

0 comments on commit 31c04dd

Please sign in to comment.