public static string ConvertPacketToTreeString(IEnumerable ps) { using (TextWriter writer = new StringWriter()) { int count = 0; foreach (object o in ps) { LogPacket p = o as LogPacket; DataFrame f = o as DataFrame; if (p != null) { writer.WriteLine(GetHeader(p)); WriteNodeToTreeString(writer, "", p.Frame.Root); } else if (f != null) { writer.WriteLine("Packet {0}", count); WriteNodeToTreeString(writer, "", f.Root); } count++; } return(writer.ToString()); } }
public static string ConvertPacketToString(IEnumerable ps) { using (TextWriter writer = new StringWriter()) { int count = 0; foreach (object o in ps) { LogPacket p = o as LogPacket; DataFrame f = o as DataFrame; if (p != null) { writer.Write(ConvertPacketToString(p)); } else if (f != null) { writer.WriteLine("Packet {0}", count); writer.Write(ConvertPacketToString(f)); } count++; } return(writer.ToString()); } }
public static string ConvertPacketToString(LogPacket p) { using (TextWriter writer = new StringWriter()) { writer.WriteLine(GetHeader(p)); writer.WriteLine(ConvertPacketToString(p.Frame)); return(writer.ToString()); } }
public static string ConvertPacketToTreeString(LogPacket p) { using (TextWriter writer = new StringWriter()) { writer.WriteLine(GetHeader(p)); WriteNodeToTreeString(writer, "", p.Frame.Root); return(writer.ToString()); } }
/// <summary> /// Add a packet to the buffer to write /// </summary> /// <param name="packet">The packet to write</param> public void AddPacket(LogPacket packet) { lock (_packets) { _packets[_currPos++] = packet; if (_currPos == MAX_PACKETS) { Flush(); } } }
/// <summary> /// Convert a packet to a hex string format /// </summary> /// <param name="p">The packet to convert</param> /// <returns>The converted string</returns> public static string ConvertBinaryPacketToString(LogPacket p) { using (TextWriter writer = new StringWriter()) { writer.WriteLine(GetHeader(p)); writer.WriteLine(GeneralUtils.BuildHexDump(16, p.Frame.ToArray())); writer.WriteLine(); return(writer.ToString()); } }
private static void ConvertPacketsToHtml(TextWriter textWriter, IEnumerable ps, bool forcebin) { XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.OmitXmlDeclaration = true; using (XmlWriter writer = XmlWriter.Create(textWriter, settings)) { int count = 0; writer.WriteStartElement("html"); writer.WriteStartElement("head"); writer.WriteElementString("title", "Packet Log"); writer.WriteEndElement(); writer.WriteStartElement("body"); foreach (object o in ps) { LogPacket p = o as LogPacket; DataFrame f = o as DataFrame; ColorValue c = ColorValue.White; if (p != null) { writer.WriteElementString("h2", String.Format("Time {0} - Tag '{1}' - Network '{2}'", p.Timestamp.ToString(), p.Tag, p.Network)); f = p.Frame; c = p.Color; } else { writer.WriteElementString("h2", String.Format("Packet {0}", count)); } count++; if (f.IsBasic || forcebin) { WriteBinaryPacketAsHtml(writer, f, c); } else if (f.IsDataString) { WriteTextPacketAsHtml(writer, f, c); } else { WriteTreePacketAsHtml(writer, f, c); } } writer.WriteEndElement(); writer.WriteEndElement(); } }
private static LogPacket ReadPacket(DateTime packetTime, Guid netId, byte[] data) { MemoryStream stm = new MemoryStream(data); DataReader reader = new DataReader(stm); LogPacket ret = null; try { EthernetPacket eth = ReadEthernetPacket(reader); if (eth.PacketType == EthernetPacket.ETHERNET_TYPE_IPv4) { IpPacket ip = ReadIpv4Packet(reader); if (ip.IpType == IpPacket.IP_TYPE_TCP) { TcpPacket tcp = ReadTcpPacket(reader, ip.DataLength); if (tcp.Data.Length > 0) { ret = new LogPacket("TCP", netId, Guid.NewGuid(), String.Format("{0}:{1} => {2}:{3}", ip.SrcAddress, tcp.SrcPort, ip.DestAddress, tcp.DestPort), new DataFrame(tcp.Data), new ColorValue(0xFF, 0xFF, 0xFF, 0xFF), packetTime); } } else if (ip.IpType == IpPacket.IP_TYPE_UDP) { UdpPacket udp = ReadUdpPacket(reader); ret = new LogPacket("UDP", netId, Guid.NewGuid(), String.Format("{0}:{1} => {2}:{3}", ip.SrcAddress, udp.SrcPort, ip.DestAddress, udp.DestPort), new DataFrame(udp.Data), new ColorValue(0xFF, 0xFF, 0xFF, 0xFF), packetTime); } else { // Do nothing } } } catch (EndOfStreamException) { ret = null; } catch (ArgumentException) { } return(ret); }
/// <summary> /// Reads the packets from file. /// </summary> /// <returns>The packets from file.</returns> /// <param name="filename">The filename</param> public static IEnumerable <LogPacket> ReadPacketsFromFile(string filename) { using (var stream = File.OpenRead(filename)) { List <LogPacket> packets = new List <LogPacket>(); try { while (true) { packets.Add(LogPacket.ReadFromStream(stream)); } } catch (EndOfStreamException) { } return(packets); } }
/// <summary> /// Flush the current buffer to the stream /// </summary> public void Flush() { lock (_packets) { if (_currPos > 0) { LogPacket[] arr = _packets; if (_currPos < MAX_PACKETS) { arr = new LogPacket[_currPos]; Array.Copy(_packets, 0, arr, 0, _currPos); } GeneralUtils.SerializeLogPackets(arr, _stm); Array.Clear(_packets, 0, _packets.Length); _currPos = 0; } } }
public static List <LogPacket> EnumerableToPackets(IEnumerable ps) { List <LogPacket> packets = new List <LogPacket>(); foreach (object o in ps) { LogPacket p = o as LogPacket; if (p == null) { DataFrame f = o as DataFrame; if (f == null) { continue; } p = new LogPacket("Unknown", Guid.Empty, "Unknown", f, ColorValue.White); } packets.Add(p); } return(packets); }
public static void SavePackets(string filename, IEnumerable ps) { List <LogPacket> packets = new List <LogPacket>(); foreach (object o in ps) { LogPacket p = o as LogPacket; if (p == null) { DataFrame f = o as DataFrame; if (f == null) { continue; } p = new LogPacket("Unknown", Guid.Empty, "Unknown", f, ColorValue.White); } packets.Add(p); } using (FileStream stm = File.Open(filename, FileMode.Create)) { GeneralUtils.SerializeLogPackets(packets.ToArray(), stm); } }
public static string ConvertPacketsToHtml(LogPacket p) { return(ConvertPacketsToHtml(new LogPacket[] { p })); }
/// <summary> /// Load a list of packets from a PCAP (really basic mode) /// </summary> /// <param name="fileName">The file to read from</param>l /// <param name="raw">Whether to import the raw data or parse for TCP/UDP data</param> /// <returns>The array of LogPackets</returns> public static LogPacket[] Load(string fileName, bool raw) { List <LogPacket> packets = new List <LogPacket>(); using (Stream stm = File.OpenRead(fileName)) { DataReader reader = new DataReader(stm); bool littleEndian = true; uint magic = reader.ReadUInt32(littleEndian); if (magic == 0xa1b2c3d4) { // OK } else if (magic == 0xd4c3b2a1) { littleEndian = false; } else { throw new ArgumentException(Resources.PcapReader_InvalidMagic); } reader.ReadUInt16(littleEndian); // Major reader.ReadUInt16(littleEndian); // Minor reader.ReadInt32(littleEndian); // Zone reader.ReadUInt32(littleEndian); // Sig figures reader.ReadUInt32(littleEndian); // Snap length uint netType = reader.ReadUInt32(littleEndian); if (!raw && netType != 1) { throw new ArgumentException(Resources.PcapReader_OnlyEthernet); } try { Guid netId = Guid.NewGuid(); while (reader.DataLeft > 0) { int secs = reader.ReadInt32(littleEndian); int usecs = reader.ReadInt32(littleEndian); DateTime captureTime = GeneralUtils.FromUnixTime(secs).AddMilliseconds(usecs / 10); int caplen = reader.ReadInt32(littleEndian); int origlen = reader.ReadInt32(littleEndian); byte[] data = reader.ReadBytes(caplen); if (raw) { packets.Add(new LogPacket("PCAP Raw", netId, Guid.NewGuid(), "Unknown", new DataFrame(data), new ColorValue(0xFF, 0xFF, 0xFF, 0xFF), captureTime)); } else { LogPacket p = ReadPacket(captureTime, netId, data); if (p != null) { packets.Add(p); } } } } catch (EndOfStreamException) { } } return(packets.ToArray()); }
/// <summary> /// Dump a text packet to stdout /// </summary> /// <param name="packet">The packet to dump</param> public static void DumpTextPacket(LogPacket packet) { Console.Out.WriteLine(ConvertTextPacketToString(packet)); }
private static string GetHeader(LogPacket p) { return(String.Format("Time {0} - Tag '{1}' - Network '{2}'", p.Timestamp.ToString(), p.Tag, p.Network)); }
public static string ConvertPacketToTreeString(LogPacket p) { return(ConvertPacketToTreeString(new LogPacket[] { p })); }