mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-07-04 01:07:49 +02:00
internal/clientinfo: use all possible source IP for listing clients
This commit is contained in:
committed by
Cuong Manh Le
parent
46509be8a0
commit
e5389ffecb
@@ -16,8 +16,6 @@ type IpResolver interface {
|
|||||||
fmt.Stringer
|
fmt.Stringer
|
||||||
// LookupIP returns ip of the device with given mac.
|
// LookupIP returns ip of the device with given mac.
|
||||||
LookupIP(mac string) string
|
LookupIP(mac string) string
|
||||||
// List returns list of ip known by the resolver.
|
|
||||||
List() []string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MacResolver is the interface for retrieving Mac from IP.
|
// MacResolver is the interface for retrieving Mac from IP.
|
||||||
@@ -50,6 +48,12 @@ type refresher interface {
|
|||||||
refresh() error
|
refresh() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ipLister interface {
|
||||||
|
fmt.Stringer
|
||||||
|
// List returns list of ip known by the resolver.
|
||||||
|
List() []string
|
||||||
|
}
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
IP netip.Addr
|
IP netip.Addr
|
||||||
Mac string
|
Mac string
|
||||||
@@ -255,7 +259,8 @@ func (t *Table) ListClients() []*Client {
|
|||||||
_ = r.refresh()
|
_ = r.refresh()
|
||||||
}
|
}
|
||||||
ipMap := make(map[string]*Client)
|
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() {
|
for _, ip := range ir.List() {
|
||||||
c, ok := ipMap[ip]
|
c, ok := ipMap[ip]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
@@ -47,6 +47,15 @@ func (m *mdns) String() string {
|
|||||||
return "mdns"
|
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 {
|
func (m *mdns) init(quitCh chan struct{}) error {
|
||||||
ifaces, err := multicastInterfaces()
|
ifaces, err := multicastInterfaces()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -123,8 +132,11 @@ func (m *mdns) readLoop(conn *net.UDPConn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var ip, name string
|
var ip, name string
|
||||||
for _, answer := range msg.Answer {
|
rrs := make([]dns.RR, 0, len(msg.Answer)+len(msg.Extra))
|
||||||
switch ar := answer.(type) {
|
rrs = append(rrs, msg.Answer...)
|
||||||
|
rrs = append(rrs, msg.Extra...)
|
||||||
|
for _, rr := range rrs {
|
||||||
|
switch ar := rr.(type) {
|
||||||
case *dns.A:
|
case *dns.A:
|
||||||
ip, name = ar.A.String(), ar.Hdr.Name
|
ip, name = ar.A.String(), ar.Hdr.Name
|
||||||
case *dns.AAAA:
|
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 {
|
func (m *mdns) probe(conns []*net.UDPConn, remoteAddr net.Addr) error {
|
||||||
msg := new(dns.Msg)
|
msg := new(dns.Msg)
|
||||||
msg.Question = make([]dns.Question, len(services))
|
msg.Question = make([]dns.Question, len(services))
|
||||||
|
msg.Compress = true
|
||||||
for i, service := range services {
|
for i, service := range services {
|
||||||
msg.Question[i] = dns.Question{
|
msg.Question[i] = dns.Question{
|
||||||
Name: dns.CanonicalName(service),
|
Name: dns.CanonicalName(service),
|
||||||
|
|||||||
@@ -40,6 +40,15 @@ func (p *ptrDiscover) String() string {
|
|||||||
return "ptr"
|
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 {
|
func (p *ptrDiscover) lookupHostnameFromCache(ip string) string {
|
||||||
if val, ok := p.hostname.Load(ip); ok {
|
if val, ok := p.hostname.Load(ip); ok {
|
||||||
return val.(string)
|
return val.(string)
|
||||||
|
|||||||
Reference in New Issue
Block a user