public void TestParsing3d() { PortRange.TryParse("0-1,1-2,2-3,,,,", out var range1); Assert.Single(range1); Assert.Equal(new PortRange(0, 3), range1.First()); Assert.Equal("0-3", PortRange.Format(range1)); }
public void TestParsing2b() { PortRange.TryParse("0-100;44", out var range1); var range2 = new PortRange(0, 100); Assert.Equal(range2, range1.First()); Assert.Single(range1); Assert.Equal("0-100", PortRange.Format(range1)); }
public void TestParsing3b() { PortRange.TryParse("0-1,3-4,6-9,,,,", out var range1); Assert.Equal(new PortRange(0, 1), range1.First()); Assert.Equal(new PortRange(3, 4), range1.Skip(1).First()); Assert.Equal(new PortRange(6, 9), range1.Skip(2).First()); Assert.Equal(3, range1.Count()); Assert.Equal("0-1;3-4;6-9", PortRange.Format(range1)); }
public void TestParsing3() { PortRange.TryParse("0,3,6,,,,", out var range1); Assert.Equal(new PortRange(0), range1.First()); Assert.Equal(new PortRange(3), range1.Skip(1).First()); Assert.Equal(new PortRange(6), range1.Skip(2).First()); Assert.Equal(3, range1.Count()); Assert.Equal("0;3;6", PortRange.Format(range1)); }
public void TestParsing2() { PortRange.TryParse("0-100;144", out var range1); var range2 = new PortRange(0, 100); var range3 = new PortRange(144); Assert.Equal(range2, range1.First()); Assert.Equal(range3, range1.Skip(1).First()); Assert.Equal(2, range1.Count()); Assert.Equal("0-100;144", PortRange.Format(range1)); }
/// <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; }
/// <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; }