This commit is contained in:
crusader 2017-11-18 19:49:58 +09:00
parent be4832f7a9
commit d7f3ea3268
3 changed files with 21 additions and 10 deletions

View File

@ -2,6 +2,7 @@ package pcap
import ( import (
"sync" "sync"
"time"
"git.loafle.net/overflow/overflow_discovery/api/module/discovery/model" "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model"
) )
@ -36,10 +37,13 @@ func ReleaseScanner(zone *model.Zone) {
defer mtx.Unlock() defer mtx.Unlock()
if ps, ok := instances[zone.Network]; ok { if ps, ok := instances[zone.Network]; ok {
go func() {
time.Sleep(2 * time.Second)
if ps.release() { if ps.release() {
ps.stop() ps.stop()
delete(instances, zone.Network) delete(instances, zone.Network)
} }
}()
} }
} }

View File

@ -197,6 +197,7 @@ func (ps *pCapScan) release() bool {
if 0 > ps.refCount { if 0 > ps.refCount {
ps.refCount = 0 ps.refCount = 0
} }
return 0 == ps.refCount return 0 == ps.refCount
} }
@ -243,6 +244,7 @@ func handleReceive(ps *pCapScan) {
handlePacket(ps, packet) handlePacket(ps, packet)
case <-ps.stopChan: case <-ps.stopChan:
ps.destroy() ps.destroy()
return
} }
} }
} }

View File

@ -15,17 +15,26 @@ import (
) )
func ScanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}) { 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) ps, err := pcap.RetainScanner(zone)
if nil != err { if nil != err {
errChan <- fmt.Errorf("Discovery: Cannot retain pcap instance %v", err) errChan <- fmt.Errorf("Discovery: Cannot retain pcap instance %v", err)
// doneChan <- struct{}{}
return return
} }
defer func() {
pcap.ReleaseScanner(zone)
}()
cr, err := cidr.NewCIDRRanger(zone.Network) cr, err := cidr.NewCIDRRanger(zone.Network)
if nil != err { if nil != err {
errChan <- err errChan <- err
return return
} }
hostRanges, err := getTargetHostRange(dh, cr) hostRanges, err := getTargetHostRange(dh, cr)
if nil != err { if nil != err {
errChan <- err errChan <- err
@ -34,11 +43,7 @@ func ScanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan interfa
arpChan := ps.OpenARP() arpChan := ps.OpenARP()
defer func() { defer func() {
if nil != ps {
ps.CloseARP(arpChan) ps.CloseARP(arpChan)
pcap.ReleaseScanner(zone)
}
doneChan <- struct{}{}
}() }()
go func() { go func() {