63 lines
1.1 KiB
Go
63 lines
1.1 KiB
Go
package net
|
|
|
|
import "net"
|
|
import "git.loafle.net/overflow/overflow_discovery/net/model"
|
|
import "strings"
|
|
|
|
func ScanZone(endChan chan<- bool, zoneChan chan<- *model.DiscoveryZone, logChan chan<- error) {
|
|
var err error
|
|
var ifaces []net.Interface
|
|
var addrs []net.Addr
|
|
var ipnet *net.IPNet
|
|
var zones []*net.IPNet
|
|
|
|
if ifaces, err = net.Interfaces(); nil != err {
|
|
logChan <- err
|
|
return
|
|
}
|
|
|
|
zones = make([]*net.IPNet, 0)
|
|
|
|
for _, i := range ifaces {
|
|
|
|
if addrs, err = i.Addrs(); nil != err {
|
|
logChan <- err
|
|
continue
|
|
}
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if _, ipnet, err = net.ParseCIDR(addr.String()); nil != err {
|
|
logChan <- err
|
|
continue
|
|
}
|
|
if ipnet.IP.IsLoopback() || checkSameZone(zones, ipnet) {
|
|
continue
|
|
}
|
|
|
|
zones = append(zones, ipnet)
|
|
|
|
dz := &model.DiscoveryZone{
|
|
Network: ipnet.String(),
|
|
Iface: i.Name,
|
|
Mac: i.HardwareAddr.String(),
|
|
IP: strings.Split(addr.String(), "/")[0],
|
|
}
|
|
|
|
zoneChan <- dz
|
|
}
|
|
}
|
|
|
|
endChan <- true
|
|
|
|
}
|
|
|
|
func checkSameZone(zones []*net.IPNet, ipnet *net.IPNet) bool {
|
|
for _, i := range zones {
|
|
if i.Contains(ipnet.IP) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|