Skip to content

Commit

Permalink
dns: fix dnsutils (networkservicemesh#1330)
Browse files Browse the repository at this point in the history
* dns: use upd instead of tcp

Signed-off-by: Artem Glazychev <artem.glazychev@xored.com>

* Add tcp

Signed-off-by: Artem Glazychev <artem.glazychev@xored.com>

* Fix cache chain element

Signed-off-by: Artem Glazychev <artem.glazychev@xored.com>
Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com>
  • Loading branch information
glazychev-art authored and anastasia-malysheva committed Jul 27, 2022
1 parent d1aa808 commit 8bf6e12
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 29 deletions.
4 changes: 2 additions & 2 deletions pkg/tools/dnsutils/cache/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package cache

import "sync"

//go:generate go-syncmap -output sync_map.gen.go -type msgMap<string,*github.com/miekg/dns.Msg>
//go:generate go-syncmap -output sync_map.gen.go -type msgMap<github.com/miekg/dns.Question,*github.com/miekg/dns.Msg>

// msgMap is like a Go map[string]*dns.Msg but is safe for concurrent use
// msgMap is like a Go map[dns.Question]*dns.Msg but is safe for concurrent use
// by multiple goroutines without additional locking or coordination
type msgMap sync.Map
16 changes: 11 additions & 5 deletions pkg/tools/dnsutils/cache/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cache

import (
"context"
"sync"
"time"

"github.com/miekg/dns"
Expand All @@ -29,24 +30,27 @@ import (
)

type dnsCacheHandler struct {
cache *msgMap
cache *msgMap

lastTTLUpdate time.Time
m sync.Mutex
}

func (h *dnsCacheHandler) ServeDNS(ctx context.Context, rw dns.ResponseWriter, m *dns.Msg) {
h.updateTTL()
if v, ok := h.cache.Load(m.Question[0].Name); ok {
if val, ok := h.cache.Load(m.Question[0]); ok {
v := val.Copy()
if validateMsg(v) {
v.Id = m.Id
if err := rw.WriteMsg(v); err != nil {
log.FromContext(ctx).Warnf("got an error during write the message: %v", err.Error())
log.FromContext(ctx).WithField("dnsCacheHandler", "ServeDNS").Warnf("got an error during write the message: %v", err.Error())
dns.HandleFailed(rw, v)
return
}
return
}

h.cache.Delete(m.Question[0].Name)
h.cache.Delete(m.Question[0])
}

wrapper := responseWriterWrapper{
Expand All @@ -59,13 +63,15 @@ func (h *dnsCacheHandler) ServeDNS(ctx context.Context, rw dns.ResponseWriter, m

func (h *dnsCacheHandler) updateTTL() {
now := time.Now()
h.m.Lock()
defer h.m.Unlock()

diff := uint32(now.Sub(h.lastTTLUpdate).Seconds())
if diff == 0 {
return
}

h.cache.Range(func(key string, value *dns.Msg) bool {
h.cache.Range(func(key dns.Question, value *dns.Msg) bool {
for i := range value.Answer {
if value.Answer[i].Header().Ttl < diff {
value.Answer[i].Header().Ttl = 0
Expand Down
2 changes: 1 addition & 1 deletion pkg/tools/dnsutils/cache/response_writer_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type responseWriterWrapper struct {

func (r *responseWriterWrapper) WriteMsg(m *dns.Msg) error {
if m != nil && m.Rcode == dns.RcodeSuccess {
r.cache.Store(m.Question[0].Name, m)
r.cache.Store(m.Question[0], m)
}
return r.ResponseWriter.WriteMsg(m)
}
16 changes: 8 additions & 8 deletions pkg/tools/dnsutils/cache/sync_map.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/tools/dnsutils/connect/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ func (c *connectDNSHandler) ServeDNS(ctx context.Context, rp dns.ResponseWriter,
var resp, _, err = client.Exchange(msg, c.connectTO.Host)

if err != nil {
log.FromContext(ctx).Warnf("got an error during exchanging: %v", err.Error())
log.FromContext(ctx).WithField("connectDNSHandler", "ServeDNS").Warnf("got an error during exchanging: %v", err.Error())
dns.HandleFailed(rp, msg)
return
}

if err = rp.WriteMsg(resp); err != nil {
log.FromContext(ctx).Warnf("got an error during write the message: %v", err.Error())
log.FromContext(ctx).WithField("connectDNSHandler", "ServeDNS").Warnf("got an error during write the message: %v", err.Error())
dns.HandleFailed(rp, msg)
return
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/tools/dnsutils/dnsconfigs/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ func (h *dnsConfigsHandler) ServeDNS(ctx context.Context, rp dns.ResponseWriter,

h.configs.Range(func(key string, value []*networkservice.DNSConfig) bool {
for _, conf := range value {
ips := make([]url.URL, len(conf.DnsServerIps))
for i, ip := range conf.DnsServerIps {
ips[i] = url.URL{Scheme: "tcp", Host: ip}
for _, ip := range conf.DnsServerIps {
dnsIPs = append(dnsIPs,
url.URL{Scheme: "udp", Host: ip},
url.URL{Scheme: "tcp", Host: ip})
}

dnsIPs = append(dnsIPs, ips...)
searchDomains = append(searchDomains, conf.SearchDomains...)
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/tools/dnsutils/dnsconfigs/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ func TestDNSConfigs(t *testing.T) {
require.Contains(t, domains, "net")

urls := check.URLs
require.Equal(t, len(urls), 3)
require.Equal(t, len(urls), 6)
require.Contains(t, urls, "udp://7.7.7.7")
require.Contains(t, urls, "tcp://7.7.7.7")
require.Contains(t, urls, "udp://1.1.1.1")
require.Contains(t, urls, "tcp://1.1.1.1")
require.Contains(t, urls, "udp://9.9.9.9")
require.Contains(t, urls, "tcp://9.9.9.9")
}
6 changes: 3 additions & 3 deletions pkg/tools/dnsutils/fanout/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (h *fanoutHandler) ServeDNS(ctx context.Context, rw dns.ResponseWriter, msg
timeout := time.Until(deadline)

if len(connectTO) == 0 {
log.FromContext(ctx).Error("no urls to fanout")
log.FromContext(ctx).WithField("fanoutHandler", "ServeDNS").Error("no urls to fanout")
dns.HandleFailed(rw, msg)
return
}
Expand All @@ -62,7 +62,7 @@ func (h *fanoutHandler) ServeDNS(ctx context.Context, rw dns.ResponseWriter, msg

var resp, _, err = client.Exchange(msg, address)
if err != nil {
log.FromContext(ctx).Warnf("got an error during exchanging: %v", err.Error())
log.FromContext(ctx).WithField("fanoutHandler", "ServeDNS").Warnf("got an error during exchanging with address %v: %v", address, err.Error())
responseCh <- nil
return
}
Expand All @@ -79,7 +79,7 @@ func (h *fanoutHandler) ServeDNS(ctx context.Context, rw dns.ResponseWriter, msg
}

if err := rw.WriteMsg(resp); err != nil {
log.FromContext(ctx).Warnf("got an error during write the message: %v", err.Error())
log.FromContext(ctx).WithField("fanoutHandler", "ServeDNS").Warnf("got an error during write the message: %v", err.Error())
dns.HandleFailed(rw, msg)
return
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/tools/dnsutils/noloop/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type noloopDNSHandler struct{ ids sync.Map }

func (n *noloopDNSHandler) ServeDNS(ctx context.Context, rp dns.ResponseWriter, m *dns.Msg) {
if _, loaded := n.ids.LoadOrStore(m.Id, struct{}{}); loaded {
log.FromContext(ctx).Errorf("loop is not allowed: query: %v", m.String())
log.FromContext(ctx).WithField("noloopDNSHandler", "ServeDNS").Errorf("loop is not allowed: query: %v", m.String())
dns.HandleFailed(rp, m)
return
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/tools/dnsutils/searches/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (h *searchDomainsHandler) ServeDNS(ctx context.Context, rw dns.ResponseWrit
if resp != nil && resp.Rcode == dns.RcodeSuccess {
resp.Question = m.Question
if err := rw.WriteMsg(resp); err != nil {
log.FromContext(ctx).Warnf("got an error during write the message: %v", err.Error())
log.FromContext(ctx).WithField("searchDomainsHandler", "ServeDNS").Warnf("got an error during write the message: %v", err.Error())
dns.HandleFailed(rw, resp)
return
}
Expand Down

0 comments on commit 8bf6e12

Please sign in to comment.