Skip to content

Commit 3dc3b12

Browse files
committed
Add SUCCESS with empty Answer-section as negative NoData response handling
Signed-off-by: AliveDevil <[email protected]>
1 parent 4b16e00 commit 3dc3b12

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

alternate.go

+22
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ type Alternate struct {
1919
handlers []HandlerWithCallbacks
2020
}
2121

22+
// Use RCode that is out-of-range for any DNS response
23+
// RCodes can be any of 4 to 16 bits long
24+
const RcodeNoData int = -1
25+
2226
type rule struct {
2327
original bool
2428
handler HandlerWithCallbacks
@@ -55,6 +59,11 @@ func (f Alternate) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
5559
rulesIndex = nw.Msg.Rcode
5660
}
5761

62+
// if rcode is SUCCESS, and no answer is given, use RcodeNoData as hint for negative response.
63+
if rulesIndex == dns.RcodeSuccess && isEmpty(r) {
64+
rulesIndex = RcodeNoData
65+
}
66+
5867
if u, ok := f.rules[rulesIndex]; ok {
5968
if u.original && originalRequest != nil {
6069
return u.handler.ServeDNS(ctx, w, originalRequest)
@@ -69,3 +78,16 @@ func (f Alternate) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
6978

7079
// Name implements the Handler interface.
7180
func (f Alternate) Name() string { return "alternate" }
81+
82+
func isEmpty(r *dns.Msg) bool {
83+
if len(r.Answer) == 0 {
84+
return true
85+
}
86+
87+
for _, r := range r.Answer {
88+
if r != nil {
89+
return false
90+
}
91+
}
92+
return true
93+
}

setup.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ func getRCodes(c *caddy.Controller) ([]int, error) {
101101
var rc int
102102
var ok bool
103103

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

0 commit comments

Comments
 (0)