Exemplo n.º 1
0
Arquivo: Icmp.cs Projeto: dioptre/nkd
		public static string ToStringEx(EchoMessage[] messages)
		{
			string retVal = "";

			foreach(EchoMessage m in messages){
				retVal += m.ToStringEx() + "\r\n";
			}

			return retVal;
        }
Exemplo n.º 2
0
        //	public Icmp()
        //	{
        //	}


        #region function Trace

        /// <summary>
        /// Traces specified ip.
        /// </summary>
        /// <param name="destIP"></param>
        /// <returns></returns>
        public static EchoMessage[] Trace(string destIP)
        {
            ArrayList retVal = new ArrayList();

            //Create Raw ICMP Socket
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);

            IPEndPoint ipdest   = new IPEndPoint(System.Net.IPAddress.Parse(destIP), 80);
            EndPoint   endpoint = (EndPoint)(new IPEndPoint(System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList[0], 80));

            ushort id = (ushort)DateTime.Now.Millisecond;

            byte[] ByteSend = CreatePacket(id);

            int continuesNoReply = 0;

            //send requests with increasing number of TTL
            for (int ittl = 1; ittl <= 30; ittl++)
            {
                byte[] ByteRecv = new byte[256];

                try
                {
                    //Socket options to set TTL and Timeouts
                    s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, ittl);
                    s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 4000);
                    s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 4000);

                    //Get current time
                    DateTime startTime = DateTime.Now;

                    //Send Request
                    s.SendTo(ByteSend, ByteSend.Length, SocketFlags.None, ipdest);

                    //Receive
                    s.ReceiveFrom(ByteRecv, ByteRecv.Length, SocketFlags.None, ref endpoint);

                    //Calculate time required
                    TimeSpan ts = DateTime.Now - startTime;
                    retVal.Add(new EchoMessage(((IPEndPoint)endpoint).Address.ToString(), ittl, ts.Milliseconds));

                    // Endpoint reached
                    if (ByteRecv[20] == (byte)ICMP_Type.EchoReply)
                    {
                        break;
                    }

                    // Un wanted reply
                    if (ByteRecv[20] != (byte)ICMP_Type.TimeExceeded)
                    {
                        throw new Exception("UnKnown error !");
                    }

                    continuesNoReply = 0;
                }
                catch {
                    //ToDo: Handle recive/send timeouts
                    continuesNoReply++;
                }

                // If there is 3 continues no reply, consider that destination host won't accept ping.
                if (continuesNoReply >= 3)
                {
                    break;
                }
            }

            EchoMessage[] val = new EchoMessage[retVal.Count];
            retVal.CopyTo(val);
            return(val);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Traces specified ip.
        /// </summary>
        /// <param name="destIP"></param>
        /// <returns></returns>
        public static EchoMessage[] Trace(string destIP)
        {
            ArrayList retVal = new ArrayList();

            //Create Raw ICMP Socket
            Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Raw,ProtocolType.Icmp);

            IPEndPoint ipdest = new IPEndPoint(System.Net.IPAddress.Parse(destIP),80);
            EndPoint endpoint = (EndPoint)(new IPEndPoint(System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList[0],80));

            ushort id = (ushort)DateTime.Now.Millisecond;
            byte[] ByteSend= CreatePacket(id);

            int continuesNoReply = 0;
            //send requests with increasing number of TTL
            for(int ittl=1;ittl<=30; ittl++){
                byte[] ByteRecv = new byte[256];

                try
                {
                    //Socket options to set TTL and Timeouts
                    s.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.IpTimeToLive      ,ittl);
                    s.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.SendTimeout   ,4000);
                    s.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReceiveTimeout,4000);

                    //Get current time
                    DateTime startTime = DateTime.Now;

                    //Send Request
                    s.SendTo(ByteSend,ByteSend.Length,SocketFlags.None,ipdest);

                    //Receive
                    s.ReceiveFrom(ByteRecv,ByteRecv.Length,SocketFlags.None,ref endpoint);

                    //Calculate time required
                    TimeSpan ts = DateTime.Now - startTime;
                    retVal.Add(new EchoMessage(((IPEndPoint)endpoint).Address.ToString(),ittl,ts.Milliseconds));

                    // Endpoint reached
                    if(ByteRecv[20] == (byte)ICMP_Type.EchoReply){
                        break;
                    }

                    // Un wanted reply
                    if(ByteRecv[20] != (byte)ICMP_Type.TimeExceeded){
                        throw new Exception("UnKnown error !");
                    }

                    continuesNoReply = 0;
                }
                catch{
                    //ToDo: Handle recive/send timeouts
                    continuesNoReply++;
                }

                // If there is 3 continues no reply, consider that destination host won't accept ping.
                if(continuesNoReply >= 3){
                    break;
                }
            }

            EchoMessage[] val = new EchoMessage[retVal.Count];
            retVal.CopyTo(val);
            return val;
        }