Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 C # N e t w o r k P r o g r a m m i n g
Part 3 _ chapter 11 ICMP >>> 430
Chapter 11 _ 1 431
Part 3 _ 432
Chapter 11 _ N o t e 433
Part 3 _ 2 434
Chapter 11 _ Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp); IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.1.2"), 0); sock.sendto(packet, iep); W a r ning 435
Part 3 _ W a r ning 3 436
Chapter 11 _ class ICMP public byte Type; public byte Code; public UInt16 Checksum; public int MessageSize; public byte[] Message = new byte[1024]; public ICMP() 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); byte[] data = Encoding.ASCII.GetBytes("test packet"); 437
Part 3 _ Buffer.BlockCopy(data, 0, packet.message, 4, data.length); packet.messagesize = data.length + 4; public ICMP(byte[] data, int size) Type = data[20]; Code = data[21]; Checksum = BitConverter.ToUInt16(data, 22); MessageSize = size - 24; Buffer.BlockCopy(data, 24, Message, 0, MessageSize); int recv = ReceiveFrom(data, ref ep); ICMP response = new ICMP(data, recv); Console.WriteLine("Received ICMP packet:"); Console.WriteLine(" Type 0", response.type); Console.WriteLine(" Code: 0", response.code); Int16 Identifier = BitConverter.ToInt16(response.Message, 0); Int16 Sequence = BitConverter.ToInt16(response.Message, 2); Console.WriteLine(" Identifier: 0", Identifier); Console.WriteLine(" Sequence: 0", Sequence); stringdata = Encoding.ASCII.GetString(response.Message, 4, response.messagesize - 4); Console.WriteLine(" data: 0", stringdata); 438
Chapter 11 _ public byte[] getbytes() byte[] data = new byte[messagesize + 9]; Buffer.BlockCopy(BitConverter.GetBytes(Type), 0, data, 0, 1); Buffer.BlockCopy(BitConverter.GetBytes(Code), 0, data, 1, 1); Buffer.BlockCopy(BitConverter.GetBytes(Checksum), 0, data, 2, 2); Buffer.BlockCopy(Message, 0, data, 4, MessageSize); return data; IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.1.2"), 0); sock.sendto(packet.getbytes(), iep); 439
Part 3 _ T i p L i Sting public UInt16 getchecksum() UInt32 chcksm = 0; byte[] data = getbytes(); int packetsize = MessageSize + 8; int index = 0; while ( index < packetsize) chcksm += Convert.ToUInt32(BitConverter.ToUInt16(data, index)); index += 2; chcksm = (chcksm >> 16) + (chcksm & 0xffff); chcksm += (chcksm >> 16); return (UInt16)(~chcksm); 440
Chapter 11 _ packet.checksum = 0; packet.checksum = packet.getchecksum(); N o t e L i Sting using System; using System.Net; using System.Text; class ICMP public byte Type; public byte Code; public UInt16 Checksum; public int MessageSize; public byte[] Message = new byte[1024]; public ICMP() 441
Part 3 _ public ICMP(byte[] data, int size) Type = data[20]; Code = data[21]; Checksum = BitConverter.ToUInt16(data, 22); MessageSize = size - 24; Buffer.BlockCopy(data, 24, Message, 0, MessageSize); public byte[] getbytes() byte[] data = new byte[messagesize + 9]; Buffer.BlockCopy(BitConverter.GetBytes(Type), 0, data, 0, 1); Buffer.BlockCopy(BitConverter.GetBytes(Code), 0, data, 1, 1); Buffer.BlockCopy(BitConverter.GetBytes(Checksum), 0, data, 2, 2); Buffer.BlockCopy(Message, 0, data, 4, MessageSize); return data; public UInt16 getchecksum() UInt32 chcksm = 0; byte[] data = getbytes(); int packetsize = MessageSize + 8; int index = 0; while ( index < packetsize) chcksm += Convert.ToUInt32(BitConverter.ToUInt16(data, index)); index += 2; chcksm = (chcksm >> 16) + (chcksm & 0xffff); chcksm += (chcksm >> 16); return (UInt16)(~chcksm); 442
Chapter 11 _ 4 Echo Request Type = 8 Code = 0 Checksum Identifier = 1 Sequence = 1 Message = test packet Server Type = 0Code = 0 Checksum Identifier = 1 Sequence = 1 L i Sting using System; using System.Net; using System.Net.Sockets; using System.Text; class SimplePing 443
Part 3 _ 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); 444
Chapter 11 _ Console.WriteLine(" Sequence: 0", Sequence); string stringdata = Encoding.ASCII.GetString(response.Message, 4, response.messagesize - 4); Console.WriteLine(" data: 0", stringdata); host.close(); csc SimplePing.cs ICMP.cs C:\>SimplePing 127.0.0.1 445
Part 3 _ response from: 127.0.0.1:0 Type 0 Code: 0 Identifier: 1 Sequence: 1 data: test packet C:\> C:\>SimplePing 192.168.1.111 No response from remote host C:\> C:\>SimplePing 192.168.1.2 response from: 192.168.1.2:0 Type 0 Code: 0 Identifier: 1 Sequence: 1 data: test packet C:\> W a r ning 446
Chapter 11 _ 5 IPHostEntry iphe = Dns.Resolve(hostbox.Text); IPEndPoint iep = new IPEndPoint(iphe.AddressList[0], 0); 447
Part 3 _ L i Sting using System; using System.Drawing; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Windows.Forms; class AdvPing : Form private static int pingstart, pingstop, elapsedtime; private static TextBox hostbox, databox; private static ListBox results; private static Thread pinger; private static Socket sock; public AdvPing() Text = "Advanced Ping Program"; Size = new Size(400, 380); Label label1 = new Label(); label1.parent = this; label1.text = "Enter host to ping:"; label1.autosize = true; label1.location = new Point(10, 30); hostbox = new TextBox(); hostbox.parent = this; hostbox.size = new Size(200, 2 * Font.Height); hostbox.location = new Point(10, 55); results = new ListBox(); results.parent = this; results.location = new Point(10, 85); results.size = new Size(360, 18 * Font.Height); Label label2 = new Label(); label2.parent = this; label2.text = "Packet data:"; label2.autosize = true; label2.location = new Point(10, 330); 448
Chapter 11 _ databox = new TextBox(); databox.parent = this; databox.text = "test packet"; databox.size = new Size(200, 2 * Font.Height); databox.location = new Point(80, 325); Button sendit = new Button(); sendit.parent = this; sendit.text = "Start"; sendit.location = new Point(220,52); sendit.size = new Size(5 * Font.Height, 2 * Font.Height); sendit.click += new EventHandler(ButtonSendOnClick); Button stopit = new Button(); stopit.parent = this; stopit.text = "Stop"; stopit.location = new Point(295,52); stopit.size = new Size(5 * Font.Height, 2 * Font.Height); stopit.click += new EventHandler(ButtonStopOnClick); Button closeit = new Button(); closeit.parent = this; closeit.text = "Close"; closeit.location = new Point(300, 320); closeit.size = new Size(5 * Font.Height, 2 * Font.Height); closeit.click += new EventHandler(ButtonCloseOnClick); sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp); sock.setsocketoption(socketoptionlevel.socket, SocketOptionName.ReceiveTimeout, 3000); void ButtonSendOnClick(object obj, EventArgs ea) pinger = new Thread(new ThreadStart(sendPing)); pinger.isbackground = true; pinger.start(); void ButtonStopOnClick(object obj, EventArgs ea) pinger.abort(); results.items.add("ping stopped"); 449
Part 3 _ void ButtonCloseOnClick(object obj, EventArgs ea) sock.close(); Close(); 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"); 450
Chapter 11 _ i++; Thread.Sleep(3000); public static void Main() Application.Run(new AdvPing()); sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp); sock.setsocketoption(socketoptionlevel.socket, SocketOptionName.ReceiveTimeout, 3000); csc /t:winexe AdvPing.cs ICMP.cs 451
Part 3 _ 6 452
Chapter 11 _ L i Sting using System; using System.Net; using System.Net.Sockets; using System.Text; class TraceRoute 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; 453
Part 3 _ UInt16 chcksum = packet.getcchecksum(); 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, 2ms", i, ep.tostring(), timestop-timestart); if (response.type == 0) Console.WriteLine("0 reached in 1 hops, 2ms.", 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(); 454
Chapter 11 _ 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); csc TraceRoute.cs ICMP.cs C:\>TraceRoute www.cisco.com hop 1: No response from remote host hop 2: response from 206.148.207.106:0, 220ms hop 3: response from 65.123.106.113:0, 140ms 455
Part 3 _ hop 4: response from 205.171.20.125:0, 141ms hop 5: response from 205.171.20.142:0, 140ms hop 6: response from 205.171.1.162:0, 130ms hop 7: response from 144.232.26.54:0, 130ms hop 8: response from 144.232.8.117:0, 191ms hop 9: response from 144.232.3.138:0, 190ms hop 10: response from 144.228.44.14:0, 190ms hop 11: response from 128.107.239.89:0, 190ms hop 12: response from 128.107.239.102:0, 191ms 198.133.219.25:0 reached in 13 hops, 180ms. C:\> 7 456
Chapter 11 _ ICMP response = new ICMP(data, recv); long answer = BitConverter.ToUInt32(response.Data, 4); IPAddress netmask = new IPAddress(answer); L i Sting using System; using System.Net; using System.Net.Sockets; using System.Text; class FindMask public static void Main () byte[] data = new byte[1024]; int recv; Socket host = new Socket(AddressFamily.InterNetwork, 457
Part 3 _ 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 an 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()); 458
Chapter 11 _ csc FindMask.cs ICMP.cs host.setsocketoption(socketoptionlevel.socket, SocketOptionName.ReceiveTimeout, 3000); host.setsocketoption(socketoptionlevel.socket, SocketOptionName.Broadcast, 1); C:\>FindMask Received an ICMP type 18 packet The subnet mask for this subnet is: 255.255.252.0 C:\> 459
Part 3 _ L i Sting C:\>windump -X icmp windump listening on\device\packet_el90x1 13:21:49.576074 192.168.1.32 > 255.255.255.255: icmp: address mask request 0x0000 4500 0020 b355 0000 8001 6c55 c0a8 0120 E...U...lU... 0x0010 ffff ffff 1100 ecff 0100 0100 0000 0000... 13:21:49.576203 192.168.1.149 > 192.168.1.32: icmp: address mask is 0xfffffc00 0x0000 4500 0020 6103 0000 ff01 23ff c0a8 0195 E...a...#... 0x0010 c0a8 0120 1200 effe 0100 0100 ffff fc00... 0x0020 0000 0000 0000 0000 0000 0000 0000... 13:21:49.576236 192.168.1.143 > 192.168.1.32: icmp: address mask is 0xfffffc00 0x0000 4500 0020 8db8 0000 ff01 f74f c0a8 018f E...O... 0x0010 c0a8 0120 1200 effe 0100 0100 ffff fc00... 0x0020 0000 0000 0000 0000 0000 0000 0000... 13:21:49.576320 192.168.1.5 > 192.168.1.32: icmp: address mask is 0xfffffc00 0x0000 4500 0020 de96 0000 3c01 6afc c0a8 0105 E...<.j.... 0x0010 c0a8 0120 1200 effe 0100 0100 ffff fc00... 0x0020 0000 0000 0000 0000 0000 0000 0000 0000... 0x0030 0000 0000 0000... 408 packets received by filter 0 packets dropped by kernel C:\> 460
Chapter 11 _ 8 461