176 lines
3.8 KiB
Go
176 lines
3.8 KiB
Go
package arp
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
omd "git.loafle.net/overflow/model/discovery"
|
|
omm "git.loafle.net/overflow/model/meta"
|
|
omu "git.loafle.net/overflow/model/util"
|
|
oub "git.loafle.net/overflow/util-go/benchmark"
|
|
"git.loafle.net/overflow_scanner/probe/discovery/session"
|
|
"git.loafle.net/overflow_scanner/probe/internal/pcap"
|
|
"github.com/google/gopacket"
|
|
"github.com/google/gopacket/layers"
|
|
)
|
|
|
|
func Scan(discoverySession session.DiscoverySession) error {
|
|
oub.Elapsed("mDNS")
|
|
|
|
targetHosts := discoverySession.TargetHosts()
|
|
if nil == targetHosts || 0 == len(targetHosts) {
|
|
return nil
|
|
}
|
|
|
|
zone := discoverySession.Zone()
|
|
|
|
ps, err := pcap.RetainScanner(zone)
|
|
if nil != err {
|
|
return fmt.Errorf("Cannot retain pcap instance %v", err)
|
|
}
|
|
|
|
defer func() {
|
|
pcap.ReleaseScanner(zone)
|
|
}()
|
|
|
|
arpChan := ps.OpenARP()
|
|
defer func() {
|
|
ps.CloseARP(arpChan)
|
|
}()
|
|
|
|
timerStopped := make(chan struct{})
|
|
go func() {
|
|
hosts := make(map[string]*omd.Host)
|
|
|
|
var delay atomic.Value
|
|
delay.Store(false)
|
|
ticker := time.NewTicker(time.Millisecond * 500)
|
|
|
|
for {
|
|
select {
|
|
case packet, ok := <-arpChan:
|
|
if !ok {
|
|
// logging.Logger().Debugf("arp channel is closed")
|
|
return
|
|
}
|
|
delay.Store(true)
|
|
if h := handlePacketARP(zone, targetHosts, hosts, packet); nil != h {
|
|
if h != nil {
|
|
log.Println(h)
|
|
}
|
|
|
|
// resultChan <- h
|
|
}
|
|
case <-ticker.C:
|
|
if false == delay.Load().(bool) {
|
|
ticker.Stop()
|
|
timerStopped <- struct{}{}
|
|
return
|
|
}
|
|
delay.Store(false)
|
|
}
|
|
}
|
|
}()
|
|
|
|
if err := sendARP(ps, zone, targetHosts); nil != err {
|
|
log.Printf("sendARP %v", err)
|
|
return nil
|
|
}
|
|
|
|
select {
|
|
case <-timerStopped:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func sendARP(ps pcap.PCapScanner, zone *omd.Zone, targetHosts []net.IP) error {
|
|
hwAddr, err := net.ParseMAC(zone.Mac)
|
|
if nil != err {
|
|
return err
|
|
}
|
|
ip := net.ParseIP(zone.Address)
|
|
if nil == ip {
|
|
return fmt.Errorf("Discovery: IP(%s) of zone is not valid", zone.Address)
|
|
}
|
|
|
|
ethPacket := makePacketEthernet(hwAddr)
|
|
arpPacket := makePacketARP(hwAddr, ip.To4())
|
|
opts := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}
|
|
buf := gopacket.NewSerializeBuffer()
|
|
|
|
for _, targetHost := range targetHosts {
|
|
arpPacket.DstProtAddress = []byte(targetHost)
|
|
// log.Printf("ARP:%v", arpPacket)
|
|
gopacket.SerializeLayers(buf, opts, ðPacket, &arpPacket)
|
|
if err := ps.WritePacketData(buf.Bytes()); err != nil {
|
|
return err
|
|
}
|
|
|
|
timer := time.NewTimer(time.Microsecond * 100)
|
|
|
|
select {
|
|
case <-timer.C:
|
|
}
|
|
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func handlePacketARP(zone *omd.Zone, targetHosts []net.IP, hosts map[string]*omd.Host, packet *layers.ARP) *omd.Host {
|
|
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 targetHosts {
|
|
if h.Equal(ip) {
|
|
inRange = true
|
|
break
|
|
}
|
|
}
|
|
if !inRange {
|
|
return nil
|
|
}
|
|
|
|
h := &omd.Host{
|
|
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
|
|
Address: ip.String(),
|
|
Mac: net.HardwareAddr(packet.SourceHwAddress).String(),
|
|
Zone: zone,
|
|
DiscoveredDate: omu.NowPtr(),
|
|
}
|
|
|
|
hosts[ip.String()] = h
|
|
|
|
return h
|
|
}
|
|
|
|
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},
|
|
}
|
|
}
|