internal/clientinfo: use all possible source IP for listing clients

This commit is contained in:
Cuong Manh Le
2023-08-04 20:03:15 +00:00
committed by Cuong Manh Le
parent 46509be8a0
commit e5389ffecb
3 changed files with 32 additions and 5 deletions

View File

@@ -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 {

View File

@@ -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),

View File

@@ -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)