static void localAreaNetworkNIC_OnPacketArrival(object sender, CaptureEventArgs e) { //DateTime time = e.Packet.Timeval.Date; //int len = e.Packet.Data.Length; //string content = ByteArrayToString(e.Packet.Data); //Console.WriteLine("{0}:{1}:{2},{3} Len={4}: {5}", // time.Hour, time.Minute, time.Second, time.Millisecond, len, content); var packet = Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data); if (e.Packet.LinkLayerType == LinkLayers.Ethernet) { EthernetPacket ethernetPacket = (EthernetPacket)packet; PhysicalAddress destMacAddress = ethernetPacket.DestinationHwAddress; PhysicalAddress srcMacAddress = ethernetPacket.SourceHwAddress; //Console.WriteLine("Mac: {0} -> {1}", srcMacAddress, destMacAddress); } IpPacket ipPacket = IpPacket.GetEncapsulated(packet); if (ipPacket != null) { IPAddress srcIpAddress = ipPacket.SourceAddress; IPAddress destIpAddress = ipPacket.DestinationAddress; //Console.WriteLine("IP: {0} -> {1}", srcIpAddress, destIpAddress); TcpPacket tcpPacket = TcpPacket.GetEncapsulated(packet); if (tcpPacket != null) { int srcPort = tcpPacket.SourcePort; int destPort = tcpPacket.DestinationPort; //Console.WriteLine("TCP Port: {0} -> {1}", srcPort, destPort); byte[] tcpBody = RipHeader(tcpPacket.Bytes, tcpPacket.Header); string packetString = ByteArrayToString(tcpBody); if (packetString.Contains("HTTP")) { Console.WriteLine("IP: {0} -> {1}", srcIpAddress, destIpAddress); Console.WriteLine("TCP Port: {0} -> {1}", srcPort, destPort); Console.WriteLine(ByteArrayToString(tcpBody)); } } //UdpPacket udpPacket = UdpPacket.GetEncapsulated(packet); //if (udpPacket != null) //{ // int srcPort = udpPacket.SourcePort; // int destPort = udpPacket.DestinationPort; // Console.WriteLine("UDP Port: {0} -> {1}", srcPort, destPort); //} } //string packetString = PacketString(packet); //Console.WriteLine(packetString); //Console.WriteLine(); }
private void packetArrival(object sender, CaptureEventArgs e) { Packet p = Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data); IpPacket ip = IpPacket.GetEncapsulated(p); TcpPacket tp = TcpPacket.GetEncapsulated(p); UdpPacket up = UdpPacket.GetEncapsulated(p); if (ip != null) { if (tp != null) { listView.Items.Add(new ListViewItem(new String[] { e.Packet.Timeval.Date.Year + "/" + e.Packet.Timeval.Date.Month + "/" + e.Packet.Timeval.Date.Day + " " + (e.Packet.Timeval.Date.Hour + 8) + ":" + e.Packet.Timeval.Date.Minute + ":" + e.Packet.Timeval.Date.Second + ":" + e.Packet.Timeval.Date.Millisecond, "TCP", ip.SourceAddress.ToString() + ":" + tp.SourcePort.ToString(), ip.DestinationAddress.ToString() + ":" + tp.DestinationPort.ToString(), e.Packet.Data.Length.ToString() })); datas.Add(BitConverter.ToString(e.Packet.Data)); } else if (up != null) { listView.Items.Add(new ListViewItem(new String[] { e.Packet.Timeval.Date.Year + "/" + e.Packet.Timeval.Date.Month + "/" + e.Packet.Timeval.Date.Day + " " + (e.Packet.Timeval.Date.Hour + 8) + ":" + e.Packet.Timeval.Date.Minute + ":" + e.Packet.Timeval.Date.Second + ":" + e.Packet.Timeval.Date.Millisecond, "UDP", ip.SourceAddress.ToString() + ":" + up.SourcePort.ToString(), ip.DestinationAddress.ToString() + ":" + up.DestinationPort.ToString(), e.Packet.Data.Length.ToString() })); datas.Add(BitConverter.ToString(e.Packet.Data)); } } totalPacketLength += e.Packet.Data.Length; totalPacketNum++; statusLabel.Text = "共收到" + totalPacketNum + "个数据包,总长度" + totalPacketLength; }