2018-08-29 12:04:23 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-08-30 12:56:32 +00:00
|
|
|
"net"
|
2018-08-31 10:05:49 +00:00
|
|
|
"strconv"
|
2018-08-29 12:04:23 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
omd "git.loafle.net/overflow/model/discovery"
|
2018-08-30 12:56:32 +00:00
|
|
|
ounc "git.loafle.net/overflow/util-go/net/cidr"
|
2018-08-29 12:04:23 +00:00
|
|
|
"git.loafle.net/overflow_scanner/probe/discovery/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DiscoverySession interface {
|
2018-08-30 12:56:32 +00:00
|
|
|
InitWithRequest(request types.DiscoveryRequest) error
|
|
|
|
|
|
|
|
DiscoveryRequest() types.DiscoveryRequest
|
|
|
|
|
2018-08-29 12:04:23 +00:00
|
|
|
Zone() *omd.Zone
|
|
|
|
Host() *omd.Host
|
|
|
|
Port() *omd.Port
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
DiscoveryConfig() *omd.DiscoveryConfig
|
|
|
|
|
2018-08-29 12:04:23 +00:00
|
|
|
DiscoverHost() *omd.DiscoverHost
|
|
|
|
DiscoverPort() *omd.DiscoverPort
|
|
|
|
DiscoverService() *omd.DiscoverService
|
|
|
|
|
2018-08-31 10:05:49 +00:00
|
|
|
TargetHosts() []net.IP
|
|
|
|
|
|
|
|
SetDiscoveryDelegator(chan<- interface{})
|
|
|
|
|
2018-08-29 12:04:23 +00:00
|
|
|
AddHost(host *omd.Host) *omd.Host
|
|
|
|
AddPort(port *omd.Port) *omd.Port
|
|
|
|
AddService(service *omd.Service) *omd.Service
|
2018-08-30 12:56:32 +00:00
|
|
|
|
2018-08-31 10:05:49 +00:00
|
|
|
DiscoveredHost(address string) *omd.Host
|
|
|
|
DiscoveredPort(host *omd.Host, portNumber int) map[string]*omd.Port
|
|
|
|
DiscoveredService(port *omd.Port, name string) map[string]*omd.Service
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ofDiscoverySession struct {
|
|
|
|
discoveryRequest types.DiscoveryRequest
|
|
|
|
|
|
|
|
zone *omd.Zone
|
|
|
|
host *omd.Host
|
|
|
|
port *omd.Port
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
discoveryConfig *omd.DiscoveryConfig
|
|
|
|
|
2018-08-29 12:04:23 +00:00
|
|
|
discoverHost *omd.DiscoverHost
|
|
|
|
discoverPort *omd.DiscoverPort
|
|
|
|
discoverService *omd.DiscoverService
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
targetHosts []net.IP
|
|
|
|
|
|
|
|
discoveryDelegator chan<- interface{}
|
|
|
|
|
2018-08-29 12:04:23 +00:00
|
|
|
hosts map[string]*omd.Host
|
|
|
|
ports map[*omd.Host]map[json.Number]map[string]*omd.Port
|
|
|
|
services map[*omd.Port]map[string]map[string]*omd.Service
|
|
|
|
}
|
|
|
|
|
2018-08-29 18:20:51 +00:00
|
|
|
func (ds *ofDiscoverySession) init(request types.DiscoveryRequest) {
|
|
|
|
ds.discoveryRequest = request
|
2018-08-29 12:04:23 +00:00
|
|
|
ds.zone = nil
|
|
|
|
ds.host = nil
|
|
|
|
ds.port = nil
|
|
|
|
ds.discoverHost = nil
|
|
|
|
ds.discoverPort = nil
|
|
|
|
ds.discoverService = nil
|
|
|
|
|
|
|
|
ds.hosts = make(map[string]*omd.Host)
|
|
|
|
ds.ports = make(map[*omd.Host]map[json.Number]map[string]*omd.Port)
|
|
|
|
ds.services = make(map[*omd.Port]map[string]map[string]*omd.Service)
|
|
|
|
}
|
|
|
|
|
2018-08-29 18:20:51 +00:00
|
|
|
func (ds *ofDiscoverySession) InitWithRequest(request types.DiscoveryRequest) error {
|
|
|
|
ds.init(request)
|
|
|
|
|
|
|
|
params := request.Params()
|
|
|
|
|
|
|
|
switch request.RequestType() {
|
|
|
|
case types.DiscoveryRequestTypeHost:
|
|
|
|
if nil == params || 2 != len(params) {
|
|
|
|
return fmt.Errorf("Parameter is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
zone, ok := params[0].(*omd.Zone)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Zone of parameter is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
discoverHost, ok := params[1].(*omd.DiscoverHost)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("DiscoverHost of parameter is not valid")
|
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
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
|
2018-08-29 18:20:51 +00:00
|
|
|
ds.setZone(zone)
|
|
|
|
ds.setDiscoverHost(discoverHost)
|
|
|
|
case types.DiscoveryRequestTypePort:
|
|
|
|
if nil == params || 2 != len(params) {
|
|
|
|
return fmt.Errorf("Parameter is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
host, ok := params[0].(*omd.Host)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Host of parameter is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
discoverPort, ok := params[1].(*omd.DiscoverPort)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("DiscoverPort of parameter is not valid")
|
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
if nil == discoverPort.DiscoveryConfig {
|
|
|
|
return fmt.Errorf("DiscoveryConfig of parameter is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
ds.discoveryConfig = discoverPort.DiscoveryConfig
|
2018-08-29 18:20:51 +00:00
|
|
|
ds.setHost(host)
|
|
|
|
ds.setDiscoverPort(discoverPort)
|
|
|
|
case types.DiscoveryRequestTypeService:
|
|
|
|
if nil == params || 2 != len(params) {
|
|
|
|
return fmt.Errorf("Parameter is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
port, ok := params[0].(*omd.Port)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Port of parameter is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
discoverService, ok := params[1].(*omd.DiscoverService)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("DiscoverService of parameter is not valid")
|
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
if nil == discoverService.DiscoveryConfig {
|
|
|
|
return fmt.Errorf("DiscoveryConfig of parameter is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
ds.discoveryConfig = discoverService.DiscoveryConfig
|
2018-08-29 18:20:51 +00:00
|
|
|
ds.setPort(port)
|
|
|
|
ds.setDiscoverService(discoverService)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
func (ds *ofDiscoverySession) SetDiscoveryDelegator(discoveryDelegator chan<- interface{}) {
|
|
|
|
ds.discoveryDelegator = discoveryDelegator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *ofDiscoverySession) DiscoveryRequest() types.DiscoveryRequest {
|
|
|
|
return ds.discoveryRequest
|
|
|
|
}
|
|
|
|
|
2018-08-29 12:04:23 +00:00
|
|
|
func (ds *ofDiscoverySession) Zone() *omd.Zone {
|
|
|
|
return ds.zone
|
|
|
|
}
|
|
|
|
func (ds *ofDiscoverySession) Host() *omd.Host {
|
|
|
|
return ds.host
|
|
|
|
}
|
|
|
|
func (ds *ofDiscoverySession) Port() *omd.Port {
|
|
|
|
return ds.port
|
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
func (ds *ofDiscoverySession) DiscoveryConfig() *omd.DiscoveryConfig {
|
|
|
|
return ds.discoveryConfig
|
|
|
|
}
|
|
|
|
|
2018-08-29 12:04:23 +00:00
|
|
|
func (ds *ofDiscoverySession) DiscoverHost() *omd.DiscoverHost {
|
|
|
|
return ds.discoverHost
|
|
|
|
}
|
|
|
|
func (ds *ofDiscoverySession) DiscoverPort() *omd.DiscoverPort {
|
|
|
|
return ds.discoverPort
|
|
|
|
}
|
|
|
|
func (ds *ofDiscoverySession) DiscoverService() *omd.DiscoverService {
|
|
|
|
return ds.discoverService
|
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
func (ds *ofDiscoverySession) TargetHosts() []net.IP {
|
|
|
|
return ds.targetHosts
|
|
|
|
}
|
|
|
|
|
2018-08-29 12:04:23 +00:00
|
|
|
func (ds *ofDiscoverySession) AddHost(host *omd.Host) *omd.Host {
|
2018-08-30 12:56:32 +00:00
|
|
|
h, modified := ds.findHost(host, true)
|
2018-08-29 12:04:23 +00:00
|
|
|
|
|
|
|
if "" == h.Mac && "" != host.Mac {
|
|
|
|
h.Mac = host.Mac
|
2018-08-30 12:56:32 +00:00
|
|
|
modified = true
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
meta, metaModified := ds.appendMeta(h.Meta, host.Meta)
|
|
|
|
if metaModified {
|
|
|
|
h.Meta = meta
|
|
|
|
modified = metaModified
|
|
|
|
}
|
2018-08-29 12:04:23 +00:00
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
if modified && nil != ds.discoveryDelegator {
|
|
|
|
ds.discoveryDelegator <- h
|
|
|
|
}
|
2018-08-29 12:04:23 +00:00
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *ofDiscoverySession) AddPort(port *omd.Port) *omd.Port {
|
2018-08-30 12:56:32 +00:00
|
|
|
p, modified := ds.findPort(port, true)
|
|
|
|
|
|
|
|
meta, metaModified := ds.appendMeta(p.Meta, port.Meta)
|
|
|
|
if metaModified {
|
|
|
|
p.Meta = meta
|
|
|
|
modified = metaModified
|
|
|
|
}
|
2018-08-29 12:04:23 +00:00
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
if modified && nil != ds.discoveryDelegator {
|
|
|
|
ds.discoveryDelegator <- p
|
|
|
|
}
|
2018-08-29 12:04:23 +00:00
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *ofDiscoverySession) AddService(service *omd.Service) *omd.Service {
|
2018-08-30 12:56:32 +00:00
|
|
|
s, modified := ds.findService(service, true)
|
2018-08-29 12:04:23 +00:00
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
meta, metaModified := ds.appendMeta(s.Meta, service.Meta)
|
|
|
|
if metaModified {
|
|
|
|
s.Meta = meta
|
|
|
|
modified = metaModified
|
|
|
|
}
|
|
|
|
|
|
|
|
if modified && nil != ds.discoveryDelegator {
|
|
|
|
ds.discoveryDelegator <- s
|
|
|
|
}
|
2018-08-29 12:04:23 +00:00
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2018-08-31 10:05:49 +00:00
|
|
|
func (ds *ofDiscoverySession) DiscoveredHost(address string) *omd.Host {
|
|
|
|
h, ok := ds.hosts[address]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *ofDiscoverySession) DiscoveredPort(host *omd.Host, portNumber int) map[string]*omd.Port {
|
|
|
|
h, _ := ds.findHost(host, false)
|
|
|
|
if nil == h {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hostPorts, ok := ds.ports[h]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ports, ok := hostPorts[json.Number(strconv.Itoa(portNumber))]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ports
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *ofDiscoverySession) DiscoveredService(port *omd.Port, name string) map[string]*omd.Service {
|
|
|
|
p, _ := ds.findPort(port, false)
|
|
|
|
if nil == p {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
portServices, ok := ds.services[p]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
services, ok := portServices[name]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
func (ds *ofDiscoverySession) findHost(host *omd.Host, add bool) (h *omd.Host, modified bool) {
|
|
|
|
modified = false
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
h, ok = ds.hosts[host.Address]
|
2018-08-29 12:04:23 +00:00
|
|
|
if !ok {
|
|
|
|
if !add {
|
2018-08-30 12:56:32 +00:00
|
|
|
return
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
ds.hosts[host.Address] = host
|
|
|
|
h = host
|
2018-08-30 12:56:32 +00:00
|
|
|
modified = true
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
return
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
func (ds *ofDiscoverySession) findPort(port *omd.Port, add bool) (p *omd.Port, modified bool) {
|
|
|
|
modified = false
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
h, _ := ds.findHost(port.Host, false)
|
2018-08-29 12:04:23 +00:00
|
|
|
if nil == h {
|
2018-08-30 12:56:32 +00:00
|
|
|
return
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hostPorts, ok := ds.ports[h]
|
|
|
|
if !ok {
|
|
|
|
if !add {
|
2018-08-30 12:56:32 +00:00
|
|
|
return
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ds.ports[h] = make(map[json.Number]map[string]*omd.Port)
|
|
|
|
hostPorts = ds.ports[h]
|
|
|
|
}
|
|
|
|
|
|
|
|
ports, ok := hostPorts[port.PortNumber]
|
|
|
|
if !ok {
|
|
|
|
if !add {
|
2018-08-30 12:56:32 +00:00
|
|
|
return
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hostPorts[port.PortNumber] = make(map[string]*omd.Port)
|
|
|
|
ports = hostPorts[port.PortNumber]
|
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
p, ok = ports[port.MetaPortType.Key]
|
2018-08-29 12:04:23 +00:00
|
|
|
if !ok {
|
|
|
|
if !add {
|
2018-08-30 12:56:32 +00:00
|
|
|
return
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ports[port.MetaPortType.Key] = port
|
|
|
|
p = ports[port.MetaPortType.Key]
|
2018-08-30 12:56:32 +00:00
|
|
|
modified = true
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
return
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
func (ds *ofDiscoverySession) findService(service *omd.Service, add bool) (s *omd.Service, modified bool) {
|
|
|
|
modified = false
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
p, _ := ds.findPort(service.Port, false)
|
2018-08-29 12:04:23 +00:00
|
|
|
if nil == p {
|
2018-08-30 12:56:32 +00:00
|
|
|
return
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
portServices, ok := ds.services[p]
|
|
|
|
if !ok {
|
|
|
|
if !add {
|
2018-08-30 12:56:32 +00:00
|
|
|
return
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ds.services[p] = make(map[string]map[string]*omd.Service)
|
|
|
|
portServices = ds.services[p]
|
|
|
|
}
|
|
|
|
|
|
|
|
services, ok := portServices[service.Key]
|
|
|
|
if !ok {
|
|
|
|
if !add {
|
2018-08-30 12:56:32 +00:00
|
|
|
return
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
portServices[service.Key] = make(map[string]*omd.Service)
|
|
|
|
services = portServices[service.Key]
|
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
s, ok = services[service.MetaCryptoType.Key]
|
2018-08-29 12:04:23 +00:00
|
|
|
if !ok {
|
|
|
|
if !add {
|
2018-08-30 12:56:32 +00:00
|
|
|
return
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
services[service.MetaCryptoType.Key] = service
|
|
|
|
s = services[service.MetaCryptoType.Key]
|
2018-08-30 12:56:32 +00:00
|
|
|
modified = true
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
return
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
func (ds *ofDiscoverySession) appendMeta(oriMeta map[string]string, newMeta map[string]string) (resultMap map[string]string, modified bool) {
|
|
|
|
modified = false
|
2018-08-29 12:04:23 +00:00
|
|
|
if nil == newMeta {
|
2018-08-30 12:56:32 +00:00
|
|
|
resultMap = oriMeta
|
|
|
|
return
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
if nil == oriMeta {
|
2018-08-30 12:56:32 +00:00
|
|
|
resultMap = newMeta
|
|
|
|
modified = true
|
|
|
|
return
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LOOP:
|
2018-08-30 12:56:32 +00:00
|
|
|
for k, v := range newMeta {
|
2018-08-29 12:04:23 +00:00
|
|
|
_v, _ok := oriMeta[k]
|
|
|
|
if !_ok {
|
|
|
|
oriMeta[k] = v
|
2018-08-30 12:56:32 +00:00
|
|
|
modified = true
|
2018-08-29 12:04:23 +00:00
|
|
|
continue LOOP
|
|
|
|
}
|
|
|
|
if v == _v {
|
|
|
|
continue LOOP
|
|
|
|
}
|
|
|
|
oriMeta[k] = fmt.Sprintf("%s|||%s", _v, v)
|
2018-08-30 12:56:32 +00:00
|
|
|
modified = true
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
resultMap = oriMeta
|
|
|
|
|
|
|
|
return
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *ofDiscoverySession) setZone(zone *omd.Zone) {
|
|
|
|
if nil == zone {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ds.zone = zone
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *ofDiscoverySession) setHost(host *omd.Host) {
|
|
|
|
if nil == host {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ds.setZone(host.Zone)
|
|
|
|
ds.host = host
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *ofDiscoverySession) setPort(port *omd.Port) {
|
|
|
|
if nil == port {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ds.setHost(port.Host)
|
|
|
|
ds.port = port
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *ofDiscoverySession) setDiscoverHost(discoverHost *omd.DiscoverHost) {
|
|
|
|
if nil == discoverHost {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ds.discoverHost = discoverHost
|
|
|
|
ds.setDiscoverPort(discoverHost.DiscoverPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *ofDiscoverySession) setDiscoverPort(discoverPort *omd.DiscoverPort) {
|
|
|
|
if nil == discoverPort {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ds.discoverPort = discoverPort
|
|
|
|
ds.setDiscoverService(discoverPort.DiscoverService)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *ofDiscoverySession) setDiscoverService(discoverService *omd.DiscoverService) {
|
|
|
|
if nil == discoverService {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ds.discoverService = discoverService
|
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
func (ds *ofDiscoverySession) getTargetHosts(zone *omd.Zone, discoverHost *omd.DiscoverHost) ([]net.IP, error) {
|
|
|
|
cr, err := ounc.NewCIDRRanger(zone.Network)
|
|
|
|
if nil != err {
|
|
|
|
return nil, 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
includeIPs := make([]net.IP, 0)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
includeIPs = append(includeIPs, iIP)
|
|
|
|
}
|
|
|
|
|
|
|
|
excludeIPs := make([]net.IP, 0)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
excludeIPs = append(excludeIPs, eIP)
|
|
|
|
}
|
|
|
|
|
|
|
|
ranges, err := cr.Ranges(firstIP, lastIP, includeIPs, excludeIPs)
|
|
|
|
if nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ranges, nil
|
|
|
|
}
|
|
|
|
|
2018-08-29 12:04:23 +00:00
|
|
|
var discoverySessionPool sync.Pool
|
|
|
|
|
|
|
|
func RetainDiscoverySession() *ofDiscoverySession {
|
|
|
|
v := discoverySessionPool.Get()
|
|
|
|
var ds *ofDiscoverySession
|
|
|
|
if v == nil {
|
|
|
|
ds = &ofDiscoverySession{}
|
|
|
|
} else {
|
|
|
|
ds = v.(*ofDiscoverySession)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ds
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReleaseDiscoverySession(ds *ofDiscoverySession) {
|
|
|
|
ds.discoveryRequest = nil
|
|
|
|
ds.zone = nil
|
|
|
|
ds.host = nil
|
|
|
|
ds.port = nil
|
|
|
|
ds.discoverHost = nil
|
|
|
|
ds.discoverPort = nil
|
|
|
|
ds.discoverService = nil
|
|
|
|
|
|
|
|
ds.hosts = nil
|
|
|
|
ds.ports = nil
|
|
|
|
ds.services = nil
|
|
|
|
|
|
|
|
discoverySessionPool.Put(ds)
|
|
|
|
}
|