Example #1
0
        static void Traceroute(Socket socket, ICMPPacket packet, IPEndPoint destIPEndPoint)
        {
            int timeStart, timeEnd, responseSize, errCount = 0;

            byte[] responseMessage;

            EndPoint hopEndPoint = destIPEndPoint;

            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);
            for (int i = 1; i < 50; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, i);
                    timeStart = Environment.TickCount;
                    socket.SendTo(packet.getBytes(), packet.PacketSize, SocketFlags.None, destIPEndPoint);
                    try
                    {
                        responseMessage = new byte[MESSAGE_MAX_SIZE];
                        responseSize    = socket.ReceiveFrom(responseMessage, ref hopEndPoint);
                        timeEnd         = Environment.TickCount;
                        ICMPPacket response = new ICMPPacket(responseMessage, responseSize);
                        if ((response.Type == 0) || (response.Type == 11))
                        {
                            Console.Write("{0} мс\t", timeEnd - timeStart);
                            if (j == 2)
                            {
                                Console.WriteLine("{0}: {1}", i, hopEndPoint.ToString());
                            }
                        }
                        if ((response.Type == 0) && (j == 2))
                        {
                            Console.WriteLine("Трассировка завершена.");
                            return;
                        }
                        errCount = 0;
                    }
                    catch (SocketException)
                    {
                        Console.Write("*\t");
                        if (j == 2)
                        {
                            Console.WriteLine("{0}: Превышен интервал ожидания для запроса.", i); //
                        }
                        errCount++;
                        if (errCount == 30)
                        {
                            Console.WriteLine("Невозможно связаться с удаленным хостом."); //
                            return;
                        }
                    }
                }
            }
        }
Example #2
0
        static void Traceroute(Socket socket, ICMPPacket packet, IPEndPoint destIP)
        {
            int      responseSize;
            DateTime timeStart, timeEnd;

            byte[] responseMessage;

            EndPoint hopIP = destIP;

            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);
            for (int i = 1; i < 31; i++)
            {
                EndPoint BufPoint = null;
                Console.Write("{0, 3} ", i);
                socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, i);
                for (int j = 0; j < 3; j++)
                {
                    timeStart = DateTime.Now;
                    socket.SendTo(packet.getBytes(), packet.PacketSize, SocketFlags.None, destIP);
                    try
                    {
                        responseMessage = new byte[MESSAGE_SIZE];
                        responseSize    = socket.ReceiveFrom(responseMessage, ref hopIP);
                        timeEnd         = DateTime.Now;
                        ICMPPacket response = new ICMPPacket(responseMessage, responseSize);
                        if ((response.Type == TYPE_ECHO_RESPONSE) || (response.Type == TYPE_TTL_EXCEEDED))
                        {
                            BufPoint = hopIP;
                            if ((timeEnd.Ticks - timeStart.Ticks) / 10000 > 0)
                            {
                                Console.Write("{0, 5} ms", (timeEnd.Ticks - timeStart.Ticks) / 10000);
                            }
                            else
                            {
                                Console.Write("   <1 ms");
                            }
                            if (j == 2)
                            {
                                string IP = hopIP.ToString();
                                IP = IP.Remove(IP.IndexOf(':'), IP.Length - IP.IndexOf(':'));
                                try
                                {
                                    if (showName)
                                    {
                                        string HostName = Dns.GetHostEntry(IP).HostName;
                                        Console.WriteLine("   {0} [{1}]", HostName, IP);
                                    }
                                    else
                                    {
                                        Console.WriteLine("   {0}", IP);
                                    }
                                }
                                catch (SocketException)
                                {
                                    Console.WriteLine("   {0}", IP);
                                }
                            }
                        }
                        if ((response.Type == TYPE_ECHO_RESPONSE) && (j == 2))
                        {
                            Console.WriteLine("\nТрассировка завершена.");
                            return;
                        }
                    }
                    catch (SocketException)
                    {
                        Console.Write("    *   ");
                        if (j == 2 && BufPoint == null)
                        {
                            Console.WriteLine("   Превышен интервал ожидания для запроса.", i);
                        }
                        else if (j == 2)
                        {
                            string IP = hopIP.ToString();
                            IP = IP.Remove(IP.IndexOf(':'), IP.Length - IP.IndexOf(':'));
                            try
                            {
                                if (showName)
                                {
                                    string HostName = Dns.GetHostEntry(IP).HostName;
                                    Console.WriteLine("{0} [{1}]", IP, HostName);
                                }
                                else
                                {
                                    Console.WriteLine("{0}", IP);
                                }
                            }
                            catch (SocketException)
                            {
                                Console.WriteLine("{0}", IP);
                            }
                        }
                    }
                    packet.incSeq();
                }
                if (i == 30)
                {
                    Console.WriteLine("Невозможно связаться с удаленным хостом.");
                }
            }
        }