Beispiel #1
0
 //ICMP协议的Info
 private string setIcmpInfo(ICMPv4Packet ICMP, string MF, int OFF)
 {
     //string type = ICMP.Header[0].ToString("D");
     if (isFragment(MF, OFF) && OFF != 0)
         return "Fragment packet";
     else if (isFragment(MF, OFF) && OFF == 0)
     {
         switch (ICMP.Header[0].ToString("D"))
         {
             case "8":
                 return "Fragment packet. Echo (ping) request  id=0x" + ICMP.ID.ToString("X4") + ",  seq=" + ICMP.Sequence;
             case "0":
                 return "Fragment packet. Echo (ping) reply  id=0x" + ICMP.ID.ToString("X4") + ",  seq=" + ICMP.Sequence;
             case "3":
                 return "Fragment packet. Destination unreachable";
             default:
                 return "Fragment packet. Unknown type/code";
         }
     }
     else
     {
         switch (ICMP.Header[0].ToString("D"))
         {
             case "8":
                 return "Echo (ping) request  id=0x" + ICMP.ID.ToString("X4") + ",  seq=" + ICMP.Sequence;
             case "0":
                 return "Echo (ping) reply  id=0x" + ICMP.ID.ToString("X4") + ",  seq=" + ICMP.Sequence;
             case "3":
                 return "Destination unreachable";
             default:
                 return "Unknown type/code";
         }
     }
 }
Beispiel #2
0
        public int sendPacket()
        {
            byte[] bytes = new byte[4096];
            ByteArraySegment bas = new ByteArraySegment(bytes);
            ushort srcPort = 23444;
            ushort dstPort = 12345;

            PacketDotNet.UdpPacket udpPacket = new UdpPacket(srcPort, dstPort);
            string cmdString = "xxxxyyyyHello world!";
            byte[] sendBuffer = Encoding.ASCII.GetBytes(cmdString);
            udpPacket.PayloadData = sendBuffer;

            ICMPv4Packet icmpPacket = new ICMPv4Packet(new ByteArraySegment(sendBuffer));
            // sanity check:
            //bas.BytesLength = 10;
            //Console.WriteLine("bas - Offset = " + bas.Offset + " len= " + bas.Length);
            
            IPAddress ipSrcAddr = System.Net.IPAddress.Parse("192.168.0.186"); // laptop
            IPAddress ipDestAddr = System.Net.IPAddress.Parse("192.168.0.185"); // my linux box
            IpPacket ipPacket = new IPv4Packet(ipSrcAddr, ipDestAddr);
            ipPacket.PayloadPacket = udpPacket;

            icmpPacket.TypeCode = ICMPv4TypeCodes.Unassigned1;
            icmpPacket.Sequence = 1;

            Console.WriteLine("icmpPacket - TypeCode = " + icmpPacket.TypeCode + " Sequence= " + icmpPacket.Sequence);
            Console.WriteLine("icmpPacket : " + icmpPacket.PrintHex());
            icmpPacket.UpdateCalculatedValues();
            Console.WriteLine("icmpPacket : " + icmpPacket.PrintHex());

            //ushort etype = 0xFF00; //EthernetPacketType RAW ?
            System.Net.NetworkInformation.PhysicalAddress ethSrcAddr = System.Net.NetworkInformation.PhysicalAddress.Parse("02-1E-EC-8F-7F-E1");
            System.Net.NetworkInformation.PhysicalAddress ethDstAddr = System.Net.NetworkInformation.PhysicalAddress.Parse("48-5B-39-ED-96-36");
            EthernetPacket ethernetPacket = new EthernetPacket(ethSrcAddr, ethDstAddr, EthernetPacketType.IpV4);
            // I thought "None" for type would fill in type automatically; but it remained zero on the wire and was flagged "Malformed"
            ethernetPacket.PayloadPacket = icmpPacket;
            Console.WriteLine("ethernetPacket : " + ethernetPacket.PrintHex());
            ethernetPacket.Type = EthernetPacketType.IpV4;
            
            Console.WriteLine("ethernetPacket : " + ethernetPacket.PrintHex());

            //ipPacket.PayloadPacket = udpPacket;
            //ethernetPacket.PayloadPacket = gmpPacket; //ipPacket;
            ethernetPacket.UpdateCalculatedValues();
            Console.WriteLine(ethernetPacket.ToString());

            ipPacket.UpdateCalculatedValues();
            //udpPacket.UpdateUDPChecksum();
            // Experiment with raw ip packet?
            ipPacket.Protocol = IPProtocolType.RAW;

            // Why isn't ValidChecksum true?
            //Console.WriteLine("After updating calculated values, ValidUDPChecksum = " + udpPacket.ValidUDPChecksum + " ValidChecksum = " + udpPacket.ValidChecksum);
            //Console.WriteLine(ethernetPacket.ToString());

            device.Open(DeviceMode.Normal, 15000);  // 15 sec timeout

            //ushort checksum = csum(ipPacket.BytesHighPerformance, ipPacket.P);
            device.SendPacket(ethernetPacket);

            return 0;
        }
 void listField(ref ICMPv4Packet p)
 {
     addField("ICMPv4", "Checksum", p);
     addField("ICMPv4", "ID", p);
     addField("ICMPv4", "Sequence", p);
     addField("ICMPv4", "Type Code", p);
 }
Beispiel #4
0
        public static String ICMPv4Analyzor(ref TreeView tree, ICMPv4Packet icmp4)
        {
            string info = "\r\n-------- ICMPv4 Header --------\r\n";
            TreeNode ICMP4Node = new TreeNode("ICMP(Internet Control Message Protocol)v4 Header");
            string type, code, chk, ecp;

            try
            {
                ICMP4Node.Nodes.Add(type = "Type = " + icmp4.TypeCode.ToString());
                ICMP4Node.Nodes.Add(code = "Code = " + icmp4.ID.ToString());
                ICMP4Node.Nodes.Add(chk = "Checksum = " + icmp4.Checksum.ToString());
                info += type + "\r\n" + code + "\r\n" + chk + "\r\n";
            }
            catch (Exception)
            {
                ICMP4Node.Nodes.Add(ecp = "ICMP segment or incomplete header");
                info += ecp + "\r\n";
            }
            tree.Nodes.Add(ICMP4Node);
            return info;
        }