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

Add client option to detect disconnected clients #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
81 changes: 47 additions & 34 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ var initialQueryInterval = 4 * time.Second

// Client structure encapsulates both IPv4/IPv6 UDP connections.
type client struct {
ipv4conn *ipv4.PacketConn
ipv6conn *ipv6.PacketConn
ifaces []net.Interface
ipv4conn *ipv4.PacketConn
ipv6conn *ipv6.PacketConn
ifaces []net.Interface
unannouncements bool
}

type clientOpts struct {
listenOn IPType
ifaces []net.Interface
listenOn IPType
ifaces []net.Interface
unannouncements bool
}

// ClientOption fills the option struct to configure intefaces, etc.
Expand All @@ -63,6 +65,14 @@ func SelectIfaces(ifaces []net.Interface) ClientOption {
}
}

// Emit an entry with an expiry in the past if a previously emitted entry is unannounced.
// This is never guaranteed to occur, but can speed up detection of disconnected clients.
func Unannouncements() ClientOption {
return func(o *clientOpts) {
o.unannouncements = true
}
}

// Browse for all services of a given type in a given domain.
// Received entries are sent on the entries channel.
// It blocks until the context is canceled (or an error occurs).
Expand Down Expand Up @@ -157,9 +167,10 @@ func newClient(opts clientOpts) (*client, error) {
}

return &client{
ipv4conn: ipv4conn,
ipv6conn: ipv6conn,
ifaces: ifaces,
ipv4conn: ipv4conn,
ipv6conn: ipv6conn,
ifaces: ifaces,
unannouncements: opts.unannouncements,
}, nil
}

Expand All @@ -177,12 +188,12 @@ func (c *client) mainloop(ctx context.Context, params *lookupParams) {
}

// Iterate through channels from listeners goroutines
var entries map[string]*ServiceEntry
sentEntries := make(map[string]*ServiceEntry)

ticker := time.NewTicker(cleanupFreq)
defer ticker.Stop()
for {
var entries map[string]*ServiceEntry
var now time.Time
select {
case <-ctx.Done():
Expand Down Expand Up @@ -269,35 +280,37 @@ func (c *client) mainloop(ctx context.Context, params *lookupParams) {
}
}

if len(entries) > 0 {
for k, e := range entries {
if !e.Expiry.After(now) {
delete(entries, k)
delete(sentEntries, k)
continue
}
if _, ok := sentEntries[k]; ok {
continue
for k, e := range entries {
if !e.Expiry.After(now) {
// Implies TTL=0, meaning a "Goodbye Packet".
if _, ok := sentEntries[k]; ok && c.unannouncements {
params.Entries <- e
}
delete(sentEntries, k)
continue
}
if _, ok := sentEntries[k]; ok {
// Already sent, suppress duplicates
continue
}

// If this is an DNS-SD query do not throw PTR away.
// It is expected to have only PTR for enumeration
if params.ServiceRecord.ServiceTypeName() != params.ServiceRecord.ServiceName() {
// Require at least one resolved IP address for ServiceEntry
// TODO: wait some more time as chances are high both will arrive.
if len(e.AddrIPv4) == 0 && len(e.AddrIPv6) == 0 {
continue
}
}
// Submit entry to subscriber and cache it.
// This is also a point to possibly stop probing actively for a
// service entry.
params.Entries <- e
sentEntries[k] = e
if !params.isBrowsing {
params.disableProbing()
// If this is an DNS-SD query do not throw PTR away.
// It is expected to have only PTR for enumeration
if params.ServiceRecord.ServiceTypeName() != params.ServiceRecord.ServiceName() {
// Require at least one resolved IP address for ServiceEntry
// TODO: wait some more time as chances are high both will arrive.
if len(e.AddrIPv4) == 0 && len(e.AddrIPv6) == 0 {
continue
}
}
// Submit entry to subscriber and cache it.
// This is also a point to possibly stop probing actively for a
// service entry.
params.Entries <- e
sentEntries[k] = e
if !params.isBrowsing {
params.disableProbing()
}
}
}
}
Expand Down