public PcapFile(string filename) { byte[] initData; this.filename = filename; content = new MemoryStream(); if (File.Exists(this.filename)) { initData = File.ReadAllBytes(filename); content.Write(initData, 0, initData.Length); } else { /* pcap file header */ PcapHeader fileHeader; fileHeader = new PcapHeader(1); initData = fileHeader.GetBytes(); content.Write(initData, 0, initData.Length); Save(); } }
static void Main(string[] args) { PcapHeader fileHeader; PcapItemHeader packetHeader; Ethernet packetEthernet; IPv4 packetIP; UDP packetUDP; UInt16 destPort; byte[] payloadFileHeader, payloadItemHeader, payloadEthernet, payloadIP, payloadUDP, payloadData; DateTime captureTime; byte[] payload; int index; string source, dest; if (args.Length < 2) { usage(); return; } if (args.Length > 2) { if (!UInt16.TryParse(args[2], out destPort)) { Console.WriteLine(String.Format("Error: The value {0} is not a valid port number!", args[2])); usage(); return; } } else { destPort = 0; } source = args[0]; dest = args[1]; if (!File.Exists(source)) { Console.WriteLine(String.Format("Error: File {0} does not exists!", source)); } if (File.Exists(dest)) { Console.WriteLine(String.Format("Error: File {0} already exists! Please delete it first.", dest)); } try { /* load payload */ payloadData = File.ReadAllBytes(source); captureTime = File.GetLastWriteTimeUtc(source); /* UDP */ packetUDP = new UDP(destPort, (UInt16)payloadData.Length); payloadUDP = packetUDP.GetBytes(); /* ipv4 */ packetIP = new IPv4((UInt16)(payloadUDP.Length + payloadData.Length)); payloadIP = packetIP.GetBytes(); /* ethernet */ packetEthernet = new Ethernet(1); payloadEthernet = packetEthernet.GetBytes(); /* pcap packet header */ packetHeader = new PcapItemHeader(captureTime, (UInt32)(payloadEthernet.Length + payloadIP.Length + payloadUDP.Length + payloadData.Length)); payloadItemHeader = packetHeader.GetBytes(); /* pcap file header */ fileHeader = new PcapHeader(1); payloadFileHeader = fileHeader.GetBytes(); payload = new byte[payloadFileHeader.Length + payloadItemHeader.Length + payloadEthernet.Length + payloadIP.Length + payloadUDP.Length + payloadData.Length]; index = 0; payloadFileHeader.CopyTo(payload, index); index += payloadFileHeader.Length; payloadItemHeader.CopyTo(payload, index); index += payloadItemHeader.Length; payloadEthernet.CopyTo(payload, index); index += payloadEthernet.Length; payloadIP.CopyTo(payload, index); index += payloadIP.Length; payloadUDP.CopyTo(payload, index); index += payloadUDP.Length; payloadData.CopyTo(payload, index); index += payloadData.Length; File.WriteAllBytes(dest, payload); } catch (Exception e) { Console.WriteLine(String.Format("Error: Unable to convert '{0}' to '{1}'!", source, dest)); Console.WriteLine(e.ToString()); return; } Console.WriteLine(String.Format("'{0}' -> '{1}'", source, dest)); }