Exemplo n.º 1
0
        /// <summary>Check array of ports for correctness as range of ports.</summary>
        /// <param name="ports">Array to check.</param>
        /// <returns>Correctess of array.</returns>
        public static bool CheckArrayOnRangeCorrectness(Port[] ports)
        {
            if (ports.Length == 0)
            {
                return(false);
            }

            RangeOfPorts rangeOfPorts = new RangeOfPorts();

            if (ports.Length == 2 || ports.Length == 4)
            {
                rangeOfPorts.FromTcp = ports[0].Value;
                rangeOfPorts.ToTcp   = ports[1].Value;
            }
            else
            {
                return(false);
            }

            if (ports.Length == 4)
            {
                rangeOfPorts.FromUdp = ports[2].Value;
                rangeOfPorts.ToUdp   = ports[3].Value;
            }

            return(rangeOfPorts.IsValid);
        }
Exemplo n.º 2
0
        /// <summary>Scan all of ports in host.</summary>
        /// <exception cref="ArgumentNullException">Throws when <see cref="Host"/> or <see cref="Ports"/> was equals null.</exception>
        /// <exception cref="RangeOfPortsException">Throws when <see cref="Ports"/> parameter not looks like a range.</exception>
        public async Task ScanAllAsync()
        {
            if (Host == null)
            {
                throw new ArgumentNullException(nameof(Host), "Host value was equals null.");
            }
            if (Ports == null)
            {
                throw new ArgumentNullException(nameof(Ports), "Ports value was equals null.");
            }
            if (!RangeOfPorts.CheckArrayOnRangeCorrectness(Ports))
            {
                throw new RangeOfPortsException("Ports array are not correct.");
            }

            _scannedPortsCount = 0;

            try
            {
                await _checker.HostIsValidAsync(Host);

                List <Port> results = new List <Port>();

                if (Ports.Length == 2 || Ports.Length == 4)
                {
                    Port[] scannedPorts = await ScanRange(Ports[0], Ports[1]);

                    results.AddRange(scannedPorts);
                }
                if (Ports.Length == 4)
                {
                    Port[] scannedPorts = await ScanRange(Ports[2], Ports[3]);

                    results.AddRange(scannedPorts);
                }

                Port[] sortedResults = results.OrderBy(result => result.Value).ToArray();

                OnScanEnding?.Invoke(new ScanResult(Host, DateTime.Now, sortedResults));
            }
            catch (HostNotValidException)
            {
                OnScanEnding?.Invoke(new ScanResult(Host, DateTime.Now, new Port[] { }, false));
            }
            catch (Exception ex)
            {
                new Error(ex).HandleError();
            }

            _scannedPortsCount = 0;
            _aborted           = false;
        }
Exemplo n.º 3
0
 public RangeOfPortsException(string message, RangeOfPorts range) : this(message)
 {
     Range = range;
 }