Ejemplo n.º 1
0
        private IcmpResult Ping(IcmpAction action, IcmpService icmpService)
        {
            try
            {
                var icmpResult = icmpService.Ping(action.Timeout, action.MaxTTL, action.WeightPacket);
                if (icmpResult.Duration.Equals(TimeSpan.MaxValue))
                {
                    icmpResult.ErrorMessage = "Connection timed out";
                    icmpResult.Success      = false;
                }

                // Adding some sleep to prevent icmp attacks on the host.
                if (1000 - icmpResult.Duration.TotalMilliseconds > 0)
                {
                    Thread.Sleep(action.UpdatePeriod - (int)icmpResult.Duration.TotalMilliseconds);
                }
                icmpResult.Host = action.Host;
                return(icmpResult);
            }
            catch (Exception e)
            {
                return(new IcmpResult
                {
                    Host = action.Host,
                    Weight = action.WeightPacket,
                    Success = false,
                    ErrorMessage = e.Message,
                });
            }
        }
        public IEnumerable <IcmpResult> Handle(IcmpAction icmpAction)
        {
            var  icmpService   = new IcmpService(icmpAction.Host);
            bool isTargetFound = false;
            bool isTimeout     = false;
            int  startTTL      = 1;
            int  hopNumber     = 1;
            int  nbrErr        = 0;

            while (!isTargetFound & !isTimeout)
            {
                IcmpResult icmpResult = new IcmpResult();
                try
                {
                    startTTL            += 1; // With each attempt allow a higher TTL.
                    icmpResult           = icmpService.Ping(icmpAction.Timeout, startTTL);
                    icmpResult.HopNumber = hopNumber;
                    nbrErr = 0; // New hop was reached, reset error count to 0;
                    try
                    {
                        icmpResult.Host = Dns.GetHostEntry(icmpResult.IPEndPoint).HostName.ToString();
                    }
                    catch { }

                    if (icmpResult.Duration.Equals(TimeSpan.MaxValue))
                    {
                        icmpResult.Success      = false;
                        icmpResult.ErrorMessage = "Timed out";
                    }
                    else
                    {
                        hopNumber++;
                    }

                    if (icmpResult.IPEndPoint.Equals(icmpService.TargetIp.ToString()))
                    {
                        isTargetFound = true;
                    }

                    if (startTTL >= icmpAction.MaxTTL)
                    {
                        isTimeout = true;
                    }

                    hopNumber++;
                }
                catch
                {
                    nbrErr++;
                    if (nbrErr > 10)
                    {
                        isTimeout = true;
                    }
                }
                yield return(icmpResult);
            }
        }
Ejemplo n.º 3
0
        public IEnumerable <IcmpResult> Handle(IcmpAction action)
        {
            var icmpService = new IcmpService(action.Host);

            // Keep pinging until the amount of wanted echos is exchausted.
            while (action.NbrEcho > 0)
            {
                yield return(Ping(action, icmpService));

                action.NbrEcho--;
            }
        }
Ejemplo n.º 4
0
        private IEnumerable <IcmpResult> HandleIcmpAction(string host, IcmpType icmpType, int nbrEcho, int weightPacket, int updatePeriod, int timeout, int maxTTL)
        {
            IcmpAction icmpAction = new IcmpAction(host, icmpType, nbrEcho, weightPacket, updatePeriod, timeout, maxTTL);

            return(GetIcmpActionHandler(icmpType).Handle(icmpAction));
        }