public static void Main(string[] argv)
        {
            byte[] data = new byte[1024];
            int    recv;
            Socket host = new Socket(AddressFamily.InterNetwork, SocketType.Raw,
                                     ProtocolType.Icmp);
            IPEndPoint iep    = new IPEndPoint(IPAddress.Parse(argv[0]), 0);
            EndPoint   ep     = (EndPoint)iep;
            ICMP       packet = new ICMP();

            packet.Type     = 0x08;
            packet.Code     = 0x00;
            packet.Checksum = 0;
            Buffer.BlockCopy(BitConverter.GetBytes((short)1), 0, packet.Message, 0, 2);
            Buffer.BlockCopy(BitConverter.GetBytes((short)1), 0, packet.Message, 2, 2);
            data = Encoding.ASCII.GetBytes("test packet");
            Buffer.BlockCopy(data, 0, packet.Message, 4, data.Length);
            packet.MessageSize = data.Length + 4;
            int packetsize = packet.MessageSize + 4;

            UInt16 chcksum = packet.getChecksum();

            packet.Checksum = chcksum;

            host.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);
            host.SendTo(packet.getBytes(), packetsize, SocketFlags.None, iep);
            try
            {
                data = new byte[1024];
                recv = host.ReceiveFrom(data, ref ep);
            }
            catch (SocketException)
            {
                Console.WriteLine("No response from remote host");
                return;
            }
            ICMP response = new ICMP(data, recv);

            Console.WriteLine("response from: {0}", ep.ToString());
            Console.WriteLine("  Type {0}", response.Type);
            Console.WriteLine("  Code: {0}", response.Code);
            int Identifier = BitConverter.ToInt16(response.Message, 0);
            int Sequence   = BitConverter.ToInt16(response.Message, 2);

            Console.WriteLine("  Identifier: {0}", Identifier);
            Console.WriteLine("  Sequence: {0}", Sequence);
            string stringData = Encoding.ASCII.GetString(response.Message, 4, response.MessageSize - 4);

            Console.WriteLine("  data: {0}", stringData);

            host.Close();
        }
Exemple #2
0
        public static void Main()
        {
            byte[]     data = new byte[1024];
            int        recv;
            Socket     host   = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
            IPEndPoint iep    = new IPEndPoint(IPAddress.Broadcast, 0);
            EndPoint   ep     = (EndPoint)iep;
            ICMP       packet = new ICMP();

            packet.Type     = 0x11;
            packet.Code     = 0x00;
            packet.Checksum = 0;
            Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.Message, 0, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.Message, 2, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(0), 0, packet.Message, 4, 4);
            packet.MessageSize = 8;
            int packetsize = packet.MessageSize + 4;

            UInt16 chksm = packet.getChecksum();

            packet.Checksum = chksm;

            host.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);
            host.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
            host.SendTo(packet.getBytes(), packetsize, SocketFlags.None, iep);
            try
            {
                data = new byte[1024];
                recv = host.ReceiveFrom(data, ref ep);
            }
            catch (SocketException)
            {
                Console.WriteLine("Unable to determine subnet mask for this subnet");
                return;
            }
            ICMP response = new ICMP(data, recv);

            Console.WriteLine("Received ICMP Type {0} packet", response.Type);
            long      answer  = BitConverter.ToUInt32(response.Message, 4);
            IPAddress netmask = new IPAddress(answer);

            Console.WriteLine("The subnet mask for this subnet is: {0}", netmask.ToString());
        }
Exemple #3
0
        void sendPing()
        {
            IPHostEntry iphe = Dns.Resolve(hostbox.Text);
            IPEndPoint  iep = new IPEndPoint(iphe.AddressList[0], 0);
            EndPoint    ep = (EndPoint)iep;
            ICMP        packet = new ICMP();
            int         recv, i = 1;

            packet.Type = 0x08;
            packet.Code = 0x00;
            Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.Message, 0, 2);
            byte[] data = Encoding.ASCII.GetBytes(databox.Text);
            Buffer.BlockCopy(data, 0, packet.Message, 4, data.Length);
            packet.MessageSize = data.Length + 4;
            int packetsize = packet.MessageSize + 4;

            results.Items.Add("Pinging " + hostbox.Text);
            while (true)
            {
                packet.Checksum = 0;
                Buffer.BlockCopy(BitConverter.GetBytes(i), 0, packet.Message, 2, 2);
                UInt16 chcksum = packet.getChecksum();
                packet.Checksum = chcksum;

                pingstart = Environment.TickCount;
                sock.SendTo(packet.getBytes(), packetsize, SocketFlags.None, iep);
                try
                {
                    data        = new byte[1024];
                    recv        = sock.ReceiveFrom(data, ref ep);
                    pingstop    = Environment.TickCount;
                    elapsedtime = pingstop - pingstart;
                    results.Items.Add("reply from: " + ep.ToString() + ", seq: " + i +
                                      ", time = " + elapsedtime + "ms");
                }
                catch (SocketException)
                {
                    results.Items.Add("no reply from host");
                }
                i++;
                Thread.Sleep(3000);
            }
        }
Exemple #4
0
        public static void Main(string[] argv)
        {
            byte[]      data = new byte[1024];
            int         recv, timestart, timestop;
            Socket      host   = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
            IPHostEntry iphe   = Dns.Resolve(argv[0]);
            IPEndPoint  iep    = new IPEndPoint(iphe.AddressList[0], 0);
            EndPoint    ep     = (EndPoint)iep;
            ICMP        packet = new ICMP();

            packet.Type     = 0x08;
            packet.Code     = 0x00;
            packet.Checksum = 0;
            Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.Message, 0, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.Message, 2, 2);
            data = Encoding.ASCII.GetBytes("test packet");
            Buffer.BlockCopy(data, 0, packet.Message, 4, data.Length);
            packet.MessageSize = data.Length + 4;
            int packetsize = packet.MessageSize + 4;

            UInt16 chcksum = packet.getChecksum();

            packet.Checksum = chcksum;

            host.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);

            int badcount = 0;

            for (int i = 1; i < 50; i++)
            {
                host.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, i);
                timestart = Environment.TickCount;
                host.SendTo(packet.getBytes(), packetsize, SocketFlags.None, iep);
                try
                {
                    data     = new byte[1024];
                    recv     = host.ReceiveFrom(data, ref ep);
                    timestop = Environment.TickCount;
                    ICMP response = new ICMP(data, recv);
                    if (response.Type == 11)
                    {
                        Console.WriteLine("hop {0}: response from {1}, {2}ms", i, ep.ToString(), timestop - timestart);
                    }
                    if (response.Type == 0)
                    {
                        Console.WriteLine("{0} reached in {1} hops, {2}ms.", ep.ToString(), i, timestop - timestart);
                        break;
                    }
                    badcount = 0;
                }
                catch (SocketException)
                {
                    Console.WriteLine("hop {0}: No response from remote host", i);
                    badcount++;
                    if (badcount == 5)
                    {
                        Console.WriteLine("Unable to contact remote host");
                        break;
                    }
                }
            }

            host.Close();
        }