Skip to content

Commit

Permalink
Fix searches dns chain element (#1417)
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Glazychev <artem.glazychev@xored.com>
  • Loading branch information
glazychev-art authored Feb 7, 2023
1 parent 51ab81f commit c1a6ac4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
26 changes: 9 additions & 17 deletions pkg/tools/dnsutils/searches/handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Cisco and/or its affiliates.
// Copyright (c) 2022-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -31,28 +31,20 @@ type searchDomainsHandler struct {
}

func (h *searchDomainsHandler) ServeDNS(ctx context.Context, rw dns.ResponseWriter, m *dns.Msg) {
domains := SearchDomains(ctx)

r := &responseWriter{
ResponseWriter: rw,
Responses: make([]*dns.Msg, len(domains)+1),
Index: 0,
}

next.Handler(ctx).ServeDNS(ctx, r, m)
for _, d := range append([]string{""}, SearchDomains(ctx)...) {
r := &responseWriter{
ResponseWriter: rw,
}

for _, d := range SearchDomains(ctx) {
newMsg := m.Copy()
newMsg.Question[0].Name = dns.Fqdn(newMsg.Question[0].Name + d)
next.Handler(ctx).ServeDNS(ctx, r, newMsg)
}

for _, resp := range r.Responses {
if resp != nil && resp.Rcode == dns.RcodeSuccess {
resp.Question = m.Question
if err := rw.WriteMsg(resp); err != nil {
if r.Response != nil && r.Response.Rcode == dns.RcodeSuccess {
r.Response.Question = m.Question
if err := rw.WriteMsg(r.Response); err != nil {
log.FromContext(ctx).WithField("searchDomainsHandler", "ServeDNS").Warnf("got an error during write the message: %v", err.Error())
dns.HandleFailed(rw, resp)
dns.HandleFailed(rw, r.Response)
return
}
return
Expand Down
4 changes: 2 additions & 2 deletions pkg/tools/dnsutils/searches/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Cisco and/or its affiliates.
// Copyright (c) 2022-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestDomainSearches(t *testing.T) {
handler.ServeDNS(ctx, rw, m)

resp := rw.Response.Copy()
require.Equal(t, check.Count, 4)
require.Equal(t, check.Count, 2)
require.Equal(t, resp.MsgHdr.Rcode, dns.RcodeSuccess)
require.NotNil(t, resp.Answer)
require.Equal(t, resp.Answer[0].(*dns.A).A.String(), "1.1.1.1")
Expand Down
8 changes: 3 additions & 5 deletions pkg/tools/dnsutils/searches/response_writer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Cisco and/or its affiliates.
// Copyright (c) 2022-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -22,12 +22,10 @@ import (

type responseWriter struct {
dns.ResponseWriter
Responses []*dns.Msg
Index int
Response *dns.Msg
}

func (r *responseWriter) WriteMsg(m *dns.Msg) error {
r.Responses[r.Index] = m
r.Index++
r.Response = m
return nil
}

0 comments on commit c1a6ac4

Please sign in to comment.