void ProcessCapturedPacket(PacketDotNet.Packet packet) { if (!(packet is PacketDotNet.EthernetPacket)) return; var eth = (PacketDotNet.EthernetPacket)packet; byte[] bytes = eth.Bytes; if (!Utility.IsPspPacket(bytes, Utility.HEADER_OFFSET)) { // PSPのパケットではないのでスルー return; } string src = eth.SourceHwAddress.ToString(); string dest = eth.DestinationHwAddress.ToString(); // サーバーから送られてきた他プレイヤーのPSPパケットか判別 if (RemoteMacAddresses.ContainsKey(src)) { //string log = String.Format("PSP Packet: サーバーから自PSPへ転送するパケットなのでスルーします: {0} -> {1}", src, desc); //AppendToLogTextBox(log, Color.DeepPink); return; } bool packetToRemotePSP = true; TraficStatistics myStats; lock (MyMacAddresses) { if (MyMacAddresses.ContainsKey(src)) { myStats = MyMacAddresses[src]; } else { myStats = new TraficStatistics(); MyMacAddresses[src] = myStats; } // 手元に送信先MACアドレスのPSPがあるか判別 if (MyMacAddresses.ContainsKey(dest)) { packetToRemotePSP = false; // not need tunneling //string log = String.Format("PSP Packet: Local trafic from {0} -> {1}", src, desc); //AppendToLogTextBox(log, Color.Purple); } } if (packetToRemotePSP && TunnelIsLinked) { TraficStatistics remoteStats; lock (RemoteMacAddresses) { if (RemoteMacAddresses.ContainsKey(dest)) { remoteStats = RemoteMacAddresses[dest]; } else { remoteStats = new TraficStatistics(); RemoteMacAddresses[dest] = remoteStats; } } //string log = String.Format("PSP Packet: {0} -> {1} Size: {2}", src, desc, bytes.Length); if (CurrentOperationMode == OperationMode.PlayRoomMaster || CurrentOperationMode == OperationMode.PlayRoomParticipant) { myStats.lastModified = System.DateTime.Now.Ticks; myStats.currentOutBytes += bytes.Length; myStats.totalOutBytes += bytes.Length; remoteStats.lastModified = System.DateTime.Now.Ticks; remoteStats.currentOutBytes += bytes.Length; remoteStats.totalOutBytes += bytes.Length; if (Utility.HEADER_OFFSET == 0) { //AppendToLogTextBox(System.DateTime.Now.Ticks.ToString(), Color.Black); ArenaTunnelClient.Send(bytes.ToArray()); } //else //{ // //long timestamp = Environment.TickCount; // long timestamp = System.DateTime.Now.Ticks; // AppendToLogTextBox(timestamp.ToString()); // byte[] ticks = BitConverter.GetBytes(timestamp); // ArenaTunnelClient.Send(ticks.Concat(bytes).ToArray()); //} } } }
private void listFieldEx(ref PacketDotNet.Packet targetPack) { //Get packet type name var packetTypeName = targetPack.GetType().Name; var packetFieldTypeName = packetTypeName.Substring(0, packetTypeName.Length - 6) + "Fields"; var packetFieldsType = Program.getPacketType("PacketDotNet." + packetFieldTypeName); //Get packet fields var fieldList = packetFieldsType.GetFields(); foreach (var i in fieldList) { var fieldTypeName = i.Name; if (fieldTypeName.EndsWith("Position")) { int foffset = (int)i.GetValue(null); var fieldName = fieldTypeName.Substring(0, fieldTypeName.Length - 8); offsetDict.Add(fieldName, offset + foffset); } else if (fieldTypeName.EndsWith("Length")) { int length = (int)i.GetValue(null); var fieldName = fieldTypeName.Substring(0, fieldTypeName.Length - 6); lengthDict.Add(fieldName, length); } } }
public Connection(PacketDotNet.TcpPacket packet) { m_srcIp = (packet.ParentPacket as PacketDotNet.IPv4Packet).SourceAddress.ToString(); m_dstIp = (packet.ParentPacket as PacketDotNet.IPv4Packet).DestinationAddress.ToString(); m_srcPort = (ushort)packet.SourcePort; m_dstPort = (ushort)packet.DestinationPort; }
public packView(int loffset , ref PacketDotNet.Packet targetPack) { pack = targetPack; Text = pack.GetType().Name; moffset = loffset; listField(ref pack); paintNodes(); }
private void AirservClient_onPacketArrival(PacketDotNet.Packet packet, DateTime ArrivalTime) { this.LiveCaptureFile.ProcessPacket(packet, ArrivalTime); captureInfo.AddCapturefile(this.LiveCaptureFile); this.LiveCaptureFile.Clear(); this.Invoke(new Invoky(() => RefreshAll())); }
public sPacket(ref PacketDotNet.Packet p) { timestamp = DateTime.Now; PacketDotNet.Packet c; for (c = p; c.PayloadPacket != null; c = c.PayloadPacket) ; finalType = c.GetType(); packet = p; }
public void showPacket(ref PacketDotNet.Packet p) { packViewer.Nodes.Clear(); HexView.ByteProvider = new HexViewer.DynamicByteProvider(p.Bytes); int offset = 0; for (var i = p; i != null; i = i.PayloadPacket) { packViewer.Nodes.Add(new packView(offset, ref i)); offset += i.Header.Length; } }
/// <summary> /// The main function of the class receives a tcp packet and reconstructs the stream /// </summary> /// <param name="tcpPacket"></param> public void ReassemblePacket(PacketDotNet.TcpPacket tcpPacket) { // if the paylod length is zero bail out //ulong length = (ulong)(tcpPacket.Bytes.Length - tcpPacket.TCPHeaderLength); //if (length == 0) return; if (tcpPacket.PayloadData == null || tcpPacket.PayloadData.Length == 0) return; reassemble_tcp((ulong)tcpPacket.SequenceNumber, (ulong)tcpPacket.PayloadData.Length, tcpPacket.PayloadData, (ulong)tcpPacket.PayloadData.Length, tcpPacket.Syn, (tcpPacket.ParentPacket as PacketDotNet.IPv4Packet).SourceAddress.Address, (tcpPacket.ParentPacket as PacketDotNet.IPv4Packet).DestinationAddress.Address, (uint)tcpPacket.SourcePort, (uint)tcpPacket.DestinationPort); }
public uint[] AddPacket(PacketDotNet.TcpPacket packet, string srcIp) { delay.Restart(); uint[] rel = new uint[2]; if (seq2Start == 0) seq2Start = packet.SequenceNumber; if (ip1 == srcIp) { packetsOut.Add(packet); rel[0] = packet.SequenceNumber - seq1Start; rel[1] = packet.AcknowledgmentNumber - seq2Start; } else { packetsIn.Add(packet); rel[0] = packet.SequenceNumber - seq2Start; rel[1] = packet.AcknowledgmentNumber - seq1Start; } return rel; }
/// <summary> /// Sends a raw packet throgh this device /// </summary> /// <param name="p">The packet to send</param> public virtual void SendPacket(PacketDotNet.Packet p) { SendPacket(p.Bytes); }
private static int LengthFromAddressFamily(PacketDotNet.LLDP.AddressFamily addressFamily) { if (addressFamily == PacketDotNet.LLDP.AddressFamily.IPv4) { return IPv4Fields.AddressLength; } if (addressFamily != PacketDotNet.LLDP.AddressFamily.IPv6) { throw new NotImplementedException("Unknown addressFamily of " + addressFamily); } return IPv6Fields.AddressLength; }
public override bool pass(ref PacketDotNet.Packet p) { if (p.PayloadPacket != null && (p.PayloadPacket is PacketDotNet.IpPacket)) if (isSource) { if ((p.PayloadPacket as PacketDotNet.IpPacket).SourceAddress.Equals(ip)) return true; } else { if ((p.PayloadPacket as PacketDotNet.IpPacket).DestinationAddress.Equals(ip)) return true; } return false; }
public override bool pass(ref PacketDotNet.Packet p) { if (p.PayloadPacket != null && p.PayloadPacket.PayloadPacket != null && p.PayloadPacket.PayloadPacket.GetType() == typeof(PacketDotNet.TcpPacket)) { var tcp = p.PayloadPacket.PayloadPacket as PacketDotNet.TcpPacket; if (isSource) return (tcp.SourcePort == port); else return (tcp.DestinationPort == port); } return false; }
public override bool pass(ref PacketDotNet.Packet p) { return !a.pass(ref p); }
public abstract bool pass(ref PacketDotNet.Packet p);
public override bool pass(ref PacketDotNet.Packet p) { if (p.PayloadPacket != null && p.PayloadPacket.PayloadPacket != null && p.PayloadPacket.PayloadPacket.GetType() == typeof(PacketDotNet.TcpPacket)) return true; return false; }
public void AddTcpPacket(PacketDotNet.TcpPacket packet) { Next_sequence_number = packet.SequenceNumber+packet.PayloadData.Length; Tcp_packets.Add(new TcpPacketWrapper(){ Packet = packet, Counter = counter++}); Packets_added = true; }
/// <summary> /// The main function of the class receives a tcp packet and reconstructs the stream /// </summary> /// <param name="tcpPacket"></param> public void ReassemblePacket(PacketDotNet.TcpPacket tcpPacket) { reassemble_tcp((ulong)tcpPacket.SequenceNumber, (ulong)tcpPacket.PayloadData.Length, tcpPacket.PayloadData, (ulong)tcpPacket.PayloadData.Length, tcpPacket.Syn, (tcpPacket.ParentPacket as PacketDotNet.IPv4Packet).SourceAddress.Address, (tcpPacket.ParentPacket as PacketDotNet.IPv4Packet).DestinationAddress.Address, (uint)tcpPacket.SourcePort, (uint)tcpPacket.DestinationPort); }
public SyslogMessage(PacketDotNet.UdpPacket udpSyslogPacket) { Regex msgRegex = new Regex(@" (\<(?<PRI>\d{1,3})\>){0,1} (?<HDR> (?<TIMESTAMP> (?<MMM>Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s (?<DD>[ 0-9][0-9])\s (?<HH>[0-9]{2})\:(?<MM>[0-9]{2})\:(?<SS>[0-9]{2}) )\s (?<HOSTNAME> [^ ]+? )\s ){0,1} (?<MSG>.*) ", RegexOptions.IgnorePatternWhitespace); string packet = System.Text.Encoding.ASCII.GetString(udpSyslogPacket.PayloadData); Match m = msgRegex.Match(packet); EndPoint ep = new IPEndPoint(IPAddress.Any, 0); // If the syslog message is invalid or empty, exit if (m != null && !string.IsNullOrEmpty(packet)) { System.Diagnostics.EventLog.WriteEntry("SwsyslogService", "No match." + Environment.NewLine + packet, System.Diagnostics.EventLogEntryType.Warning); return; } else { System.Diagnostics.EventLog.WriteEntry("SwsyslogService", "Match." + Environment.NewLine + packet, System.Diagnostics.EventLogEntryType.Information); } //parse PRI section into priority int pri; int? priority = int.TryParse(m.Groups["PRI"].Value, out pri) ? new int?(pri) : null; //parse the HEADER section - contains TIMESTAMP and HOSTNAME string hostname = null; Nullable<DateTime> timestamp = null; if (!string.IsNullOrEmpty(m.Groups["HDR"].Value)) { if (!string.IsNullOrEmpty(m.Groups["TIMESTAMP"].Value)) { try { timestamp = new DateTime( DateTime.Now.Year, MonthNumber(m.Groups["MMM"].Value), int.Parse(m.Groups["DD"].Value), int.Parse(m.Groups["HH"].Value), int.Parse(m.Groups["MM"].Value), int.Parse(m.Groups["SS"].Value) ); } catch (ArgumentException) { //ignore invalid timestamps } } if (!string.IsNullOrEmpty(m.Groups["HOSTNAME"].Value)) { hostname = m.Groups["HOSTNAME"].Value; } } if (!timestamp.HasValue) { //add timestamp as per RFC3164 timestamp = DateTime.Now; } if (string.IsNullOrEmpty(hostname)) { IPEndPoint ipe = (IPEndPoint)ep; IPHostEntry he = Dns.GetHostEntry(ipe.Address); if (he != null && !string.IsNullOrEmpty(he.HostName)) hostname = he.HostName; else hostname = ep.ToString(); } if (priority.HasValue) { this.facility = (int)Math.Floor((double)priority.Value / 8); this.severity = priority % 8; } else { this.facility = null; this.severity = null; } this.timestamp = timestamp.Value; this.hostname = hostname; this.message = m.Groups["MSG"].Value; }
/// <summary> /// Sends a raw packet throgh this device /// </summary> /// <param name="p">The packet to send</param> /// <param name="size">The number of bytes to send</param> public virtual void SendPacket(PacketDotNet.Packet p, int size) { SendPacket(p.Bytes, size); }
/// <summary> /// Constructor /// </summary> /// <param name="linkLayerType"> /// A <see cref="PacketDotNet.LinkLayers"/> /// </param> /// <param name="snapshotLength"> /// A <see cref="System.Nullable<System.Int32>"/> /// </param> /// <param name="captureFilename"> /// A <see cref="System.String"/> /// </param> /// <param name="mode"> /// A <see cref="FileMode"/> /// </param> public CaptureFileWriterDevice(PacketDotNet.LinkLayers linkLayerType, int? snapshotLength, string captureFilename, FileMode mode) { m_pcapFile = captureFilename; // append isn't possible without some difficulty and not implemented yet if(mode == FileMode.Append) { throw new System.InvalidOperationException("FileMode.Append is not supported, please contact the developers if you are interested in helping to implementing it"); } if(!snapshotLength.HasValue) { snapshotLength = Pcap.MAX_PACKET_SIZE; } else if(snapshotLength > Pcap.MAX_PACKET_SIZE) { throw new System.InvalidOperationException("snapshotLength > Pcap.MAX_PACKET_SIZE"); } // set the device handle PcapHandle = LibPcapSafeNativeMethods.pcap_open_dead((int)linkLayerType, snapshotLength.Value); m_pcapDumpHandle = LibPcapSafeNativeMethods.pcap_dump_open(PcapHandle, captureFilename); if(m_pcapDumpHandle == IntPtr.Zero) throw new PcapException("Error opening dump file '" + LastError + "'"); }
private void listField(ref PacketDotNet.EthernetPacket p) { addField("Dest Hw Addr", p.DestinationHwAddress, EthernetFields.DestinationMacPosition,EthernetFields.MacAddressLength); addField("Source Hw Addr", p.SourceHwAddress, EthernetFields.SourceMacPosition, EthernetFields.MacAddressLength); addField("Ethernet", "Type", p); }
private void listField(ref PacketDotNet.Packet targetPack) { if (targetPack is PacketDotNet.InternetLinkLayerPacket) this.BackColor = Color.FromArgb(0xffffff); else if (targetPack is PacketDotNet.InternetPacket) this.BackColor = Color.FromArgb(0xccffff); else if (targetPack is TransportPacket) this.BackColor = Color.FromArgb(0xccccff); switch (targetPack.GetType().Name) { case "EthernetPacket": EthernetPacket eth = targetPack as EthernetPacket; listField(ref eth); break; case "ARPPacket": ARPPacket arp = targetPack as ARPPacket; listField(ref arp); break; case "IPv4Packet": var ipv4 = targetPack as IPv4Packet; listField(ref ipv4); break; case "ICMPv4Packet": var ICMPv4 = targetPack as ICMPv4Packet; listField(ref ICMPv4); break; case "TcpPacket": var tcp = targetPack as TcpPacket; listField(ref tcp); break; case "UdpPacket": var udp = targetPack as UdpPacket; listField(ref udp); break; case "HLPacket": var hlp = targetPack as HLPacket; listField(ref hlp); Text = hlp.packetType; break; } if (targetPack.PayloadPacket == null && targetPack.PayloadData != null) { addField("Payload Data", targetPack.PayloadData, targetPack.Header.Length, targetPack.BytesHighPerformance.BytesLength-targetPack.Header.Length); } }
private void AnalysisPacket(PacketDotNet.Packet tempPacket) { if (tempPacket is PacketDotNet.EthernetPacket) { var ethernetPacket = tempPacket as PacketDotNet.EthernetPacket; Info tempRoot = new Info(); tempRoot.Detail = "Ethernet, Src: "; tempRoot.Detail += ethernetPacket.SourceHwAddress.ToString(); tempRoot.Detail += ", Dst: "; tempRoot.Detail += ethernetPacket.DestinationHwAddress.ToString(); tempRoot.Children.Add(new Info { Detail = ethernetPacket.SourceHwAddress.ToString(), Children = { new Info { Detail = ethernetPacket.SourceHwAddress.ToString(), }, } }); tempRoot.Children.Add(new Info { Detail = ethernetPacket.DestinationHwAddress.ToString(), Children = { new Info { Detail = ethernetPacket.DestinationHwAddress.ToString(), }, } }); tempRoot.Children.Add(new Info { Detail = ethernetPacket.Type.ToString(), }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); } else if (tempPacket is PacketDotNet.IpPacket) { Info tempRoot = new Info(); if (tempPacket is PacketDotNet.IPv4Packet) { tempRoot.Detail = "Internet Protocol Version 4, Src: "; } else { tempRoot.Detail = "Internet Protocol Version 6, Src: "; } var ipPacket = tempPacket as PacketDotNet.IpPacket; tempRoot.Detail += ipPacket.SourceAddress.ToString(); tempRoot.Detail += ", Dst: "; tempRoot.Detail += ipPacket.DestinationAddress.ToString(); tempRoot.Children.Add(new Info { Detail = "Version: " + ipPacket.Version.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Header Length: " + ipPacket.HeaderLength.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Total Length: " + ipPacket.TotalLength.ToString(), }); if (ipPacket.Protocol.ToString().CompareTo("TCP") == 0) { string flags = ipPacket.Header[6].ToString(); int flagvalue = 0; for (int index = 0; index < flags.Length; ++index) { flagvalue *= 10; flagvalue += (flags[index] - '0'); } string flagString; int reserverdbit = 1; string rdetail = ""; int dofragmentbit; string dofragmentDetail = ""; int morefragmentbit; string morefragmentDetail = ""; if ((flagvalue & 128) != 0) { reserverdbit = 0; rdetail = "Not set"; } else { reserverdbit = 1; rdetail = "Set"; } if ((flagvalue & 64) == 0) { dofragmentbit = 0; dofragmentDetail = "Not set"; flagString = "Don't frament"; } else { dofragmentbit = 1; dofragmentDetail = "Set"; flagString = "Frament"; } if ((flagvalue & 32) == 0) { morefragmentbit = 0; morefragmentDetail = "Not set"; } else { morefragmentbit = 1; morefragmentDetail = "Set"; } tempRoot.Children.Add(new Info { Detail = "Flags: 0x02 (" + flagString + ")", Children = { new Info { Detail =reserverdbit+ "... .... = Reserved bit: " + rdetail, }, new Info { Detail ="." +dofragmentbit+ ".. .... = Don't fragment: "+dofragmentDetail, }, new Info { Detail =".."+ morefragmentbit+ ". .... = More fragment: "+morefragmentDetail, }, } }); } else if (ipPacket.Protocol.ToString().CompareTo("UDP") == 0) { string flags = ipPacket.Header[6].ToString(); int flagvalue = 0; for (int index = 0; index < flags.Length; ++index) { flagvalue *= 10; flagvalue += (flags[index] - '0'); } int reserverdbit = 1; string rdetail = ""; int dofragmentbit; string dofragmentDetail = ""; int morefragmentbit; string morefragmentDetail = ""; if ((flagvalue & 128) != 0) { reserverdbit = 0; rdetail = "Not set"; } else { reserverdbit = 1; rdetail = "Set"; } if ((flagvalue & 64) == 0) { dofragmentbit = 0; dofragmentDetail = "Not set"; } else { dofragmentbit = 1; dofragmentDetail = "Set"; } if ((flagvalue & 32) == 0) { morefragmentbit = 0; morefragmentDetail = "Not set"; } else { morefragmentbit = 1; morefragmentDetail = "Set"; } tempRoot.Children.Add(new Info { Detail = "Flags: 0x00 ", Children = { new Info { Detail =reserverdbit+ "... .... = Reserved bit: " + rdetail, }, new Info { Detail ="." +dofragmentbit+ ".. .... = Don't fragment: "+dofragmentDetail, }, new Info { Detail =".."+ morefragmentbit+ ". .... = More fragment: "+morefragmentDetail, }, } }); } tempRoot.Children.Add(new Info { Detail = "Time To Live: " + ipPacket.TimeToLive.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Protocol: " + ipPacket.Protocol.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Source: " + ipPacket.SourceAddress.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Destination: " + ipPacket.DestinationAddress.ToString(), }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); } else if (tempPacket is PacketDotNet.ARPPacket) { var arpPacket = tempPacket as PacketDotNet.ARPPacket; Info tempRoot = new Info(); tempRoot.Detail = "Address Resolution Protocol (" + arpPacket.Operation.ToString() + ")"; tempRoot.Children.Add(new Info { Detail = "Hardware type: " + arpPacket.HardwareAddressType.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Protocol type: " + arpPacket.ProtocolAddressType.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Hardware Size: " + arpPacket.HardwareAddressLength.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Protocol size: " + arpPacket.ProtocolAddressLength.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Opcode: " + arpPacket.Operation.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Sender Mac address: " + arpPacket.SenderHardwareAddress.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Sender Ip address: " + arpPacket.SenderProtocolAddress.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Target Mac address: " + arpPacket.TargetHardwareAddress.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Target Ip address: " + arpPacket.TargetProtocolAddress.ToString(), }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); } else if (tempPacket is PacketDotNet.UdpPacket) { var udpPacket = tempPacket as PacketDotNet.UdpPacket; Info tempRoot = new Info(); tempRoot.Detail = "User Datagram Protocol, Src Port: " + udpPacket.SourcePort.ToString() + ", Dst Port: " + udpPacket.DestinationPort.ToString(); tempRoot.Children.Add(new Info { Detail = "Source Port: " + udpPacket.SourcePort.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Destination Port: " + udpPacket.DestinationPort.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Checksum: " + udpPacket.Checksum.ToString(), Children = { new Info { Detail = udpPacket.Checksum.ToString(), }, } }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); if ((udpPacket.DestinationPort.ToString().CompareTo("80") == 0) || (udpPacket.DestinationPort.ToString().CompareTo("8080") == 0) || (udpPacket.DestinationPort.ToString().CompareTo("1900") == 0)) { Info temp = new Info(); temp.Detail = "Hypertext Transfer Protocol"; try { string str = System.Text.ASCIIEncoding.ASCII.GetString(tempPacket.Bytes); temp.Children.Add(new Info { Detail = str, }); } catch { } InfoTreeViewControl ifvc1 = new InfoTreeViewControl(temp); this.TreeViewStack.Children.Add(ifvc1); } } else if (tempPacket is PacketDotNet.TcpPacket) { var tcpPacket = tempPacket as PacketDotNet.TcpPacket; Info tempRoot = new Info(); tempRoot.Detail = "Transmission Control Protocol, Src Port: " + tcpPacket.SourcePort.ToString() + ", Dst Port: " + tcpPacket.DestinationPort.ToString() + ", Seq: " + tcpPacket.SequenceNumber.ToString() + ", Ack: " + tcpPacket.Ack.ToString(); tempRoot.Children.Add(new Info { Detail = "Source Port: " + tcpPacket.SourcePort.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Destination Port: " + tcpPacket.DestinationPort.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Sequence number: " + tcpPacket.SequenceNumber.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Acknowledgement number: " + tcpPacket.AcknowledgmentNumber.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Header length: " + tcpPacket.Header.Length.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Flags: ", Children = { new Info { Detail = tcpPacket.AllFlags.ToString(), }, } }); tempRoot.Children.Add(new Info { Detail = "Window zise value: " + tcpPacket.WindowSize.ToString(), }); tempRoot.Children.Add(new Info { Detail = "Checksum: " + tcpPacket.Checksum.ToString(), Children = { new Info { Detail = tcpPacket.Checksum.ToString(), }, } }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); if ((tcpPacket.DestinationPort.ToString().CompareTo("80") == 0) || (tcpPacket.DestinationPort.ToString().CompareTo("8080") == 0) || (tcpPacket.DestinationPort.ToString().CompareTo("1900") == 0)) { Info temp = new Info(); temp.Detail = "Hypertext Transfer Protocol"; try { string str = System.Text.ASCIIEncoding.ASCII.GetString(tempPacket.Bytes); temp.Children.Add(new Info { Detail = str, }); } catch { } InfoTreeViewControl ifvc1 = new InfoTreeViewControl(temp); this.TreeViewStack.Children.Add(ifvc1); } } else if (tempPacket is PacketDotNet.ApplicationPacket) { var applicationPacket = tempPacket as PacketDotNet.ApplicationPacket; Info tempRoot = new Info(); tempRoot.Detail = "Data"; tempRoot.Children.Add(new Info { Detail = applicationPacket.ToString(), }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); } else if (tempPacket is PacketDotNet.ICMPv4Packet) { var iCMPv4Packet = tempPacket as PacketDotNet.ICMPv4Packet; Info tempRoot = new Info(); tempRoot.Detail = "Internet Control Message Protocol v4"; tempRoot.Children.Add(new Info { Detail = "Checksum: " + iCMPv4Packet.Checksum.ToString(), }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); } else if (tempPacket is PacketDotNet.ICMPv6Packet) { var iCMPv6Packet = tempPacket as PacketDotNet.ICMPv6Packet; Info tempRoot = new Info(); tempRoot.Detail = "Internet Control Message Protocol v6"; tempRoot.Children.Add(new Info { Detail = "Checksum: " + iCMPv6Packet.Checksum.ToString(), }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); } else if (tempPacket is PacketDotNet.Ieee80211RadioPacket) { var ieee80211RadioPacket = tempPacket as PacketDotNet.Ieee80211RadioPacket; Info tempRoot = new Info(); tempRoot.Detail = "Ieee80211RadioPacket"; tempRoot.Children.Add(new Info { //Detail = ieee80211RadioPacket.ToString(), }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); } else if (tempPacket is PacketDotNet.DataLinkPacket) { var dataLinkPacket = tempPacket as PacketDotNet.DataLinkPacket; Info tempRoot = new Info(); tempRoot.Detail = "DataLinkPacket"; tempRoot.Children.Add(new Info { // Detail = dataLinkPacket.ToString(), }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); } else if (tempPacket is PacketDotNet.Ieee80211MacFrame) { var ieee80211MacFrame = tempPacket as PacketDotNet.Ieee80211MacFrame; Info tempRoot = new Info(); tempRoot.Detail = "Ieee80211MacFrame"; tempRoot.Children.Add(new Info { // Detail = ieee80211MacFrame.ToString(), }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); } else if (tempPacket is PacketDotNet.IGMPv2Packet) { var iGMPv2Packet = tempPacket as PacketDotNet.IGMPv2Packet; Info tempRoot = new Info(); tempRoot.Detail = "IGMPv2Packet"; tempRoot.Children.Add(new Info { //Detail = iGMPv2Packet.ToString(), }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); } else if (tempPacket is PacketDotNet.InternetPacket) { var internetPacket = tempPacket as PacketDotNet.InternetPacket; Info tempRoot = new Info(); tempRoot.Detail = "InternetPacket"; tempRoot.Children.Add(new Info { //Detail = internetPacket.ToString(), }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); } else if (tempPacket is PacketDotNet.LLDPPacket) { var lLDPPacket = tempPacket as PacketDotNet.LLDPPacket; Info tempRoot = new Info(); tempRoot.Detail = "LLDPPacket"; tempRoot.Children.Add(new Info { //Detail = lLDPPacket.ToString(), }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); } else if (tempPacket is PacketDotNet.PPPPacket) { var pPPPacket = tempPacket as PacketDotNet.PPPPacket; Info tempRoot = new Info(); tempRoot.Detail = "PPPPacket"; tempRoot.Children.Add(new Info { // Detail = pPPPacket.ToString(), }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); } else if (tempPacket is PacketDotNet.SessionPacket) { var sessionPacket = tempPacket as PacketDotNet.SessionPacket; Info tempRoot = new Info(); tempRoot.Detail = "SessionPacket"; tempRoot.Children.Add(new Info { //Detail = sessionPacket.ToString(), }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); } else if (tempPacket is PacketDotNet.TransportPacket) { var transportPacket = tempPacket as PacketDotNet.TransportPacket; Info tempRoot = new Info(); tempRoot.Detail = "TransportPacket"; tempRoot.Children.Add(new Info { Detail = "TransportPacket", }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); } else if (tempPacket is PacketDotNet.WakeOnLanPacket) { var wakeOnLanPacket = tempPacket as PacketDotNet.WakeOnLanPacket; Info tempRoot = new Info(); tempRoot.Detail = "WakeOnLanPacket"; tempRoot.Children.Add(new Info { //Detail = "TransportPacket", }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); } else { Info tempRoot = new Info(); tempRoot.Detail = "UnknowPacketType"; tempRoot.Children.Add(new Info { //Detail = "TransportPacket", }); InfoTreeViewControl ifvc = new InfoTreeViewControl(tempRoot); this.TreeViewStack.Children.Add(ifvc); } }
private static void PcapPorcessContext(PacketDotNet.RawPacket pPacket) { var time = pPacket.Timeval.Date; var len = pPacket.Data.Length; var layer = pPacket.LinkLayerType; Console.WriteLine("{0}:{1}:{2},{3} Len={4} Layer={5}", time.Hour, time.Minute, time.Second, time.Millisecond, len, layer); var packet = PacketDotNet.Packet.ParsePacket(pPacket); //Raw基础包对象 if (layer == PacketDotNet.LinkLayers.Ethernet) //以太网包 { var ethernetPacket = (PacketDotNet.EthernetPacket)packet; System.Net.NetworkInformation.PhysicalAddress srcMac = ethernetPacket.SourceHwAddress; System.Net.NetworkInformation.PhysicalAddress destMac = ethernetPacket.DestinationHwAddress; Console.WriteLine("MAC:{0} -> {1}", srcMac, destMac); if (showDetails) Console.WriteLine("Ethernet packet: " + ethernetPacket.ToColoredString(false)); } var ipPacket = PacketDotNet.IpPacket.GetEncapsulated(packet); //IP包 if (ipPacket != null) { System.Net.IPAddress srcIp = ipPacket.SourceAddress; System.Net.IPAddress destIp = ipPacket.DestinationAddress; Console.WriteLine("IP: {0} -> {1}", srcIp, destIp); if (showDetails) Console.WriteLine("IP packet: " + ipPacket.ToColoredString(false)); var tcpPacket = PacketDotNet.TcpPacket.GetEncapsulated(packet); //TCP包 if (tcpPacket != null) { int srcPort = tcpPacket.SourcePort; int destPort = tcpPacket.DestinationPort; Console.WriteLine("TCP Port: {0} -> {1}", srcPort, destPort); if (showDetails) Console.WriteLine("TCP packet: " + tcpPacket.ToColoredString(false)); } var udpPacket = PacketDotNet.UdpPacket.GetEncapsulated(packet); //UDP包 if (udpPacket != null) { int srcPort = udpPacket.SourcePort; int destPort = udpPacket.DestinationPort; Console.WriteLine("UDP Port: {0} -> {1}", srcPort, destPort); if (showDetails) Console.WriteLine("UDP packet: " + udpPacket.ToColoredString(false)); } } }