mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-09-29 13:51:51 +02:00
internal/clientinfo: use last seen IP for NDP discovery
This commit is contained in:
1 parent
3e62bd3dbd
commit
cc6ae290f8
3 files changed
+26
-12
No files matched your search
@@ -69,6 +69,21 @@ func (nd *ndpDiscover) List() []string {
|
|||||||
return ips
|
return ips
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// saveInfo saves ip and mac info to mapping table.
|
||||||
|
// Last seen ip address will override the old one,
|
||||||
|
func (nd *ndpDiscover) saveInfo(ip, mac string) {
|
||||||
|
// Store ip => map mapping,
|
||||||
|
nd.mac.Store(ip, mac)
|
||||||
|
// If there is old ip => mac mapping, delete it.
|
||||||
|
old, ok := nd.ip.Load(mac)
|
||||||
|
if ok {
|
||||||
|
oldIP := old.(string)
|
||||||
|
nd.mac.Delete(oldIP)
|
||||||
|
}
|
||||||
|
// Store mac => ip mapping.
|
||||||
|
nd.ip.Store(mac, ip)
|
||||||
|
}
|
||||||
|
|
||||||
// listen listens on ipv6 link local for Neighbor Solicitation message
|
// listen listens on ipv6 link local for Neighbor Solicitation message
|
||||||
// to update new neighbors information to ndp table.
|
// to update new neighbors information to ndp table.
|
||||||
func (nd *ndpDiscover) listen(ctx context.Context) {
|
func (nd *ndpDiscover) listen(ctx context.Context) {
|
||||||
@@ -111,8 +126,7 @@ func (nd *ndpDiscover) listen(ctx context.Context) {
|
|||||||
for _, opt := range am.Options {
|
for _, opt := range am.Options {
|
||||||
if lla, ok := opt.(*ndp.LinkLayerAddress); ok {
|
if lla, ok := opt.(*ndp.LinkLayerAddress); ok {
|
||||||
mac := lla.Addr.String()
|
mac := lla.Addr.String()
|
||||||
nd.mac.Store(fromIP, mac)
|
nd.saveInfo(fromIP, mac)
|
||||||
nd.ip.Store(mac, fromIP)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -127,8 +141,7 @@ func (nd *ndpDiscover) scanWindows(r io.Reader) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if mac := parseMAC(fields[1]); mac != "" {
|
if mac := parseMAC(fields[1]); mac != "" {
|
||||||
nd.mac.Store(fields[0], mac)
|
nd.saveInfo(fields[0], mac)
|
||||||
nd.ip.Store(mac, fields[0])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -147,8 +160,7 @@ func (nd *ndpDiscover) scanUnix(r io.Reader) {
|
|||||||
if idx := strings.IndexByte(ip, '%'); idx != -1 {
|
if idx := strings.IndexByte(ip, '%'); idx != -1 {
|
||||||
ip = ip[:idx]
|
ip = ip[:idx]
|
||||||
}
|
}
|
||||||
nd.mac.Store(ip, mac)
|
nd.saveInfo(ip, mac)
|
||||||
nd.ip.Store(mac, ip)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,8 +17,7 @@ func (nd *ndpDiscover) scan() {
|
|||||||
for _, n := range neighs {
|
for _, n := range neighs {
|
||||||
ip := n.IP.String()
|
ip := n.IP.String()
|
||||||
mac := n.HardwareAddr.String()
|
mac := n.HardwareAddr.String()
|
||||||
nd.mac.Store(ip, mac)
|
nd.saveInfo(ip, mac)
|
||||||
nd.ip.Store(mac, ip)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -45,12 +45,15 @@ ff02::c 33-33-00-00-00-0c Permanent
|
|||||||
nd.scanWindows(r)
|
nd.scanWindows(r)
|
||||||
|
|
||||||
count := 0
|
count := 0
|
||||||
|
expectedCount := 5
|
||||||
nd.mac.Range(func(key, value any) bool {
|
nd.mac.Range(func(key, value any) bool {
|
||||||
count++
|
count++
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
if count != 6 {
|
// There are 2 entries for 60-57-47-21-dd-00 in the table, but (*ndpDiscover).saveInfo
|
||||||
t.Errorf("unexpected count, want 6, got: %d", count)
|
// only saves the last one, that's why the expected count number is 5.
|
||||||
|
if count != expectedCount {
|
||||||
|
t.Errorf("unexpected count, want %d, got: %d", expectedCount, count)
|
||||||
}
|
}
|
||||||
|
|
||||||
count = 0
|
count = 0
|
||||||
@@ -58,7 +61,7 @@ ff02::c 33-33-00-00-00-0c Permanent
|
|||||||
count++
|
count++
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
if count != 5 {
|
if count != expectedCount {
|
||||||
t.Errorf("unexpected count, want 5, got: %d", count)
|
t.Errorf("unexpected count, want %d, got: %d", expectedCount, count)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user