diff --git a/internal/clientinfo/client_info.go b/internal/clientinfo/client_info.go index f648ac9..9235ca9 100644 --- a/internal/clientinfo/client_info.go +++ b/internal/clientinfo/client_info.go @@ -16,8 +16,6 @@ type IpResolver interface { fmt.Stringer // LookupIP returns ip of the device with given mac. LookupIP(mac string) string - // List returns list of ip known by the resolver. - List() []string } // MacResolver is the interface for retrieving Mac from IP. @@ -50,6 +48,12 @@ type refresher interface { refresh() error } +type ipLister interface { + fmt.Stringer + // List returns list of ip known by the resolver. + List() []string +} + type Client struct { IP netip.Addr Mac string @@ -255,7 +259,8 @@ func (t *Table) ListClients() []*Client { _ = r.refresh() } ipMap := make(map[string]*Client) - for _, ir := range t.ipResolvers { + il := []ipLister{t.dhcp, t.arp, t.ptr, t.mdns} + for _, ir := range il { for _, ip := range ir.List() { c, ok := ipMap[ip] if !ok { diff --git a/internal/clientinfo/mdns.go b/internal/clientinfo/mdns.go index 1e99fe6..5aed579 100644 --- a/internal/clientinfo/mdns.go +++ b/internal/clientinfo/mdns.go @@ -47,6 +47,15 @@ func (m *mdns) String() string { return "mdns" } +func (m *mdns) List() []string { + var ips []string + m.name.Range(func(key, value any) bool { + ips = append(ips, key.(string)) + return true + }) + return ips +} + func (m *mdns) init(quitCh chan struct{}) error { ifaces, err := multicastInterfaces() if err != nil { @@ -123,8 +132,11 @@ func (m *mdns) readLoop(conn *net.UDPConn) { } var ip, name string - for _, answer := range msg.Answer { - switch ar := answer.(type) { + rrs := make([]dns.RR, 0, len(msg.Answer)+len(msg.Extra)) + rrs = append(rrs, msg.Answer...) + rrs = append(rrs, msg.Extra...) + for _, rr := range rrs { + switch ar := rr.(type) { case *dns.A: ip, name = ar.A.String(), ar.Hdr.Name case *dns.AAAA: @@ -151,6 +163,7 @@ func (m *mdns) readLoop(conn *net.UDPConn) { func (m *mdns) probe(conns []*net.UDPConn, remoteAddr net.Addr) error { msg := new(dns.Msg) msg.Question = make([]dns.Question, len(services)) + msg.Compress = true for i, service := range services { msg.Question[i] = dns.Question{ Name: dns.CanonicalName(service), diff --git a/internal/clientinfo/ptr_lookup.go b/internal/clientinfo/ptr_lookup.go index ba76b12..9c02fa1 100644 --- a/internal/clientinfo/ptr_lookup.go +++ b/internal/clientinfo/ptr_lookup.go @@ -40,6 +40,15 @@ func (p *ptrDiscover) String() string { return "ptr" } +func (p *ptrDiscover) List() []string { + var ips []string + p.hostname.Range(func(key, value any) bool { + ips = append(ips, key.(string)) + return true + }) + return ips +} + func (p *ptrDiscover) lookupHostnameFromCache(ip string) string { if val, ok := p.hostname.Load(ip); ok { return val.(string)