From d4ad7752fdc8189b20b743398a848c668022b10e Mon Sep 17 00:00:00 2001 From: Grime Date: Wed, 11 Sep 2019 14:41:23 -0500 Subject: [PATCH] Added simple change to allow user detection of services *leaving* via TTL=0 in DNS-SD messages --- client.go | 13 ++++--------- examples/resolv/client.go | 6 +++++- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/client.go b/client.go index 53df90a9..9e808feb 100644 --- a/client.go +++ b/client.go @@ -191,8 +191,8 @@ func (c *client) mainloop(ctx context.Context, params *LookupParams) { } // Iterate through channels from listeners goroutines - var entries, sentEntries map[string]*ServiceEntry - sentEntries = make(map[string]*ServiceEntry) + var entries map[string]*ServiceEntry + sentEntries := make(map[string]uint32) for { select { case <-ctx.Done(): @@ -273,12 +273,7 @@ func (c *client) mainloop(ctx context.Context, params *LookupParams) { if len(entries) > 0 { for k, e := range entries { - if e.TTL == 0 { - delete(entries, k) - delete(sentEntries, k) - continue - } - if _, ok := sentEntries[k]; ok { + if oldTTL, ok := sentEntries[k]; ok && (e.TTL==oldTTL) { continue } @@ -295,7 +290,7 @@ func (c *client) mainloop(ctx context.Context, params *LookupParams) { // This is also a point to possibly stop probing actively for a // service entry. params.Entries <- e - sentEntries[k] = e + sentEntries[k] = e.TTL params.disableProbing() } // reset entries diff --git a/examples/resolv/client.go b/examples/resolv/client.go index 83186bc4..3ea49544 100644 --- a/examples/resolv/client.go +++ b/examples/resolv/client.go @@ -27,7 +27,11 @@ func main() { entries := make(chan *zeroconf.ServiceEntry) go func(results <-chan *zeroconf.ServiceEntry) { for entry := range results { - log.Println(entry) + if entry.TTL>0 { + log.Println("[+]", entry) + } else { + log.Println("[-]", entry) + } } log.Println("No more entries.") }(entries)