container_discovery/internal/discoverer/ipv4/host.go

223 lines
5.0 KiB
Go
Raw Permalink Normal View History

2018-04-19 11:36:56 +00:00
package ipv4
import (
"fmt"
"net"
2018-04-27 15:33:22 +00:00
"sync/atomic"
2018-04-19 11:36:56 +00:00
"time"
"git.loafle.net/commons/logging-go"
"git.loafle.net/commons/util-go/net/cidr"
2018-05-11 02:44:47 +00:00
occu "git.loafle.net/overflow/commons-go/core/util"
2018-04-26 09:00:24 +00:00
ocmd "git.loafle.net/overflow/commons-go/model/discovery"
2018-06-13 10:38:34 +00:00
ocmm "git.loafle.net/overflow/commons-go/model/meta"
2018-04-19 11:36:56 +00:00
"git.loafle.net/overflow/container_discovery/internal/pcap"
2018-05-11 02:44:47 +00:00
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
2018-04-19 11:36:56 +00:00
)
2018-04-27 16:20:01 +00:00
func ScanHost(zone *ocmd.Zone, dh *ocmd.DiscoverHost, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) {
2018-04-19 11:36:56 +00:00
ps, err := pcap.RetainScanner(zone)
if nil != err {
errChan <- fmt.Errorf("Discovery: Cannot retain pcap instance %v", err)
return
}
defer func() {
pcap.ReleaseScanner(zone)
}()
cr, err := cidr.NewCIDRRanger(zone.Network)
if nil != err {
errChan <- err
return
}
hostRanges, err := getTargetHostRange(dh, cr)
if nil != err {
errChan <- err
return
}
arpChan := ps.OpenARP()
defer func() {
ps.CloseARP(arpChan)
}()
2018-04-27 15:33:22 +00:00
timerStopped := make(chan struct{})
2018-04-19 11:36:56 +00:00
go func() {
2018-04-26 09:00:24 +00:00
hosts := make(map[string]*ocmd.Host)
2018-04-27 15:33:22 +00:00
var delay atomic.Value
delay.Store(false)
ticker := time.NewTicker(time.Millisecond * 500)
2018-04-19 11:36:56 +00:00
for {
select {
case packet, ok := <-arpChan:
if !ok {
2018-04-19 15:25:07 +00:00
logging.Logger().Debugf("arp channel is closed")
2018-04-19 11:36:56 +00:00
return
}
2018-04-27 15:33:22 +00:00
delay.Store(true)
2018-04-19 11:36:56 +00:00
if h := handlePacketARP(zone, hostRanges, hosts, packet); nil != h {
resultChan <- h
}
2018-04-27 15:33:22 +00:00
case <-ticker.C:
if false == delay.Load().(bool) {
ticker.Stop()
timerStopped <- struct{}{}
return
}
delay.Store(false)
2018-04-19 11:36:56 +00:00
case <-stopChan:
return
}
}
}()
if err := sendARP(ps, zone, hostRanges, stopChan); nil != err {
errChan <- err
return
}
select {
case <-stopChan:
return
2018-04-27 15:33:22 +00:00
case <-timerStopped:
2018-04-19 11:36:56 +00:00
return
}
}
2018-04-26 09:00:24 +00:00
func sendARP(ps pcap.PCapScanner, zone *ocmd.Zone, hostRanges []net.IP, stopChan chan struct{}) error {
2018-04-19 11:36:56 +00:00
hwAddr, err := net.ParseMAC(zone.Mac)
if nil != err {
return err
}
2018-06-13 10:20:14 +00:00
ip := net.ParseIP(zone.Address)
2018-04-19 11:36:56 +00:00
if nil == ip {
2018-06-13 10:20:14 +00:00
return fmt.Errorf("Discovery: IP(%s) of zone is not valid", zone.Address)
2018-04-19 11:36:56 +00:00
}
ethPacket := makePacketEthernet(hwAddr)
arpPacket := makePacketARP(hwAddr, ip.To4())
opts := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}
buf := gopacket.NewSerializeBuffer()
for _, targetHost := range hostRanges {
arpPacket.DstProtAddress = []byte(targetHost)
// log.Printf("ARP:%v", arpPacket)
gopacket.SerializeLayers(buf, opts, &ethPacket, &arpPacket)
if err := ps.WritePacketData(buf.Bytes()); err != nil {
return err
}
timer := time.NewTimer(time.Microsecond * 100)
select {
case <-stopChan:
return nil
case <-timer.C:
}
}
return nil
}
2018-04-26 09:00:24 +00:00
func handlePacketARP(zone *ocmd.Zone, hostRanges []net.IP, hosts map[string]*ocmd.Host, packet *layers.ARP) *ocmd.Host {
2018-04-19 11:36:56 +00:00
if packet.Operation != layers.ARPReply {
return nil
}
ip := net.IP(packet.SourceProtAddress)
if _, ok := hosts[ip.String()]; ok {
return nil
}
inRange := false
for _, h := range hostRanges {
if h.Equal(ip) {
inRange = true
break
}
}
if !inRange {
return nil
}
2018-05-11 02:44:47 +00:00
h := &ocmd.Host{
2018-06-13 10:38:34 +00:00
MetaIPType: ocmm.ToMetaIPType(ocmm.MetaIPTypeEnumV4),
2018-06-13 10:20:14 +00:00
Address: ip.String(),
2018-05-11 02:44:47 +00:00
Mac: net.HardwareAddr(packet.SourceHwAddress).String(),
Zone: zone,
DiscoveredDate: occu.NowPtr(),
}
2018-04-19 11:36:56 +00:00
hosts[ip.String()] = h
return h
}
2018-04-27 16:20:01 +00:00
func getTargetHostRange(dh *ocmd.DiscoverHost, cr cidr.CIDRRanger) ([]net.IP, error) {
2018-04-19 11:36:56 +00:00
var firstIP net.IP
2018-06-13 10:20:14 +00:00
if "" != dh.FirstScanRange {
firstIP = net.ParseIP(dh.FirstScanRange)
2018-04-19 11:36:56 +00:00
if nil == firstIP {
return nil, fmt.Errorf("Discovery: IP(%v) of FirstScanRange host is not valid", firstIP)
}
}
var lastIP net.IP
2018-06-13 10:20:14 +00:00
if "" != dh.LastScanRange {
lastIP = net.ParseIP(dh.LastScanRange)
2018-04-19 11:36:56 +00:00
if nil == lastIP {
return nil, fmt.Errorf("Discovery: IP(%v) of LastScanRange host is not valid", lastIP)
}
}
includeIPs := make([]net.IP, 0)
2018-06-13 10:20:14 +00:00
for _, iHost := range dh.IncludeHosts {
2018-04-19 11:36:56 +00:00
iIP := net.ParseIP(iHost)
if nil == iIP {
return nil, fmt.Errorf("Discovery: IP(%v) of include host is not valid", iHost)
}
includeIPs = append(includeIPs, iIP)
}
excludeIPs := make([]net.IP, 0)
2018-06-13 10:20:14 +00:00
for _, eHost := range dh.ExcludeHosts {
2018-04-19 11:36:56 +00:00
eIP := net.ParseIP(eHost)
if nil == eIP {
return nil, fmt.Errorf("Discovery: IP(%v) of exclude host is not valid", eHost)
}
excludeIPs = append(excludeIPs, eIP)
}
ranges, err := cr.Ranges(firstIP, lastIP, includeIPs, excludeIPs)
if nil != err {
return nil, err
}
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},
}
}