/// <summary>
        /// Constructor
        /// </summary>
        /// <param name="linkLayerType">
        /// A <see cref="GodLesZ.Library.Network.Packet.LinkLayers"/>
        /// </param>
        /// <param name="snapshotLength">
        /// A <see cref="System.Nullable&lt;System.Int32&gt;"/>
        /// </param>
        /// <param name="captureFilename">
        /// A <see cref="System.String"/>
        /// </param>
        /// <param name="mode">
        /// A <see cref="FileMode"/>
        /// </param>
        public CaptureFileWriterDevice(GodLesZ.Library.Network.Packet.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 + "'");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns true if the filter expression was able to be compiled into a
        /// program without errors
        /// </summary>
        public static bool CheckFilter(string filterExpression,
                                       out string errorString)
        {
            IntPtr bpfProgram;
            IntPtr fakePcap = LibPcapSafeNativeMethods.pcap_open_dead((int)GodLesZ.Library.Network.Packet.LinkLayers.Ethernet, Pcap.MAX_PACKET_SIZE);

            uint mask = 0;

            if (!CompileFilter(fakePcap, filterExpression, mask, out bpfProgram, out errorString))
            {
                LibPcapSafeNativeMethods.pcap_close(fakePcap);
                return(false);
            }

            FreeBpfProgram(bpfProgram);

            LibPcapSafeNativeMethods.pcap_close(fakePcap);
            return(true);
        }