Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix searches dns chain element #1417

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}