예제 #1
0
        /// <summary>
        /// Create request wrapper
        /// </summary>
        /// <param name="request"></param>
        /// <param name="networkClass"></param>
        /// <param name="isScan"></param>
        public DiscoveryRequest(DiscoveryRequestModel request,
                                NetworkClass networkClass = NetworkClass.Wired, bool isScan = false)
        {
            Request      = request?.Clone() ?? throw new ArgumentNullException(nameof(request));
            _cts         = new CancellationTokenSource();
            NetworkClass = networkClass;
            IsScan       = isScan;

            if (!string.IsNullOrEmpty(request.Configuration?.AddressRangesToScan))
            {
                if (AddressRange.TryParse(request.Configuration?.AddressRangesToScan,
                                          out var addresses))
                {
                    AddressRanges = addresses;
                }
            }

            if (AddressRanges == null)
            {
                if (request.Discovery == DiscoveryMode.Fast)
                {
                    var interfaces = NetworkInformationEx.GetAllNetInterfaces(NetworkClass.Wired);
                    AddressRanges = interfaces.Select(t => new AddressRange(t, false, 24));
                    AddressRanges = AddressRanges.Concat(interfaces
                                                         .Where(t => t.Gateway != null &&
                                                                !t.Gateway.Equals(System.Net.IPAddress.Any) &&
                                                                !t.Gateway.Equals(System.Net.IPAddress.None))
                                                         .Select(i => new AddressRange(i.Gateway, 32)));
                }
            }

            if (!string.IsNullOrEmpty(request.Configuration?.PortRangesToScan))
            {
                if (PortRange.TryParse(request.Configuration?.PortRangesToScan,
                                       out var ports))
                {
                    PortRanges = ports;
                }
            }

            if (PortRanges == null)
            {
                switch (request.Discovery)
                {
                case DiscoveryMode.Local:
                    PortRanges = PortRange.All;
                    break;

                case DiscoveryMode.Fast:
                    PortRanges = PortRange.WellKnown;
                    break;

                case DiscoveryMode.Scan:
                    PortRanges = PortRange.Unassigned;
                    break;

                default:
                    PortRanges = PortRange.OpcUa;
                    break;
                }
            }

            TotalAddresses = AddressRanges?.Sum(r => r.Count) ?? 0;
            TotalPorts     = PortRanges?.Sum(r => r.Count) ?? 0;
        }
        /// <summary>
        /// Create request wrapper
        /// </summary>
        /// <param name="request"></param>
        /// <param name="networkClass"></param>
        /// <param name="isScan"></param>
        public DiscoveryRequest(DiscoveryRequestModel request,
                                NetworkClass networkClass = NetworkClass.Wired, bool isScan = false)
        {
            Request      = request?.Clone() ?? throw new ArgumentNullException(nameof(request));
            _cts         = new CancellationTokenSource();
            NetworkClass = networkClass;
            IsScan       = isScan;

            if (Request.Configuration == null)
            {
                Request.Configuration = new DiscoveryConfigModel();
            }

            if (Request.Discovery == null ||
                Request.Discovery == DiscoveryMode.Off)
            {
                // Report empty configuration if off, but keep the
                // discovery urls details from the original request
                Request.Configuration = new DiscoveryConfigModel()
                {
                    ActivationFilter = Request.Configuration.ActivationFilter?.Clone(),
                    DiscoveryUrls    = Request.Configuration.DiscoveryUrls?.ToList(),
                    Locales          = Request.Configuration.Locales?.ToList()
                };
                Request.Discovery = DiscoveryMode.Off;
                return;
            }

            // Parse whatever provided

            if (!string.IsNullOrEmpty(Request.Configuration.PortRangesToScan))
            {
                if (PortRange.TryParse(Request.Configuration.PortRangesToScan,
                                       out var ports))
                {
                    PortRanges = ports;
                    if (Request.Discovery == null)
                    {
                        Request.Discovery = DiscoveryMode.Fast;
                    }
                }
            }

            if (!string.IsNullOrEmpty(Request.Configuration.AddressRangesToScan))
            {
                if (AddressRange.TryParse(Request.Configuration.AddressRangesToScan,
                                          out var addresses))
                {
                    AddressRanges = addresses;
                    if (Request.Discovery == null)
                    {
                        Request.Discovery = DiscoveryMode.Fast;
                    }
                }
            }

            // Set default ranges

            if (AddressRanges == null)
            {
                IEnumerable <NetInterface> interfaces;
                switch (Request.Discovery)
                {
                case DiscoveryMode.Local:
                    interfaces    = NetworkInformationEx.GetAllNetInterfaces(NetworkClass);
                    AddressRanges = AddLocalHost(interfaces
                                                 .Select(t => new AddressRange(t, true)))
                                    .Distinct();
                    break;

                case DiscoveryMode.Fast:
                    interfaces    = NetworkInformationEx.GetAllNetInterfaces(NetworkClass.Wired);
                    AddressRanges = AddLocalHost(interfaces
                                                 .Select(t => new AddressRange(t, false, 24))
                                                 .Concat(interfaces
                                                         .Where(t => t.Gateway != null &&
                                                                !t.Gateway.Equals(IPAddress.Any) &&
                                                                !t.Gateway.Equals(IPAddress.None))
                                                         .Select(i => new AddressRange(i.Gateway, 32)))
                                                 .Distinct());
                    break;

                case DiscoveryMode.Network:
                case DiscoveryMode.Scan:
                    interfaces    = NetworkInformationEx.GetAllNetInterfaces(NetworkClass);
                    AddressRanges = AddLocalHost(interfaces
                                                 .Select(t => new AddressRange(t, false))
                                                 .Concat(interfaces
                                                         .Where(t => t.Gateway != null &&
                                                                !t.Gateway.Equals(IPAddress.Any) &&
                                                                !t.Gateway.Equals(IPAddress.None))
                                                         .Select(i => new AddressRange(i.Gateway, 32)))
                                                 .Distinct());
                    break;

                case DiscoveryMode.Off:
                default:
                    AddressRanges = Enumerable.Empty <AddressRange>();
                    break;
                }
            }

            if (PortRanges == null)
            {
                switch (Request.Discovery)
                {
                case DiscoveryMode.Local:
                    PortRanges = PortRange.All;
                    break;

                case DiscoveryMode.Fast:
                    PortRanges = PortRange.WellKnown;
                    break;

                case DiscoveryMode.Scan:
                    PortRanges = PortRange.Unassigned;
                    break;

                case DiscoveryMode.Network:
                    PortRanges = PortRange.OpcUa;
                    break;

                case DiscoveryMode.Off:
                default:
                    PortRanges = Enumerable.Empty <PortRange>();
                    break;
                }
            }

            // Update reported configuration with used settings

            if (AddressRanges != null && AddressRanges.Any())
            {
                Request.Configuration.AddressRangesToScan = AddressRange.Format(AddressRanges);
                TotalAddresses = AddressRanges?.Sum(r => r.Count) ?? 0;
            }

            if (PortRanges != null && PortRanges.Any())
            {
                Request.Configuration.PortRangesToScan = PortRange.Format(PortRanges);
                TotalPorts = PortRanges?.Sum(r => r.Count) ?? 0;
            }

            Request.Configuration.IdleTimeBetweenScans ??= kDefaultIdleTime;
            Request.Configuration.PortProbeTimeout ??= kDefaultPortProbeTimeout;
            Request.Configuration.NetworkProbeTimeout ??= kDefaultNetworkProbeTimeout;
        }
예제 #3
0
        /// <summary>
        /// Create request wrapper
        /// </summary>
        /// <param name="request"></param>
        /// <param name="networkClass"></param>
        /// <param name="isScan"></param>
        public DiscoveryRequest(DiscoveryRequestModel request,
                                NetworkClass networkClass = NetworkClass.Wired, bool isScan = false)
        {
            Request      = request?.Clone() ?? throw new ArgumentNullException(nameof(request));
            _cts         = new CancellationTokenSource();
            NetworkClass = networkClass;
            IsScan       = isScan;

            if (request == null)
            {
                request = new DiscoveryRequestModel {
                    Discovery = DiscoveryMode.Off
                };
            }
            if (request.Configuration == null)
            {
                request.Configuration = new DiscoveryConfigModel();
            }

            if (!string.IsNullOrEmpty(request.Configuration.AddressRangesToScan))
            {
                if (AddressRange.TryParse(request.Configuration.AddressRangesToScan,
                                          out var addresses))
                {
                    AddressRanges = addresses;
                }
            }

            if (AddressRanges == null)
            {
                switch (request.Discovery)
                {
                case DiscoveryMode.Local:
                    AddressRanges = NetworkInformationEx.GetAllNetInterfaces(NetworkClass)
                                    .Select(t => new AddressRange(t, true)).Distinct();
                    break;

                case DiscoveryMode.Fast:
                    var interfaces = NetworkInformationEx.GetAllNetInterfaces(NetworkClass.Wired);
                    AddressRanges = interfaces.Select(t => new AddressRange(t, false, 24));
                    AddressRanges = AddressRanges.Concat(interfaces
                                                         .Where(t => t.Gateway != null &&
                                                                !t.Gateway.Equals(System.Net.IPAddress.Any) &&
                                                                !t.Gateway.Equals(System.Net.IPAddress.None))
                                                         .Select(i => new AddressRange(i.Gateway, 32)));
                    break;

                case DiscoveryMode.Off:
                    AddressRanges = Enumerable.Empty <AddressRange>();
                    break;

                case DiscoveryMode.Scan:
                    AddressRanges = NetworkInformationEx.GetAllNetInterfaces(NetworkClass)
                                    .Select(t => new AddressRange(t, false)).Distinct();
                    break;

                default:
                    AddressRanges = Enumerable.Empty <AddressRange>();
                    break;
                }
            }

            request.Configuration.AddressRangesToScan = AddressRange.Format(AddressRanges);

            if (!string.IsNullOrEmpty(request.Configuration.PortRangesToScan))
            {
                if (PortRange.TryParse(request.Configuration.PortRangesToScan,
                                       out var ports))
                {
                    PortRanges = ports;
                }
            }

            if (PortRanges == null)
            {
                switch (request.Discovery)
                {
                case DiscoveryMode.Local:
                    PortRanges = PortRange.All;
                    break;

                case DiscoveryMode.Fast:
                    PortRanges = PortRange.WellKnown;
                    break;

                case DiscoveryMode.Scan:
                    PortRanges = PortRange.Unassigned;
                    break;

                case DiscoveryMode.Off:
                    PortRanges = Enumerable.Empty <PortRange>();
                    break;

                default:
                    PortRanges = PortRange.OpcUa;
                    break;
                }
            }

            request.Configuration.PortRangesToScan = PortRange.Format(PortRanges);
            request.Configuration.IdleTimeBetweenScans ??= kDefaultIdleTime;
            request.Configuration.PortProbeTimeout ??= kDefaultPortProbeTimeout;
            request.Configuration.NetworkProbeTimeout ??= kDefaultNetworkProbeTimeout;

            TotalAddresses = AddressRanges?.Sum(r => r.Count) ?? 0;
            TotalPorts     = PortRanges?.Sum(r => r.Count) ?? 0;
        }