예제 #1
0
 private void UpdateUI(TracertEntry r)
 {
     if (this.InvokeRequired)
     {
         this.Invoke(new Action <TracertEntry>(UpdateUI), r);
     }
     else
     {
         listBox1.Items.Add(r.ToString());
     }
 }
예제 #2
0
        private async void TraceRoute(int hopLimit, string hostNameOrIp, CancellationToken ct)
        {
            if (ct.IsCancellationRequested == true)
            {
                return;
                //ct.ThrowIfCancellationRequested();
            }


            for (int hopIndex = 0; hopIndex < hopLimit; hopIndex++)
            {
                int ttl     = hopIndex + 1;
                int timeout = 200;

                // string data = Guid.NewGuid().ToString();
                byte[] dataBytes = new byte[] { 0 };//Encoding.ASCII.GetBytes(data);
                Ping   ping      = new Ping();


                string           ip = " ", local = " ", hostname = " ", reply_time = " ";
                IPStatus         status        = IPStatus.Unknown;
                Stopwatch        pingReplyTime = new Stopwatch();
                List <string>    times         = new List <string>();
                List <IPAddress> ips           = new List <IPAddress>();
                for (int i = 0; i < 3; i++)
                {
                    pingReplyTime.Start();
                    PingReply pingReply = await ping.SendPingAsync(hostNameOrIp, timeout, dataBytes, new PingOptions(ttl, true));

                    times.Add(pingReplyTime.ElapsedMilliseconds.ToString());
                    pingReplyTime.Reset();

                    ips.Add(pingReply.Address);
                    status = pingReply.Status;
                    // reply_time = pingReplyTime.ElapsedMilliseconds.ToString() + "ms";
                }



                var ipa = ips.FirstOrDefault(t => t != null);

                reply_time = string.Join(" / ", times);// + " ms";
                if (reply_time.Length < 12)
                {
                    reply_time = "      " + reply_time;
                }

                if (ipa != null)
                {
                    ip = ipa.ToString();


                    if (ip == "0.0.0.0")
                    {
                        ip       = "*";
                        local    = "";
                        hostname = "";
                    }
                    else
                    {
                        string   t   = IPSearch3Fast.Instance.Find(ip);
                        string[] r   = t.Split('|');
                        string   pcd = "";
                        if (r.Length > 3)
                        {
                            pcd = r[1] + r[2] + r[3] + r[4] + (r[5] == "" ? "" : "(" + r[5] + ")");
                        }
                        local = pcd;

                        if (r[5] != "保留")
                        {
                            hostname = GetReverseDNS(ip, r[1] == "中国" ? 60 : 200);
                        }
                    }
                }

                var entity = new TracertEntry()
                {
                    HopIndex    = hopIndex + 1,
                    Ip          = ip,
                    Hostname    = hostname,
                    Local       = local,
                    ReplyTime   = reply_time,
                    ReplyStatus = status
                };
                UpdateUI(entity);


                if (status == IPStatus.Success)
                {
                    break;
                }

                if (ct.IsCancellationRequested)
                {
                    break;
                }
            }
        }