Packet Sniffer SDK DLL Edition

HNUserFilter wrapper for C#

 Previous Next

File: HNUserFilter.cs

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Runtime.InteropServices;

namespace microOLAP.PSSDK
{
    /// <summary>
    /// Summary description for HNUserFilter.
    /// </summary>
    public partial class HNUserFilter : Component
    {
        #region DLL exports

        /// <summary>
        /// Creates HNUserFilter object.
        /// </summary>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfCreate")]
        private static extern IntPtr BpfCreate();

        /// <summary>
        /// Destroys HNUserFilter object.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfDestroy")]
        private static extern int BpfDestroy(IntPtr hFtr);

        /// <summary>
        /// Returns the total number of instructions in the user-settable packet filtering program.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfGetInstructionsCount")]
        private static extern int BpfGetInstructionsCount(IntPtr hFtr);

        /// <summary>
        /// Clears the user-settable packet filtering program.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfClear")]
        private static extern int BpfClear(IntPtr hFtr);

        /// <summary>
        /// Adds an instruction to the user-settable packet filtering program.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <param name="Code"></param>
        /// <param name="k"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfAddCmd")]
        private static extern int BpfAddCmd(IntPtr hFtr, short Code, int k);

        /// <summary>
        /// Adds JMP instruction to the user-settable packet filtering program.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <param name="Code"></param>
        /// <param name="k"></param>
        /// <param name="TrueOffset"></param>
        /// <param name="FalseOffset"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfAddJmp")]
        private static extern int BpfAddJmp(IntPtr hFtr, short Code, int k, short TrueOffset, short FalseOffset);

        /// <summary>
        /// Checks the BPF program code accuracy.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfCheckFilter")]
        private static extern int BpfCheckFilter(IntPtr hFtr);

        /// <summary>
        /// Returns the number of the first erroneous instruction in the user-settable packet filtering program.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfGetErrInstruction")]
        private static extern int BpfGetErrInstruction(IntPtr hFtr);

        /// <summary>
        /// Checks the packet by the user-settable packet filtering program.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <param name="pPacket"></param>
        /// <param name="PacketSize"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfCheckPacket")]
        private static extern int BpfCheckPacket(IntPtr hFtr, IntPtr pPacket, int PacketSize);

        /// <summary>
        /// Loads the BPF program from the file.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <param name="FileName"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfLoadFromFileW", CharSet = CharSet.Unicode)]
        private static extern int BpfLoadFromFile(IntPtr hFtr, string FileName);

        /// <summary>
        /// Saves the BPF program to the file.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <param name="FileName"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfSaveToFileW", CharSet = CharSet.Unicode)]
        private static extern int BpfSaveToFile(IntPtr hFtr, string FileName);

        /// <summary>
        /// Loads the BPF program from the memory block.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <param name="pProgramm"></param>
        /// <param name="Size"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfLoadFromMemory")]
        private static extern int BpfLoadFromMemory(IntPtr hFtr, byte[] pProgramm, int Size);

        /// <summary>
        /// Saves the BPF program to memory.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <param name="pProgramm"></param>
        /// <param name="Size"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfSaveToMemory")]
        private static extern int BpfSaveToMemory(IntPtr hFtr, byte[] pProgramm, int Size);

        /// <summary>
        /// Returns the size of the user-settable packet filtering program in bytes.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfGetProgrammSize")]
        private static extern int BpfGetProgrammSize(IntPtr hFtr);

        /// <summary>
        /// Returns the status flag of the FastBPF using.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfGetUseFastBPF")]
        private static extern bool BpfGetUseFastBPF(IntPtr hFtr);

        /// <summary>
        /// Turns on/off FastBPF.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <param name="bUseFastBpf"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfSetUseFastBPF")]
        private static extern bool BpfSetUseFastBPF(IntPtr hFtr, bool bUseFastBpf);

        /// <summary>
        /// Compiles BPF filter in a 32-bit optimized CPU code.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfCompileToFastBPF")]
        private static extern int BpfCompileToFastBPF(IntPtr hFtr);

        /// <summary>
        /// Compiles constants definitions from a file.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <param name="FileName"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfCompileBPFDefinesFromFileW", CharSet = CharSet.Unicode)]
        private static extern int BpfCompileBPFDefinesFromFile(IntPtr hFtr, string FileName);

        /// <summary>
        /// Compiles constants definitions from an ASCIIZ string.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <param name="Str"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfCompileBPFDefinesFromStr", CharSet = CharSet.Ansi)]
        private static extern int BpfCompileBPFDefinesFromStr(IntPtr hFtr, string Str);

        /// <summary>
        /// Compiles the BPF filtering program written in BPF assembler from a specified file.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <param name="FileName"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfCompileBPFAsmFromFileW", CharSet = CharSet.Unicode)]
        private static extern int BpfCompileBPFAsmFromFile(IntPtr hFtr, string FileName);

        /// <summary>
        /// Compiles the BPF filtering program written in BPF assembler from a string.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <param name="Str"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfCompileBPFAsmFromStr", CharSet = CharSet.Ansi)]
        private static extern int BpfCompileBPFAsmFromStr(IntPtr hFtr, string Str);

        /// <summary>
        /// Decompiles BPF program into BPF assembler source and saves it in a file.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <param name="FileName"></param>
        /// <param name="StrLabels"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfDecompileBPFAsmToFileW", CharSet = CharSet.Unicode)]
        private static extern int BpfDecompileBPFAsmToFile(IntPtr hFtr, string FileName, bool StrLabels);

        /// <summary>
        /// Decompiles BPF program into BPF assembler source and saves it in the memory allocated by application.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <param name="Str"></param>
        /// <param name="Size"></param>
        /// <param name="StrLabels"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfDecompileBPFAsmToStr", CharSet = CharSet.Ansi)]
        private static extern int BpfDecompileBPFAsmToStr(IntPtr hFtr, StringBuilder Str, ref int Size, bool StrLabels);

        /// <summary>
        /// Sets the OnBPFAsmError event.
        /// </summary>
        /// <param name="hFtr"></param>
        /// <param name="pfOnBPFAsmError"></param>
        /// <param name="Param"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "BpfSetOnBPFAsmError")]
        private static extern IntPtr BpfSetOnBPFAsmError(IntPtr hFtr, CallBackOnBPFAsmError pfOnBPFAsmError, IntPtr Param);

        #endregion

        #region HNUserFilter events

        /// <summary>
        /// CallBack OnBPFAsmError.
        /// </summary>
        /// <param name="Param"></param>
        /// <param name="ErrType"></param>
        /// <param name="Err"></param>
        /// <param name="Line"></param>
        /// <param name="Pos"></param>
        /// <returns></returns>
        private delegate bool CallBackOnBPFAsmError(IntPtr Param, int ErrType, int Err, int Line, int Pos);

        /// <summary>
        /// CallBack variable OnBPFAsmError.
        /// </summary>
        private CallBackOnBPFAsmError pfOnBPFAsmError;

        /// <summary>
        /// OnBPFAsmError event handler.
        /// </summary>
        public event OnBPFAsmErrorEventHandler OnBPFAsmError;

        #endregion

        /// <summary>
        /// Handle of the HNUserFilter object.
        /// </summary>
        private IntPtr hFtr = IntPtr.Zero;

        /// <summary>
        /// Creates HNUserFilter object.
        /// </summary>
        public HNUserFilter()
        {
            InitializeComponent();

            hFtr = BpfCreate();
            if (hFtr != IntPtr.Zero)
            {
                pfOnBPFAsmError = new CallBackOnBPFAsmError(this.DoOnBPFAsmError);
                BpfSetOnBPFAsmError(hFtr, pfOnBPFAsmError, IntPtr.Zero);
            }
        }


        /// <summary>
        /// Destroys HNUserFilter object.
        /// </summary>
        ~HNUserFilter()
        {
            if (hFtr != IntPtr.Zero) BpfDestroy(hFtr);
        }

        #region HNUserFilter properties

        /// <summary>
        /// Checks if the HNUserFilter object has been created correctly.
        /// </summary>
        [Browsable(false)]
        public bool IsValid
        {
            get
            {
                return (hFtr != IntPtr.Zero);
            }
        }

        /// <summary>
        /// Returns the user-settable packet filter handle.
        /// </summary>
        [Browsable(false)]
        public IntPtr Handle
        {
            get
            {
                return hFtr;
            }
        }

        /// <summary>
        /// Returns the total number of instructions in the user-settable packet filtering program.
        /// </summary>
        [Browsable(false)]
        public int InstructionsCount
        {
            get
            {
                return BpfGetInstructionsCount(hFtr);
            }
        }

        /// <summary>
        /// Returns the number of the first erroneous instruction in the user-settable packet filtering program.
        /// </summary>
        [Browsable(false)]
        public int ErrInstruction
        {
            get
            {
                return BpfGetErrInstruction(hFtr);
            }
        }

        /// <summary>
        /// Returns the size of the user-settable packet filtering program in bytes.
        /// </summary>
        [Browsable(false)]
        public int ProgrammSize
        {
            get
            {
                return BpfGetProgrammSize(hFtr);
            }
        }

        /// <summary>
        /// Turns on/off FastBPF.
        /// </summary>
        public bool UseFastUserFilter
        {
            get
            {
                return BpfGetUseFastBPF(hFtr);
            }
            set
            {
                BpfSetUseFastBPF(hFtr, value);
            }
        }

        #endregion

        #region HNUserFilter methods

        /// <summary>
        /// Clears the user-settable packet filtering program.
        /// </summary>
        /// <returns></returns>
        public PSSDKRES Clear()
        {
            return (PSSDKRES)BpfClear(hFtr);
        }

        /// <summary>
        /// Adds an instruction to the user-settable packet filtering program.
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="k"></param>
        /// <returns></returns>
        public PSSDKRES AddCmd(short Code, int k)
        {
            return (PSSDKRES)BpfAddCmd(hFtr, Code, k);
        }

        /// <summary>
        /// Adds JMP instruction to the user-settable packet filtering program.
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="k"></param>
        /// <param name="TrueOffset"></param>
        /// <param name="FalseOffset"></param>
        /// <returns></returns>
        public PSSDKRES AddJmp(short Code, int k, short TrueOffset, short FalseOffset)
        {
            return (PSSDKRES)BpfAddJmp(hFtr, Code, k, TrueOffset, FalseOffset);
        }

        /// <summary>
        /// Checks the BPF program code accuracy.
        /// </summary>
        /// <returns></returns>
        public PSSDKRES CheckFilter()
        {
            return (PSSDKRES)BpfCheckFilter(hFtr);
        }

        /// <summary>
        /// Checks the packet by the user-settable packet filtering program.
        /// </summary>
        /// <param name="pPacket"></param>
        /// <param name="PacketSize"></param>
        /// <returns></returns>
        public int CheckPacket(IntPtr pPacket, int PacketSize)
        {
            return BpfCheckPacket(hFtr, pPacket, PacketSize);
        }

        /// <summary>
        /// Loads the BPF program from the file.
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public PSSDKRES LoadFromFile(string FileName)
        {
            return (PSSDKRES)BpfLoadFromFile(hFtr, FileName);
        }

        /// <summary>
        /// Saves the BPF program to the file.
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public PSSDKRES SaveToFile(string FileName)
        {
            return (PSSDKRES)BpfSaveToFile(hFtr, FileName);
        }

        /// <summary>
        /// Loads the BPF program from the memory block.
        /// </summary>
        /// <param name="pProgramm"></param>
        /// <param name="Size"></param>
        /// <returns></returns>
        public PSSDKRES LoadFromMemory(byte[] pProgramm, int Size)
        {
            return (PSSDKRES)BpfLoadFromMemory(hFtr, pProgramm, Size);
        }

        /// <summary>
        /// Saves the BPF program to memory.
        /// </summary>
        /// <param name="pProgramm"></param>
        /// <param name="Size"></param>
        /// <returns></returns>
        public PSSDKRES SaveToMemory(byte[] pProgramm, int Size)
        {
            return (PSSDKRES)BpfSaveToMemory(hFtr, pProgramm, Size);
        }

        /// <summary>
        /// Compiles BPF filter in a 32-bit optimized CPU code.
        /// </summary>
        /// <returns></returns>
        public PSSDKRES CompileToFastBPF()
        {
            return (PSSDKRES)BpfCompileToFastBPF(hFtr);
        }

        /// <summary>
        /// Compiles constants definitions from a file.
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public PSSDKRES CompileBPFDefinesFromFile(string FileName)
        {
            return (PSSDKRES)BpfCompileBPFDefinesFromFile(hFtr, FileName);
        }

        /// <summary>
        /// Compiles constants definitions from an ASCIIZ string.
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public PSSDKRES CompileBPFDefinesFromStr(string Str)
        {
            return (PSSDKRES)BpfCompileBPFDefinesFromStr(hFtr, Str);
        }

        /// <summary>
        /// Compiles the BPF filtering program written in BPF assembler from a specified file.
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public PSSDKRES CompileBPFAsmFromFile(string FileName)
        {
            return (PSSDKRES)BpfCompileBPFAsmFromFile(hFtr, FileName);
        }

        /// <summary>
        /// Compiles the BPF filtering program written in BPF assembler from a string.
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public PSSDKRES CompileBPFAsmFromStr(string Str)
        {
            return (PSSDKRES)BpfCompileBPFAsmFromStr(hFtr, Str);
        }

        /// <summary>
        /// Decompiles BPF program into BPF assembler source and saves it in a file.
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="StrLabels"></param>
        /// <returns></returns>
        public PSSDKRES DecompileBPFAsmToFile(string FileName, bool StrLabels)
        {
            return (PSSDKRES)BpfDecompileBPFAsmToFile(hFtr, FileName, StrLabels);
        }

        /// <summary>
        /// Decompiles BPF program into BPF assembler source and saves it in the memory allocated by application.
        /// </summary>
        /// <param name="Str"></param>
        /// <param name="StrLabels"></param>
        /// <returns></returns>
        public PSSDKRES DecompileBPFAsmToStr(ref string Str, bool StrLabels)
        {
            int Size = 0;
            int Res = BpfDecompileBPFAsmToStr(hFtr, null, ref Size, StrLabels);
            if (Size != 0)
            {
                Size += 1;
                StringBuilder StrBuf = new StringBuilder(Size);
                Res = BpfDecompileBPFAsmToStr(hFtr, StrBuf, ref Size, StrLabels);
                Str = StrBuf.ToString();
            }
            return (PSSDKRES)Res;
        }

        #endregion

        #region HNUserFilter callbacks
        
        /// <summary>
        /// Tracks possible errors in BPF program during the compilation.
        /// </summary>
        /// <param name="Param"></param>
        /// <param name="ErrType"></param>
        /// <param name="Err"></param>
        /// <param name="Line"></param>
        /// <param name="Pos"></param>
        /// <returns></returns>
        private bool DoOnBPFAsmError(IntPtr Param, int ErrType, int Err, int Line, int Pos)
        {
            if (OnBPFAsmError != null)
                return OnBPFAsmError(this, (HNBpfErrorType)ErrType, (PSSDKRES)Err, Line, Pos);

            return false;
        }

        #endregion
    }
}
See also:HNUserFilter, All wrappers in one ZIP archive