diff --git a/nameserver/cache.go b/nameserver/cache.go index 8cfd424c..4e788c41 100644 --- a/nameserver/cache.go +++ b/nameserver/cache.go @@ -34,6 +34,11 @@ const ( stResolved entryStatus = iota // resolved ) +var statusToString = map[entryStatus]string{ + stPending: "pending", + stResolved: "resolved", +} + const ( CacheNoLocalReplies uint8 = 1 << iota // not found in local network (stored in the cache so we skip another local lookup or some time) ) @@ -166,6 +171,19 @@ func (e *cacheEntry) setReply(reply *dns.Msg, ttl int, flags uint8, now time.Tim return (prevValidUntil != e.validUntil) } +func (e *cacheEntry) String() string { + var buf bytes.Buffer + q := e.question + fmt.Fprintf(&buf, "'%s'[%s]: ", q.Name, dns.TypeToString[q.Qtype]) + if e.Flags&CacheNoLocalReplies != 0 { + fmt.Fprintf(&buf, "neg-local") + } else { + fmt.Fprintf(&buf, "%s", statusToString[e.Status]) + } + fmt.Fprintf(&buf, "(%d bytes)", e.ReplyLen) + return buf.String() +} + ////////////////////////////////////////////////////////////////////////////////////// // An entriesPtrHeap is a min-heap of cache entries. diff --git a/nameserver/mdns_client.go b/nameserver/mdns_client.go index 878ef98c..e6c91025 100644 --- a/nameserver/mdns_client.go +++ b/nameserver/mdns_client.go @@ -1,6 +1,8 @@ package nameserver import ( + "bytes" + "fmt" "github.com/miekg/dns" "math" "net" @@ -52,6 +54,24 @@ func (r Response) Equal(r2 *Response) bool { return true } +func (i Response) String() string { + var buf bytes.Buffer + if i.err != nil { + fmt.Fprintf(&buf, "%s", i.err) + } else { + if len(i.Name()) > 0 { + fmt.Fprintf(&buf, "%s", i.Name()) + } + if !i.IP().IsUnspecified() { + fmt.Fprintf(&buf, "[%s]", i.IP()) + } + if i.ttl > 0 { + fmt.Fprintf(&buf, "(TTL:%d)", i.TTL()) + } + } + return buf.String() +} + type responseInfo struct { timeout time.Time // if no answer by this time, give up insistent bool // insistent queries are not removed on the first reply diff --git a/nameserver/mdns_client_test.go b/nameserver/mdns_client_test.go index bc7a8621..7880c2a4 100644 --- a/nameserver/mdns_client_test.go +++ b/nameserver/mdns_client_test.go @@ -76,6 +76,8 @@ func RunLocalMulticastServer() (*dns.Server, error) { } func setup(t *testing.T) (*MDNSClient, *dns.Server, error) { + InitDefaultLogging(testing.Verbose()) + server, err := RunLocalMulticastServer() if err != nil { t.Fatalf("Unable to run test server: %s. No default multicast interface?", err)