From 381a488709ccc2465e7eabf8485542b430c2ce41 Mon Sep 17 00:00:00 2001 From: crusader Date: Thu, 16 Nov 2017 20:31:48 +0900 Subject: [PATCH] ing --- commons/pcap/pcap_scan.go | 1 + discovery/host.go | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/commons/pcap/pcap_scan.go b/commons/pcap/pcap_scan.go index f86e14e..8a6fdd1 100644 --- a/commons/pcap/pcap_scan.go +++ b/commons/pcap/pcap_scan.go @@ -74,6 +74,7 @@ func (ps *pCapScan) start() error { h.Close() return err } + ps.pCapHandle = h ps.arpListenerChans = make([]chan *layers.ARP, 0) ps.tcpListenerChans = make(map[string][]chan *layers.TCP, 0) diff --git a/discovery/host.go b/discovery/host.go index 3ca34ff..d5d2968 100644 --- a/discovery/host.go +++ b/discovery/host.go @@ -32,6 +32,7 @@ func scanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan interfa }() go func() { + hosts := make(map[string]*model.Host) for { select { case packet, ok := <-arpChan: @@ -39,12 +40,31 @@ func scanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan interfa logging.Logger().Debug(fmt.Sprintf("Discovery: arp channel is closed")) return } - logging.Logger().Debug(fmt.Sprintf("Discovery: arp packet %v", packet)) + if h := handlePacketARP(zone, hosts, packet); nil != h { + logging.Logger().Debug(fmt.Sprintf("Discovery: Host[%v] is founded", h)) + } } } }() - time.Sleep(10 * time.Second) + time.Sleep(30 * time.Second) +} + +func handlePacketARP(zone *model.Zone, hosts map[string]*model.Host, packet *layers.ARP) *model.Host { + // logging.Logger().Debug(fmt.Sprintf("Discovery: arp packet %v", packet)) + + ip := net.IP(packet.SourceProtAddress) + if _, ok := hosts[ip.String()]; ok { + 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 } func sendARP(ps pcap.PCapScanner, zone *model.Zone, dh *model.DiscoveryHost) error {