diff --git a/commons/pcap/packet.go b/commons/pcap/packet.go index 376733f..343c0e9 100644 --- a/commons/pcap/packet.go +++ b/commons/pcap/packet.go @@ -55,13 +55,11 @@ func handlePacketARP(ps *pCapScan, packet gopacket.Packet) { ps.arpListenerChanMtx.RLock() defer ps.arpListenerChanMtx.RUnlock() - if ps.arpListenerChans != nil { - arpLayer := packet.Layer(layers.LayerTypeARP) - arp := arpLayer.(*layers.ARP) + arpLayer := packet.Layer(layers.LayerTypeARP) + arp := arpLayer.(*layers.ARP) - for _, ch := range ps.arpListenerChans { - ch <- arp - } + for _, ch := range ps.arpListenerChans { + ch <- arp } } diff --git a/discovery/host.go b/discovery/host.go index abb1fc0..cdad9c5 100644 --- a/discovery/host.go +++ b/discovery/host.go @@ -62,10 +62,14 @@ func scanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan interfa return } - time.Sleep(30 * time.Second) + time.Sleep(10 * time.Second) } func handlePacketARP(zone *model.Zone, cr cidr.CIDRRanger, hosts map[string]*model.Host, packet *layers.ARP) *model.Host { + if packet.Operation != layers.ARPReply { + return nil + } + // logging.Logger().Debug(fmt.Sprintf("Discovery: arp packet %v", packet)) ip := net.IP(packet.SourceProtAddress) @@ -93,17 +97,18 @@ func sendARP(ps pcap.PCapScanner, zone *model.Zone, hostRanges []net.IP) error { } ethPacket := makePacketEthernet(hwAddr) - arpPacket := makePacketARP(hwAddr, ip) + arpPacket := makePacketARP(hwAddr, ip.To4()) opts := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true} buf := gopacket.NewSerializeBuffer() for _, targetHost := range hostRanges { arpPacket.DstProtAddress = []byte(targetHost) - gopacket.SerializeLayers(buf, opts, ethPacket, arpPacket) + // log.Printf("ARP:%v", arpPacket) + gopacket.SerializeLayers(buf, opts, ðPacket, &arpPacket) if err := ps.WritePacketData(buf.Bytes()); err != nil { return err } - time.Sleep(time.Microsecond * 500) + time.Sleep(time.Microsecond * 100) } return nil } @@ -123,16 +128,16 @@ func getTargetHostRange(dh *model.DiscoveryHost, cr cidr.CIDRRanger) ([]net.IP, return ranges, nil } -func makePacketEthernet(hw net.HardwareAddr) *layers.Ethernet { - return &layers.Ethernet{ +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{ +func makePacketARP(hw net.HardwareAddr, ip net.IP) layers.ARP { + return layers.ARP{ AddrType: layers.LinkTypeEthernet, Protocol: layers.EthernetTypeIPv4, HwAddressSize: 6,