overflow_discovery/net/zone.go

63 lines
1.1 KiB
Go
Raw Normal View History

2017-10-20 09:09:07 +00:00
package net
import "net"
import "git.loafle.net/overflow/overflow_discovery/net/model"
2017-10-20 09:20:13 +00:00
import "strings"
2017-10-20 09:09:07 +00:00
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 {
2017-10-20 09:20:13 +00:00
2017-10-20 09:09:07 +00:00
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{
2017-10-20 09:20:13 +00:00
Network: ipnet.String(),
Iface: i.Name,
Mac: i.HardwareAddr.String(),
IP: strings.Split(addr.String(), "/")[0],
2017-10-20 09:09:07 +00:00
}
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
}