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

View File

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