public TraceRouteNodeFoundEventArgs(TraceRouteHopDetail detail)
     : this()
 {
     Detail = detail;
 }
Exemple #2
0
        /// <summary>
        /// Executes the trace route.
        /// </summary>
        /// <param name="host">The destination host to which you wish to find the route.</param>
        /// <returns>All of the hops between the current computers and the
        /// <paramref name="host"/> computer.</returns>
        /// <exception cref="ArgumentException"></exception>
        public Task <IEnumerable <TraceRouteHopDetail> > ExecuteTraceRoute(String host)
        {
            if (String.IsNullOrWhiteSpace(host))
            {
                throw new ArgumentException("Argument \"host\" cannot be null or empty.", "host");
            }

            return(Task <IEnumerable <TraceRouteHopDetail> > .Factory.StartNew(() =>
            {
                List <TraceRouteHopDetail> output = new List <TraceRouteHopDetail>();

                using (Ping ping = new Ping())
                {
                    PingOptions options = new PingOptions(1, true);
                    Byte[] buffer = new Byte[32];

                    PingReply reply = ping.Send(host, 5000, buffer, options);

                    while (true)
                    {
                        if (CurrentCancellationTokenSource.IsCancellationRequested)
                        {
                            break;
                        }

                        if (reply.Address == null)
                        {
                            TraceRouteHopDetail detail = new TraceRouteHopDetail(options.Ttl, "*",
                                                                                 "***Request Timed Out***", new TimeSpan());
                            output.Add(detail);

                            if (TraceRouteNodeFound != null)
                            {
                                TraceRouteNodeFound(this, new TraceRouteNodeFoundEventArgs(detail));
                            }
                        }
                        else
                        {
                            String hostName = reply.Address.ToString();

                            try
                            {
                                hostName = Dns.GetHostEntry(reply.Address).HostName;
                            }
                            catch (SocketException)
                            {
                            }

                            TimeSpan responseTime = GetResponseTime(reply.Address.ToString());

                            // If we hit the last address or found the host, then stop.
                            if (reply.Address.Equals(lastReplyAddress) || reply.Address.ToString().Equals(host))
                            {
                                break;
                            }

                            TraceRouteHopDetail detail = new TraceRouteHopDetail(options.Ttl, reply.Address.ToString(),
                                                                                 hostName, responseTime);

                            output.Add(detail);

                            if (TraceRouteNodeFound != null)
                            {
                                TraceRouteNodeFound(this, new TraceRouteNodeFoundEventArgs(detail));
                            }
                        }
                        if (options.Ttl >= 30)
                        {
                            break;
                        }

                        lastReplyAddress = reply.Address;

                        options.Ttl += 1;
                        reply = ping.Send(host, 5000, buffer, options);
                    }

                    if (TraceRouteComplete != null)
                    {
                        TraceRouteComplete(this, new TraceRouteCompleteEventArgs(output));
                    }

                    return output;
                }
            }, CurrentCancellationTokenSource.Token));
        }