diff --git a/commons/pcap/pcap.go b/commons/pcap/pcap.go index 4e7ee3a..839d9ce 100644 --- a/commons/pcap/pcap.go +++ b/commons/pcap/pcap.go @@ -2,6 +2,7 @@ package pcap import ( "sync" + "time" "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model" ) @@ -36,10 +37,13 @@ func ReleaseScanner(zone *model.Zone) { defer mtx.Unlock() if ps, ok := instances[zone.Network]; ok { - if ps.release() { - ps.stop() - delete(instances, zone.Network) - } + go func() { + time.Sleep(2 * time.Second) + if ps.release() { + ps.stop() + delete(instances, zone.Network) + } + }() } } diff --git a/commons/pcap/pcap_scan.go b/commons/pcap/pcap_scan.go index 8a6fdd1..0bfc3e2 100644 --- a/commons/pcap/pcap_scan.go +++ b/commons/pcap/pcap_scan.go @@ -197,6 +197,7 @@ func (ps *pCapScan) release() bool { if 0 > ps.refCount { ps.refCount = 0 } + return 0 == ps.refCount } @@ -243,6 +244,7 @@ func handleReceive(ps *pCapScan) { handlePacket(ps, packet) case <-ps.stopChan: ps.destroy() + return } } } diff --git a/discovery/ipv4/host.go b/discovery/ipv4/host.go index 565e91f..642099b 100644 --- a/discovery/ipv4/host.go +++ b/discovery/ipv4/host.go @@ -15,17 +15,26 @@ import ( ) func ScanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}) { + defer func() { + doneChan <- struct{}{} + }() + ps, err := pcap.RetainScanner(zone) if nil != err { errChan <- fmt.Errorf("Discovery: Cannot retain pcap instance %v", err) - // doneChan <- struct{}{} 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 @@ -34,11 +43,7 @@ func ScanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan interfa arpChan := ps.OpenARP() defer func() { - if nil != ps { - ps.CloseARP(arpChan) - pcap.ReleaseScanner(zone) - } - doneChan <- struct{}{} + ps.CloseARP(arpChan) }() go func() {