aboutsummaryrefslogtreecommitdiff
path: root/request
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2018-03-15 08:42:49 +0000
committerGravatar GitHub <noreply@github.com> 2018-03-15 08:42:49 +0000
commitb813706bafc61ed3c779223893d25793c7c43726 (patch)
tree46db2d9788940bbc890426b1c5fc8c42bf21ff13 /request
parent3e6489ef67270db1ad8963d572e0dc79960a6781 (diff)
downloadcoredns-b813706bafc61ed3c779223893d25793c7c43726.tar.gz
coredns-b813706bafc61ed3c779223893d25793c7c43726.tar.zst
coredns-b813706bafc61ed3c779223893d25793c7c43726.zip
request: add match function (#1615)
Diffstat (limited to 'request')
-rw-r--r--request/request.go18
-rw-r--r--request/request_test.go20
2 files changed, 38 insertions, 0 deletions
diff --git a/request/request.go b/request/request.go
index 0ce341a1b..0bca1a405 100644
--- a/request/request.go
+++ b/request/request.go
@@ -361,6 +361,24 @@ func (r *Request) Clear() {
r.name = ""
}
+// Match checks if the reply matches the qname and qtype from the request, it returns
+// false when they don't match.
+func (r *Request) Match(reply *dns.Msg) bool {
+ if len(reply.Question) != 1 {
+ return false
+ }
+
+ if strings.ToLower(reply.Question[0].Name) != r.Name() {
+ return false
+ }
+
+ if reply.Question[0].Qtype != r.QType() {
+ return false
+ }
+
+ return true
+}
+
const (
// TODO(miek): make this less awkward.
doTrue = 1
diff --git a/request/request_test.go b/request/request_test.go
index e6fcf6935..ff9efcfd0 100644
--- a/request/request_test.go
+++ b/request/request_test.go
@@ -109,6 +109,26 @@ func TestRequestScrubExtra(t *testing.T) {
}
}
+func TestRequestMatch(t *testing.T) {
+ st := testRequest()
+ reply := new(dns.Msg)
+
+ reply.SetQuestion("example.com.", dns.TypeMX)
+ if b := st.Match(reply); b {
+ t.Errorf("failed to match %s %d, got %t, expected %t", "example.com.", dns.TypeMX, b, false)
+ }
+
+ reply.SetQuestion("example.com.", dns.TypeA)
+ if b := st.Match(reply); !b {
+ t.Errorf("failed to match %s %d, got %t, expected %t", "example.com.", dns.TypeA, b, true)
+ }
+
+ reply.SetQuestion("example.org.", dns.TypeA)
+ if b := st.Match(reply); b {
+ t.Errorf("failed to match %s %d, got %t, expected %t", "example.org.", dns.TypeA, b, false)
+ }
+}
+
func BenchmarkRequestDo(b *testing.B) {
st := testRequest()