Example #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);
            }
        }
Example #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--;
            }
        }
Example #4
0
        public void IcmpTest()
        {
            var address = new Address()
            {
                HostName = "google.com",
                Port     = 80,
                Prefix   = "http",
                Timeout  = 1000,
                Type     = "Icmp"
            };
            var mockLogger  = new Mock <ILogger>();
            var icmpService = new IcmpService(mockLogger.Object);

            Assert.DoesNotThrow(() => { icmpService.Start(address); });
        }