Repository: miekg/dns
Pull request #1151 (2020-08-17 13:30:23)

answer queries with no matching handler with RcodeRefused
... instead of RcodeServerFailure.

codecov-commenter: # [Codecov](https://codecov.io/gh/miekg/dns/pull/1151?src=pr&el=h1) Report
> Merging [#1151](https://codecov.io/gh/miekg/dns/pull/1151?src=pr&el=desc) into [master](https://codecov.io/gh/miekg/dns/commit/da812eed45cba1ce4c978e746039483064b8f92d&el=desc) will **decrease** coverage by `0.00%`.
> The diff coverage is `0.00%`.

[![Impacted file tree graph](https://codecov.io/gh/miekg/dns/pull/1151/graphs/tree.svg?width=650&height=150&src=pr&token=uT9fYPSeKT)](https://codecov.io/gh/miekg/dns/pull/1151?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #1151      +/-   ##
==========================================
- Coverage   55.90%   55.89%   -0.01%     
==========================================
  Files          41       41              
  Lines       10134    10136       +2     
==========================================
+ Hits         5665     5666       +1     
- Misses       3464     3465       +1     
  Partials     1005     1005              
```


| [Impacted Files](https://codecov.io/gh/miekg/dns/pull/1151?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [serve\_mux.go](https://codecov.io/gh/miekg/dns/pull/1151/diff?src=pr&el=tree#diff-c2VydmVfbXV4Lmdv) | `72.34% <0.00%> (-3.22%)` | :arrow_down: |
| [server.go](https://codecov.io/gh/miekg/dns/pull/1151/diff?src=pr&el=tree#diff-c2VydmVyLmdv) | `70.71% <0.00%> (+0.27%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/miekg/dns/pull/1151?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/miekg/dns/pull/1151?src=pr&el=footer). Last update [da812ee...08aacde](https://codecov.io/gh/miekg/dns/pull/1151?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/msg_helpers.go b/msg_helpers.go
index cbcab57b..6fb64475 100644
--- a/msg_helpers.go
+++ b/msg_helpers.go
@@ -683,6 +683,13 @@ func packDataAplPrefix(p *APLPrefix, msg []byte, off int) (int, error) {
 	if p.Negation {
 		n = 0x80
 	}
+
+	// trim trailing zero bytes as specified in RFC3123 Sections 4.1 and 4.2.
+	i := len(addr) - 1
+	for ; i >= 0 && addr[i] == 0; i-- {
+	}
+	addr = addr[:i+1]
+
 	adflen := uint8(len(addr)) & 0x7f
 	off, err = packUint8(n|adflen, msg, off)
 	if err != nil {
diff --git a/serve_mux.go b/serve_mux.go
index aadb0bf0..e7f36e22 100644
--- a/serve_mux.go
+++ b/serve_mux.go
@@ -91,7 +91,7 @@ func (mux *ServeMux) HandleRemove(pattern string) {
 // are redirected to the parent zone (if that is also registered),
 // otherwise the child gets the query.
 //
-// If no handler is found, or there is no question, a standard SERVFAIL
+// If no handler is found, or there is no question, a standard REFUSED
 // message is returned
 func (mux *ServeMux) ServeDNS(w ResponseWriter, req *Msg) {
 	var h Handler
@@ -102,7 +102,7 @@ func (mux *ServeMux) ServeDNS(w ResponseWriter, req *Msg) {
 	if h != nil {
 		h.ServeDNS(w, req)
 	} else {
-		HandleFailed(w, req)
+		handleRefused(w, req)
 	}
 }
 
diff --git a/server.go b/server.go
index 3cf1a024..77b43dea 100644
--- a/server.go
+++ b/server.go
@@ -78,7 +78,15 @@ type response struct {
 	writer         Writer            // writer to output the raw DNS bits
 }
 
+// handleRefused returns a HandlerFunc that returns REFUSED for every request it gets.
+func handleRefused(w ResponseWriter, r *Msg) {
+	m := new(Msg)
+	m.SetRcode(r, RcodeRefused)
+	w.WriteMsg(m)
+}
+
 // HandleFailed returns a HandlerFunc that returns SERVFAIL for every request it gets.
+// Deprecated: This function is going away.
 func HandleFailed(w ResponseWriter, r *Msg) {
 	m := new(Msg)
 	m.SetRcode(r, RcodeServerFailure)


Test patch:
diff --git a/msg_helpers_test.go b/msg_helpers_test.go
index 70e3b238..c1fa195a 100644
--- a/msg_helpers_test.go
+++ b/msg_helpers_test.go
@@ -180,6 +180,20 @@ func TestPackDataAplPrefix(t *testing.T) {
 			net.CIDRMask(48, 128),
 			[]byte{0x00, 0x02, 0x30, 0x06, 0x20, 0x01, 0x0d, 0xb8, 0xca, 0xfe},
 		},
+		{
+			"with trailing zero bytes 2:2001:db8:cafe::0/64",
+			false,
+			net.ParseIP("2001:db8:cafe::"),
+			net.CIDRMask(64, 128),
+			[]byte{0x00, 0x02, 0x40, 0x06, 0x20, 0x01, 0x0d, 0xb8, 0xca, 0xfe},
+		},
+		{
+			"no non-zero bytes 2::/16",
+			false,
+			net.ParseIP("::"),
+			net.CIDRMask(16, 128),
+			[]byte{0x00, 0x02, 0x10, 0x00},
+		},
 		{
 			"!2:2001:db8::/32",
 			true,
@@ -209,6 +223,11 @@ func TestPackDataAplPrefix(t *testing.T) {
 			if !bytes.Equal(tt.expect, out[:off]) {
 				t.Fatalf("expected output %02x, got %02x", tt.expect, out[:off])
 			}
+			// Make sure the packed bytes would be accepted by its own unpack
+			_, _, err = unpackDataAplPrefix(out, 0)
+			if err != nil {
+				t.Fatalf("expected no error, got %q", err)
+			}
 		})
 	}
 }