overflow_discovery/discovery/host.go
crusader 3579d26e52 ing
2017-11-16 20:01:42 +09:00

167 lines
4.1 KiB
Go

package discovery
import (
"bytes"
"fmt"
"net"
"time"
"git.loafle.net/commons_go/logging"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"git.loafle.net/overflow/overflow_discovery/api/module/discovery/model"
"git.loafle.net/overflow/overflow_discovery/commons/pcap"
)
func scanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}) {
ps, err := pcap.RetainScanner(zone)
if nil != err {
errChan <- fmt.Errorf("Discovery: Cannot retain pcap instance %v", err)
// doneChan <- struct{}{}
return
}
arpChan := ps.OpenARP()
defer func() {
if nil != ps {
ps.CloseARP(arpChan)
pcap.ReleaseScanner(zone)
}
doneChan <- struct{}{}
}()
go func() {
for {
select {
case packet, ok := <-arpChan:
if !ok {
logging.Logger().Debug(fmt.Sprintf("Discovery: arp channel is closed"))
return
}
logging.Logger().Debug(fmt.Sprintf("Discovery: arp packet %v", packet))
}
}
}()
time.Sleep(10 * time.Second)
}
func sendARP(ps pcap.PCapScanner, zone *model.Zone, dh *model.DiscoveryHost) error {
hwAddr, err := net.ParseMAC(zone.Mac)
if nil != err {
return err
}
ip := net.ParseIP(zone.IP)
if nil != ip {
return fmt.Errorf("Discovery: IP(%s) of zone is not valid", zone.IP)
}
ranges, err := getTargetHostRange(zone, dh)
if nil != err {
return err
}
ethPacket := makePacketEthernet(hwAddr)
arpPacket := makePacketARP(hwAddr, ip)
opts := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}
buf := gopacket.NewSerializeBuffer()
for _, targetHost := range ranges {
arpPacket.DstProtAddress = []byte(targetHost)
gopacket.SerializeLayers(buf, opts, ethPacket, arpPacket)
// if err := ps.WritePacketData(buf.Bytes()); err != nil {
// return err
// }
time.Sleep(time.Microsecond * 500)
}
return nil
}
func getTargetHostRange(zone *model.Zone, dh *model.DiscoveryHost) ([]net.IP, error) {
nIP, nIPNet, err := net.ParseCIDR(zone.Network)
if nil != err {
return nil, fmt.Errorf("Discovery: Network(%s) of zone is not valid", zone.Network)
}
firstIP := net.ParseIP(dh.FirstScanRange)
lastIP := net.ParseIP(dh.LastScanRange)
if nil == firstIP || nil == lastIP {
return nil, fmt.Errorf("Discovery: IP Range(First:%s, Last:%s) is not valid", dh.FirstScanRange, dh.LastScanRange)
}
firstIP16 := firstIP.To16()
lastIP16 := lastIP.To16()
if nil == firstIP16 || nil == lastIP16 {
return nil, fmt.Errorf("Discovery: IP Range(First:%s, Last:%s) is not valid", dh.FirstScanRange, dh.LastScanRange)
}
excludeIP16s := make([]net.IP, 0)
for _, eHost := range dh.ExcludeHosts {
eIP := net.ParseIP(eHost)
if nil == eIP {
return nil, fmt.Errorf("Discovery: IP(%v) of exclude host is not valid", eHost)
}
eIP16 := eIP.To16()
if nil == eIP16 {
return nil, fmt.Errorf("Discovery: IP(%v) of exclude host is not valid", eHost)
}
excludeIP16s = append(excludeIP16s, eIP16)
}
ranges := make([]net.IP, 0)
ip := nIP
Loop:
for ip := ip.Mask(nIPNet.Mask); nIPNet.Contains(ip); inc(ip) {
ip16 := ip.To16()
if nil == ip16 {
return nil, fmt.Errorf("Discovery: IP(%v) converting is failed", ip)
}
if 0 > bytes.Compare(ip16, firstIP16) || 0 < bytes.Compare(ip16, lastIP16) {
continue
}
for _, eIP16 := range excludeIP16s {
if 0 == bytes.Compare(eIP16, ip16) {
continue Loop
}
}
ranges = append(ranges, ip)
}
return ranges, nil
}
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
func makePacketEthernet(hw net.HardwareAddr) *layers.Ethernet {
return &layers.Ethernet{
SrcMAC: hw,
DstMAC: net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
EthernetType: layers.EthernetTypeARP,
}
}
func makePacketARP(hw net.HardwareAddr, ip net.IP) *layers.ARP {
return &layers.ARP{
AddrType: layers.LinkTypeEthernet,
Protocol: layers.EthernetTypeIPv4,
HwAddressSize: 6,
ProtAddressSize: 4,
Operation: layers.ARPRequest,
SourceHwAddress: []byte(hw),
SourceProtAddress: []byte(ip),
DstHwAddress: []byte{0, 0, 0, 0, 0, 0},
}
}