/// <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 + "'"); } }
/// <summary> /// Open the device /// </summary> public override void Open(DeviceConfiguration configuration) { // set the device handle var has_open_dead_with_tstamp_precision_support = Pcap.LibpcapVersion >= new Version(1, 5, 1); var resolution = configuration.TimestampResolution ?? TimestampResolution.Microsecond; if (has_open_dead_with_tstamp_precision_support) { PcapHandle = LibPcapSafeNativeMethods.pcap_open_dead_with_tstamp_precision((int)configuration.LinkLayerType, configuration.Snaplen, (uint)resolution); } else { if (resolution != TimestampResolution.Microsecond) { configuration.RaiseConfigurationFailed( nameof(configuration.TimestampResolution), (int)PcapError.PlatformNotSupported, "pcap version is < 1.5.1, needs pcap_open_dead_with_tstamp_precision()" ); } PcapHandle = LibPcapSafeNativeMethods.pcap_open_dead((int)configuration.LinkLayerType, configuration.Snaplen); } m_pcapDumpHandle = LibPcapSafeNativeMethods.pcap_dump_open(PcapHandle, m_pcapFile); if (m_pcapDumpHandle == IntPtr.Zero) { throw new PcapException("Error opening dump file '" + LastError + "'"); } Active = true; }