overflow_discovery/net/zone.go
crusader cb7a0cea64 ing
2017-10-20 18:09:07 +09:00

62 lines
1.1 KiB
Go

package net
import "net"
import "git.loafle.net/overflow/overflow_discovery/net/model"
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 {
// log.Printf("addr: %s", addr.String())
if _, ipnet, err = net.ParseCIDR(addr.String()); nil != err {
logChan <- err
continue
}
if ipnet.IP.IsLoopback() || checkSameZone(zones, ipnet) {
continue
}
// log.Printf("zone: %s", ipnet.String())
zones = append(zones, ipnet)
dz := &model.DiscoveryZone{
Iface: i.Name,
Mac: i.HardwareAddr.String(),
IP: ipnet.IP.String(),
}
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
}