/// <summary> A Tcpdump packet has the following format: /// seconds [4 bytes] /// microseconds [4 bytes] /// captured length [4 bytes] /// original packet length [4 bytes] /// packet data [variable length] /// </summary> public static void appendPacket(System.String filename, RawPacket rawPacket, int endian) { byte[][] packetArray = new byte[5][]; byte[] data = rawPacket.Data; if (endian == LITTLE_ENDIAN) { packetArray[0] = ArrayHelper.toBytesLittleEndian(rawPacket.Timeval.Seconds, 4); packetArray[1] = ArrayHelper.toBytesLittleEndian((long) rawPacket.Timeval.MicroSeconds, 4); packetArray[2] = ArrayHelper.toBytesLittleEndian((long) data.Length, 4); packetArray[3] = ArrayHelper.toBytesLittleEndian((long) data.Length + rawPacket.Droplen, 4); } else { packetArray[0] = ArrayHelper.toBytes(rawPacket.Timeval.Seconds, 4); packetArray[1] = ArrayHelper.toBytes((long) rawPacket.Timeval.MicroSeconds, 4); packetArray[2] = ArrayHelper.toBytes((long) data.Length, 4); packetArray[3] = ArrayHelper.toBytes((long) data.Length + rawPacket.Droplen, 4); } packetArray[4] = data; FileUtility.writeFile(packetArray, filename, true); }
/// <summary> A Tcpdump packet has the following format: /// seconds [4 bytes] /// microseconds [4 bytes] /// captured length [4 bytes] /// original packet length [4 bytes] /// packet data [variable length] /// </summary> public static void appendPacket(System.String filename, RawPacket rawPacket, int endian) { byte[][] packetArray = new byte[5][]; byte[] data = rawPacket.Data; if (endian == LITTLE_ENDIAN) { packetArray[0] = ArrayHelper.toBytesLittleEndian(rawPacket.Timeval.Seconds, 4); packetArray[1] = ArrayHelper.toBytesLittleEndian((long)rawPacket.Timeval.MicroSeconds, 4); packetArray[2] = ArrayHelper.toBytesLittleEndian((long)data.Length, 4); packetArray[3] = ArrayHelper.toBytesLittleEndian((long)data.Length + rawPacket.Droplen, 4); } else { packetArray[0] = ArrayHelper.toBytes(rawPacket.Timeval.Seconds, 4); packetArray[1] = ArrayHelper.toBytes((long)rawPacket.Timeval.MicroSeconds, 4); packetArray[2] = ArrayHelper.toBytes((long)data.Length, 4); packetArray[3] = ArrayHelper.toBytes((long)data.Length + rawPacket.Droplen, 4); } packetArray[4] = data; FileUtility.writeFile(packetArray, filename, true); }
public static bool TryGetPacket(out Packets.AbstractPacket packet, System.Type packetType, Frame parentFrame, int startIndex, int endIndex) { packet = null; try { if (packetType.Equals(typeof(Packets.Ethernet2Packet))) { packet = new Packets.Ethernet2Packet(parentFrame, startIndex, endIndex); } else if (packetType.Equals(typeof(Packets.IPv4Packet))) { IPv4Packet.TryParse(parentFrame, startIndex, endIndex, out packet); //packet = new Packets.IPv4Packet(parentFrame, startIndex, endIndex); } else if (packetType.Equals(typeof(Packets.IPv6Packet))) { packet = new Packets.IPv6Packet(parentFrame, startIndex, endIndex); } else if (packetType.Equals(typeof(Packets.TcpPacket))) { packet = new Packets.TcpPacket(parentFrame, startIndex, endIndex); } else if (packetType.Equals(typeof(Packets.IEEE_802_11Packet))) { packet = new Packets.IEEE_802_11Packet(parentFrame, startIndex, endIndex); } else if (packetType.Equals(typeof(Packets.IEEE_802_11RadiotapPacket))) { packet = new Packets.IEEE_802_11RadiotapPacket(parentFrame, startIndex, endIndex); } else if (packetType.Equals(typeof(Packets.CiscoHdlcPacket))) { packet = new Packets.CiscoHdlcPacket(parentFrame, startIndex, endIndex); } else if (packetType.Equals(typeof(Packets.LinuxCookedCapture))) { packet = new Packets.LinuxCookedCapture(parentFrame, startIndex, endIndex); } else if (packetType.Equals(typeof(Packets.PrismCaptureHeaderPacket))) { packet = new Packets.PrismCaptureHeaderPacket(parentFrame, startIndex, endIndex); } else if (packetType.Equals(typeof(Packets.PpiPacket))) { packet = new Packets.PpiPacket(parentFrame, startIndex, endIndex); } else if (packetType.Equals(typeof(Packets.PointToPointPacket))) { packet = new Packets.PointToPointPacket(parentFrame, startIndex, endIndex); } else if (packetType.Equals(typeof(Packets.NullLoopbackPacket))) { packet = new Packets.NullLoopbackPacket(parentFrame, startIndex, endIndex); } else if (packetType.Equals(typeof(Packets.ErfFrame))) { packet = new Packets.ErfFrame(parentFrame, startIndex, endIndex); } if (packet == null) { return(false); } else { return(true); } } catch (Exception) { packet = new Packets.RawPacket(parentFrame, startIndex, endIndex); return(false); } }