This commit is contained in:
crusader 2018-09-13 20:45:17 +09:00
parent 4868d241cc
commit 337f493251

View File

@ -72,7 +72,9 @@ type ofDiscoverySession struct {
discoverPort *omd.DiscoverPort discoverPort *omd.DiscoverPort
discoverService *omd.DiscoverService discoverService *omd.DiscoverService
targetHosts []net.IP targetHosts []net.IP
targetHostAddresses map[string]bool
targetPortNumbers map[int]bool
discoveryDelegator chan<- interface{} discoveryDelegator chan<- interface{}
@ -95,6 +97,9 @@ func (ds *ofDiscoverySession) init(request types.DiscoveryRequest) {
ds.discoverService = nil ds.discoverService = nil
ds.pCapScanner = nil ds.pCapScanner = nil
ds.targetHosts = nil
ds.targetHostAddresses = nil
ds.targetPortNumbers = nil
ds.hosts = make(map[string]*omd.Host) ds.hosts = make(map[string]*omd.Host)
ds.includeMachosts = make(map[string]*omd.Host) ds.includeMachosts = make(map[string]*omd.Host)
ds.ports = make(map[*omd.Host]map[json.Number]map[string]*omd.Port) ds.ports = make(map[*omd.Host]map[json.Number]map[string]*omd.Port)
@ -118,25 +123,21 @@ func (ds *ofDiscoverySession) InitWithRequest(request types.DiscoveryRequest) er
if !ok { if !ok {
return fmt.Errorf("Zone of parameter is not valid") return fmt.Errorf("Zone of parameter is not valid")
} }
ds.setZone(zone)
discoverHost, ok := params[1].(*omd.DiscoverHost) discoverHost, ok := params[1].(*omd.DiscoverHost)
if !ok { if !ok {
return fmt.Errorf("DiscoverHost of parameter is not valid") return fmt.Errorf("DiscoverHost of parameter is not valid")
} }
if err := ds.setDiscoverHost(discoverHost); nil != err {
return err
}
if nil == discoverHost.DiscoveryConfig { if nil == discoverHost.DiscoveryConfig {
return fmt.Errorf("DiscoveryConfig of parameter is not valid") return fmt.Errorf("DiscoveryConfig of parameter is not valid")
} }
if ts, err := ds.getTargetHosts(zone, discoverHost); nil != err {
return err
} else {
ds.targetHosts = ts
}
ds.discoveryConfig = discoverHost.DiscoveryConfig ds.discoveryConfig = discoverHost.DiscoveryConfig
ds.setZone(zone)
ds.setDiscoverHost(discoverHost)
case types.DiscoveryRequestTypePort: case types.DiscoveryRequestTypePort:
if nil == params || 2 != len(params) { if nil == params || 2 != len(params) {
return fmt.Errorf("Parameter is not valid") return fmt.Errorf("Parameter is not valid")
@ -146,19 +147,21 @@ func (ds *ofDiscoverySession) InitWithRequest(request types.DiscoveryRequest) er
if !ok { if !ok {
return fmt.Errorf("Host of parameter is not valid") return fmt.Errorf("Host of parameter is not valid")
} }
ds.setHost(host)
discoverPort, ok := params[1].(*omd.DiscoverPort) discoverPort, ok := params[1].(*omd.DiscoverPort)
if !ok { if !ok {
return fmt.Errorf("DiscoverPort of parameter is not valid") return fmt.Errorf("DiscoverPort of parameter is not valid")
} }
if err := ds.setDiscoverPort(discoverPort); nil != err {
return err
}
if nil == discoverPort.DiscoveryConfig { if nil == discoverPort.DiscoveryConfig {
return fmt.Errorf("DiscoveryConfig of parameter is not valid") return fmt.Errorf("DiscoveryConfig of parameter is not valid")
} }
ds.discoveryConfig = discoverPort.DiscoveryConfig ds.discoveryConfig = discoverPort.DiscoveryConfig
ds.setHost(host)
ds.setDiscoverPort(discoverPort)
case types.DiscoveryRequestTypeService: case types.DiscoveryRequestTypeService:
if nil == params || 2 != len(params) { if nil == params || 2 != len(params) {
return fmt.Errorf("Parameter is not valid") return fmt.Errorf("Parameter is not valid")
@ -168,19 +171,20 @@ func (ds *ofDiscoverySession) InitWithRequest(request types.DiscoveryRequest) er
if !ok { if !ok {
return fmt.Errorf("Port of parameter is not valid") return fmt.Errorf("Port of parameter is not valid")
} }
ds.setPort(port)
discoverService, ok := params[1].(*omd.DiscoverService) discoverService, ok := params[1].(*omd.DiscoverService)
if !ok { if !ok {
return fmt.Errorf("DiscoverService of parameter is not valid") return fmt.Errorf("DiscoverService of parameter is not valid")
} }
if err := ds.setDiscoverService(discoverService); nil != err {
return err
}
if nil == discoverService.DiscoveryConfig { if nil == discoverService.DiscoveryConfig {
return fmt.Errorf("DiscoveryConfig of parameter is not valid") return fmt.Errorf("DiscoveryConfig of parameter is not valid")
} }
ds.discoveryConfig = discoverService.DiscoveryConfig ds.discoveryConfig = discoverService.DiscoveryConfig
ds.setPort(port)
ds.setDiscoverService(discoverService)
} }
_pCapScanner := pcap.NewPCapScanner(ds.zone) _pCapScanner := pcap.NewPCapScanner(ds.zone)
@ -236,16 +240,19 @@ func (ds *ofDiscoverySession) TargetHosts() []net.IP {
} }
func (ds *ofDiscoverySession) IsTargetHost(host *omd.Host) bool { func (ds *ofDiscoverySession) IsTargetHost(host *omd.Host) bool {
for _, targetHost := range ds.targetHosts { if nil == ds.targetHostAddresses {
if targetHost.String() == host.Address { return false
return true
}
} }
if _, ok := ds.targetHostAddresses[host.Address]; ok {
return true
}
return false return false
} }
func (ds *ofDiscoverySession) IsTargetPort(port *omd.Port) bool { func (ds *ofDiscoverySession) IsTargetPort(port *omd.Port) bool {
if nil == ds.discoverPort { if nil == ds.targetPortNumbers || 0 == len(ds.targetPortNumbers) {
return false return false
} }
@ -258,19 +265,11 @@ func (ds *ofDiscoverySession) IsTargetPort(port *omd.Port) bool {
return false return false
} }
if nil != ds.discoverPort.ExcludePorts { if _, ok := ds.targetPortNumbers[portNumber]; ok {
for _, exPortNumber := range ds.discoverPort.ExcludePorts { return true
if portNumber == exPortNumber {
return false
}
}
} }
if portNumber < ds.discoverPort.FirstScanRange || portNumber > ds.discoverPort.LastScanRange { return false
return false
}
return true
} }
func (ds *ofDiscoverySession) IsTargetService(service *omd.Service) bool { func (ds *ofDiscoverySession) IsTargetService(service *omd.Service) bool {
@ -278,6 +277,10 @@ func (ds *ofDiscoverySession) IsTargetService(service *omd.Service) bool {
return false return false
} }
if !ds.IsTargetPort(service.Port) {
return false
}
return true return true
} }
@ -820,50 +823,61 @@ func (ds *ofDiscoverySession) setPort(port *omd.Port) {
ds.port = port ds.port = port
} }
func (ds *ofDiscoverySession) setDiscoverHost(discoverHost *omd.DiscoverHost) { func (ds *ofDiscoverySession) setDiscoverHost(discoverHost *omd.DiscoverHost) error {
if nil == discoverHost { if nil == discoverHost {
return return nil
}
if err := ds.setTargetHosts(ds.zone, discoverHost); nil != err {
return err
} }
ds.discoverHost = discoverHost ds.discoverHost = discoverHost
ds.setDiscoverPort(discoverHost.DiscoverPort)
return ds.setDiscoverPort(discoverHost.DiscoverPort)
} }
func (ds *ofDiscoverySession) setDiscoverPort(discoverPort *omd.DiscoverPort) { func (ds *ofDiscoverySession) setDiscoverPort(discoverPort *omd.DiscoverPort) error {
if nil == discoverPort { if nil == discoverPort {
return return nil
}
if err := ds.setTargetPortNumbers(discoverPort); nil != err {
return err
} }
ds.discoverPort = discoverPort ds.discoverPort = discoverPort
ds.setDiscoverService(discoverPort.DiscoverService) return ds.setDiscoverService(discoverPort.DiscoverService)
} }
func (ds *ofDiscoverySession) setDiscoverService(discoverService *omd.DiscoverService) { func (ds *ofDiscoverySession) setDiscoverService(discoverService *omd.DiscoverService) error {
if nil == discoverService { if nil == discoverService {
return return nil
} }
ds.discoverService = discoverService ds.discoverService = discoverService
return nil
} }
func (ds *ofDiscoverySession) getTargetHosts(zone *omd.Zone, discoverHost *omd.DiscoverHost) ([]net.IP, error) { func (ds *ofDiscoverySession) setTargetHosts(zone *omd.Zone, discoverHost *omd.DiscoverHost) error {
cr, err := ounc.NewCIDRRanger(zone.Network) cr, err := ounc.NewCIDRRanger(zone.Network)
if nil != err { if nil != err {
return nil, err return err
} }
var firstIP net.IP var firstIP net.IP
if "" != discoverHost.FirstScanRange { if "" != discoverHost.FirstScanRange {
firstIP = net.ParseIP(discoverHost.FirstScanRange) firstIP = net.ParseIP(discoverHost.FirstScanRange)
if nil == firstIP { if nil == firstIP {
return nil, fmt.Errorf("IP(%v) of FirstScanRange host is not valid", firstIP) return fmt.Errorf("IP(%v) of FirstScanRange host is not valid", firstIP)
} }
} }
var lastIP net.IP var lastIP net.IP
if "" != discoverHost.LastScanRange { if "" != discoverHost.LastScanRange {
lastIP = net.ParseIP(discoverHost.LastScanRange) lastIP = net.ParseIP(discoverHost.LastScanRange)
if nil == lastIP { if nil == lastIP {
return nil, fmt.Errorf("IP(%v) of LastScanRange host is not valid", lastIP) return fmt.Errorf("IP(%v) of LastScanRange host is not valid", lastIP)
} }
} }
@ -871,7 +885,7 @@ func (ds *ofDiscoverySession) getTargetHosts(zone *omd.Zone, discoverHost *omd.D
for _, iHost := range discoverHost.IncludeHosts { for _, iHost := range discoverHost.IncludeHosts {
iIP := net.ParseIP(iHost) iIP := net.ParseIP(iHost)
if nil == iIP { if nil == iIP {
return nil, fmt.Errorf("IP(%v) of include host is not valid", iHost) return fmt.Errorf("IP(%v) of include host is not valid", iHost)
} }
includeIPs = append(includeIPs, iIP) includeIPs = append(includeIPs, iIP)
} }
@ -880,17 +894,46 @@ func (ds *ofDiscoverySession) getTargetHosts(zone *omd.Zone, discoverHost *omd.D
for _, eHost := range discoverHost.ExcludeHosts { for _, eHost := range discoverHost.ExcludeHosts {
eIP := net.ParseIP(eHost) eIP := net.ParseIP(eHost)
if nil == eIP { if nil == eIP {
return nil, fmt.Errorf("IP(%v) of exclude host is not valid", eHost) return fmt.Errorf("IP(%v) of exclude host is not valid", eHost)
} }
excludeIPs = append(excludeIPs, eIP) excludeIPs = append(excludeIPs, eIP)
} }
ranges, err := cr.Ranges(firstIP, lastIP, includeIPs, excludeIPs) ranges, err := cr.Ranges(firstIP, lastIP, includeIPs, excludeIPs)
if nil != err { if nil != err {
return nil, err return err
} }
return ranges, nil ds.targetHosts = ranges
ds.targetHostAddresses = make(map[string]bool, len(ranges))
for _, ip := range ranges {
ds.targetHostAddresses[ip.String()] = true
}
return nil
}
func (ds *ofDiscoverySession) setTargetPortNumbers(discoverPort *omd.DiscoverPort) error {
if nil == discoverPort {
return nil
}
ds.targetPortNumbers = make(map[int]bool, 0)
LOOP:
for portNumber := discoverPort.FirstScanRange; portNumber < discoverPort.LastScanRange; portNumber++ {
if nil != discoverPort.ExcludePorts {
for _, exPortNumber := range discoverPort.ExcludePorts {
if portNumber == exPortNumber {
continue LOOP
}
}
}
ds.targetPortNumbers[portNumber] = true
}
return nil
} }
var discoverySessionPool sync.Pool var discoverySessionPool sync.Pool
@ -927,6 +970,8 @@ func ReleaseDiscoverySession(ds *ofDiscoverySession) {
ds.discoverService = nil ds.discoverService = nil
ds.targetHosts = nil ds.targetHosts = nil
ds.targetHostAddresses = nil
ds.targetPortNumbers = nil
ds.discoveryDelegator = nil ds.discoveryDelegator = nil