overflow_discovery/discovery/host.go

146 lines
3.6 KiB
Go
Raw Normal View History

2017-11-15 12:09:38 +00:00
package discovery
2017-11-16 11:01:42 +00:00
import (
"fmt"
"net"
"time"
2017-11-15 12:09:38 +00:00
2017-11-16 11:01:42 +00:00
"git.loafle.net/commons_go/logging"
2017-11-17 10:46:56 +00:00
"git.loafle.net/commons_go/util/net/cidr"
2017-11-16 11:01:42 +00:00
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
2017-11-15 12:33:46 +00:00
2017-11-16 11:01:42 +00:00
"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
}
2017-11-17 10:46:56 +00:00
cr, err := cidr.NewCIDRRanger(zone.Network)
if nil != err {
errChan <- err
return
}
hostRanges, err := getTargetHostRange(dh, cr)
if nil != err {
errChan <- err
return
}
2017-11-16 11:01:42 +00:00
arpChan := ps.OpenARP()
defer func() {
if nil != ps {
ps.CloseARP(arpChan)
pcap.ReleaseScanner(zone)
}
doneChan <- struct{}{}
}()
go func() {
2017-11-16 11:31:48 +00:00
hosts := make(map[string]*model.Host)
2017-11-16 11:01:42 +00:00
for {
select {
case packet, ok := <-arpChan:
if !ok {
logging.Logger().Debug(fmt.Sprintf("Discovery: arp channel is closed"))
return
}
2017-11-17 10:46:56 +00:00
if h := handlePacketARP(zone, cr, hosts, packet); nil != h {
2017-11-16 11:31:48 +00:00
logging.Logger().Debug(fmt.Sprintf("Discovery: Host[%v] is founded", h))
}
2017-11-16 11:01:42 +00:00
}
}
}()
2017-11-17 10:46:56 +00:00
if err := sendARP(ps, zone, hostRanges); nil != err {
errChan <- err
return
}
2017-11-16 11:31:48 +00:00
time.Sleep(30 * time.Second)
}
2017-11-17 10:46:56 +00:00
func handlePacketARP(zone *model.Zone, cr cidr.CIDRRanger, hosts map[string]*model.Host, packet *layers.ARP) *model.Host {
2017-11-16 11:31:48 +00:00
// logging.Logger().Debug(fmt.Sprintf("Discovery: arp packet %v", packet))
ip := net.IP(packet.SourceProtAddress)
2017-11-17 10:46:56 +00:00
if _, ok := hosts[ip.String()]; ok || !cr.Contains(ip) {
2017-11-16 11:31:48 +00:00
return nil
}
h := &model.Host{}
h.IP = ip.String()
h.Mac = net.HardwareAddr(packet.SourceHwAddress).String()
h.Zone = zone
hosts[ip.String()] = h
return h
2017-11-16 11:01:42 +00:00
}
2017-11-17 10:46:56 +00:00
func sendARP(ps pcap.PCapScanner, zone *model.Zone, hostRanges []net.IP) error {
2017-11-16 11:01:42 +00:00
hwAddr, err := net.ParseMAC(zone.Mac)
if nil != err {
return err
}
ip := net.ParseIP(zone.IP)
2017-11-17 10:46:56 +00:00
if nil == ip {
2017-11-16 11:01:42 +00:00
return fmt.Errorf("Discovery: IP(%s) of zone is not valid", zone.IP)
}
ethPacket := makePacketEthernet(hwAddr)
arpPacket := makePacketARP(hwAddr, ip)
opts := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}
buf := gopacket.NewSerializeBuffer()
2017-11-17 10:46:56 +00:00
for _, targetHost := range hostRanges {
2017-11-16 11:01:42 +00:00
arpPacket.DstProtAddress = []byte(targetHost)
gopacket.SerializeLayers(buf, opts, ethPacket, arpPacket)
2017-11-17 10:46:56 +00:00
if err := ps.WritePacketData(buf.Bytes()); err != nil {
return err
}
2017-11-16 11:01:42 +00:00
time.Sleep(time.Microsecond * 500)
}
return nil
}
2017-11-17 10:46:56 +00:00
func getTargetHostRange(dh *model.DiscoveryHost, cr cidr.CIDRRanger) ([]net.IP, error) {
excludeIPs := make([]net.IP, 0)
2017-11-16 11:01:42 +00:00
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)
}
2017-11-17 10:46:56 +00:00
excludeIPs = append(excludeIPs, eIP)
2017-11-16 11:01:42 +00:00
}
2017-11-17 10:46:56 +00:00
ranges := cr.Range(excludeIPs)
2017-11-16 11:01:42 +00:00
return ranges, nil
}
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},
}
2017-11-15 12:09:38 +00:00
}