Ejemplo n.º 1
0
        static void ProcessPacket(IntPtr pData, WinPcap.WinPcap.PCAP_PKTHDR pkt_hdr)
        {
            WinPcap.WinPcap.ETHERNET_HEADER e_hdr = Marshal.PtrToStructure(pData, typeof(WinPcap.WinPcap.ETHERNET_HEADER)) as WinPcap.WinPcap.ETHERNET_HEADER;
            Console.WriteLine();
            Console.WriteLine("Ethernet Header:");
            Console.WriteLine("Destination Address:\t\t{0}", e_hdr.GetENDestinationAddress());
            Console.WriteLine("Source Address:\t\t\t{0}", e_hdr.GetENSourceAddress());
            Console.WriteLine("Ethernet Type:\t\t\t0x{0:X4}", e_hdr.GetENType());

            Console.WriteLine();
            int en_hdr_size = Marshal.SizeOf(typeof(WinPcap.WinPcap.ETHERNET_HEADER));

            switch (e_hdr.GetENType())
            {
            case WinPcap.WinPcap.ETHERNET_IP:
                WinPcap.WinPcap.IP_HEADER ip_hdr = Marshal.PtrToStructure(pData + en_hdr_size, typeof(WinPcap.WinPcap.IP_HEADER)) as WinPcap.WinPcap.IP_HEADER;
                Console.WriteLine("IP Header:");
                Console.WriteLine("Version:\t\t\t{0}", ip_hdr.GetIPVersion());
                Console.WriteLine("Header Length:\t\t\t{0}", ip_hdr.GetIPHeaderLength());
                Console.WriteLine("Type Of Sevice:\t\t\t{0}", ip_hdr.GetIPTypeOfService());
                Console.WriteLine("Length:\t\t\t\t{0}", ip_hdr.GetIPPacketLength());
                Console.WriteLine("ID:\t\t\t\t{0}", ip_hdr.GetIPID());
                Console.WriteLine("Flags:\t\t\t\t{0:D16}", ulong.Parse(Convert.ToString(ip_hdr.GetIPFlags(), 2)));
                Console.WriteLine("Don't Fragment:\t\t\t{0}", ip_hdr.GetIPDontFragment());
                Console.WriteLine("More Fragments:\t\t\t{0}", ip_hdr.GetIPMoreFragments());
                Console.WriteLine("Fragment Offset:\t\t{0}", ip_hdr.GetIPFragmentOffset());
                Console.WriteLine("Time To Live(TTL):\t\t{0}", ip_hdr.GetIPTimeToLive());
                Console.WriteLine("Protocol:\t\t\t{0}", ip_hdr.GetIPProtocol());
                Console.WriteLine("Checksum:\t\t\t0x{0:X4}", ip_hdr.GetIPChecksum());
                Console.WriteLine("Source IP:\t\t\t{0}", ip_hdr.GetIPSourceIP());
                Console.WriteLine("Destination IP:\t\t\t{0}", ip_hdr.GetIPDestinationIP());

                int ip_hdr_size = ip_hdr.GetIPHeaderLength();
                ip_hdr.RecalcChecksum(pData + en_hdr_size);
                ProcessIPPacket(pData + en_hdr_size + ip_hdr_size, ip_hdr);
                break;

            case WinPcap.WinPcap.ETHERNET_ARP:
            case WinPcap.WinPcap.ETHERNET_RARP:
                WinPcap.WinPcap.ARP_HEADER arp_hdr = Marshal.PtrToStructure(pData + en_hdr_size, typeof(WinPcap.WinPcap.ARP_HEADER)) as WinPcap.WinPcap.ARP_HEADER;
                Console.WriteLine("ARP Header:");
                Console.WriteLine("Format Of Hardware Address:\t0x{0:X4}", arp_hdr.GetARPHardwareAddress());
                Console.WriteLine("Format Of Protocol Address:\t0x{0:X4}", arp_hdr.GetARPProtocolAddress());
                Console.WriteLine("Length Of Hardware Address:\t{0}", arp_hdr.GetARPHardwareAddressLength());
                Console.WriteLine("Length Of Protocol Address:\t{0}", arp_hdr.GetARPProtocolAddressLength());

                UInt16 arp_op = arp_hdr.GetARPOperation();
                Console.WriteLine("ARP/RARP Operation:\t\t{0}",
                                  WinPcap.WinPcap.ARP_REQUEST == arp_op ? "ARP Request" :
                                  WinPcap.WinPcap.ARP_REPLY == arp_op ? "ARP Reply" : arp_op.ToString());

                Console.WriteLine("Sender Hardware Address:\t{0}", arp_hdr.GetARPSenderMAC());
                Console.WriteLine("Sender Protocol Address:\t{0}", arp_hdr.GetARPSenderIP());
                Console.WriteLine("Target Hardware Address:\t{0}", arp_hdr.GetARPTargetMAC());
                Console.WriteLine("Target Protocol Address:\t{0}", arp_hdr.GetARPTargetIP());
                break;

            case WinPcap.WinPcap.ETHERNET_IPv6:
                break;

            case WinPcap.WinPcap.ETHERNET_PPPoE:
                break;

            default:
                Console.WriteLine("未知的以太网数据包:0x{0:X}", e_hdr.GetENType());
                break;
            }
        }