Packet Sniffer SDK DLL Edition

HNUserFilter wrapper for Delphi

 Previous Next

File: HNUserFilter.pas

{****************************************************************************
**                                                                         **
**                        PSSDK HNUserFilter module                        **
**            Copyright (c) 1997 - 2007 microOLAP Technologies LTD,        **
**                       Khalturin A.P., Naumov D.A.                       **
**                                                                         **
****************************************************************************}

unit HNUserFilter;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  HNPSManager, HNPsSdkDef;

type
  //---------------------------------------------------------------------------
  // Event handler OnBPFAsmError
  //---------------------------------------------------------------------------
  TOnBPFAsmError = function(Sender : TObject; ErrType : Integer; Err : DWORD; Line : DWORD; Pos : DWORD): BOOL of object;

  //---------------------------------------------------------------------------
  // Class THNUserFilter
  //---------------------------------------------------------------------------
  THNUserFilter = class
  private
    hFlt : Pointer;

    //---------------------------------------------------------------------------
    // Add in 2.2
    FOnBPFAsmError  : TOnBPFAsmError;
    procedure SetUseFastUserFilter(Value: BOOL);
    function GetUseFastUserFilter: BOOL;

  private
    function GetInstructionsCount: DWORD;
    function GetErrInstruction: DWORD;
    function GetProgrammSize: DWORD;

    //---------------------------------------------------------------------------
    // Add in 2.2
    function DoOnBPFAsmError(ErrType : Integer; Err : DWORD; Line : DWORD; Pos : DWORD): BOOL;

  protected
  public
    constructor Create();
    destructor Destroy(); override;
    function IsValid: BOOL;
    function Clear: DWORD;
    function AddCmd(Code: WORD; k: DWORD): DWORD;
    function AddJmp(Code: WORD; k: DWORD; TrueOffset: WORD; FalseOffset: WORD): DWORD;
    function CheckFilter: DWORD;
    function CheckPacket(pPacket: Pointer; PacketSize: DWORD): DWORD;
    function LoadFromFile(FileName: PChar): DWORD;
    function SaveToFile(FileName: PChar): DWORD;
    function LoadFromMemory(pProgramm: PHN_BPF_PROGRAMM; Size: DWORD): DWORD;
    function SaveToMemory(pProgramm: PHN_BPF_PROGRAMM; Size: DWORD): DWORD;

    //---------------------------------------------------------------------------
    // Add in 2.2
    function CompileToFastBPF         () : DWORD;
    function CompileBPFDefinesFromFile(FileName : PChar) : DWORD;
    function CompileBPFDefinesFromStr (Str : PChar) : DWORD;
    function CompileBPFAsmFromFile    (FileName : PChar) : DWORD;
    function CompileBPFAsmFromStr     (Str : PChar) : DWORD;
    function DecompileBPFAsmToFile    (FileName : PChar; StrLabels : BOOL) : DWORD;
    function DecompileBPFAsmToStr     (Str : PChar; var pSize : DWORD; StrLabels : BOOL) : DWORD;

  public
    property Handle: Pointer read hFlt;
    property InstructionsCount: DWORD read GetInstructionsCount;
    property ErrInstruction: DWORD read GetErrInstruction;
    property ProgrammSize: DWORD read GetProgrammSize;

  published
    //---------------------------------------------------------------------------
    // Add in 2.2
    property UseFastUserFilter: BOOL read GetUseFastUserFilter write SetUseFastUserFilter;
    property OnBPFAsmError: TOnBPFAsmError read FOnBPFAsmError write FOnBPFAsmError;
  end;

implementation

{$INCLUDE 'HNPsSdkFun.pas'}

//---------------------------------------------------------------------------
// IntOnBPFAsmError - OnBPFAsmError event handler.
//---------------------------------------------------------------------------
function IntOnBPFAsmError(Param : Pointer; ErrType : Integer; Err : DWORD; Line : DWORD; Pos : DWORD): BOOL; stdcall;
 var
   pFlt : THNUserFilter;
 begin
   pFlt := THNUserFilter(Param);
   Result := pFlt.DoOnBPFAsmError(ErrType,Err,Line,Pos);
 end;

//---------------------------------------------------------------------------
// DoOnBPFAsmError - Tracks possible errors in BPF program during the compilation.
//---------------------------------------------------------------------------
function THNUserFilter.DoOnBPFAsmError(ErrType : Integer; Err : DWORD; Line : DWORD; Pos : DWORD) : BOOL;
 begin
   Result := FALSE;
   if Assigned(FOnBPFAsmError) then Result := FOnBPFAsmError(Self,ErrType,Err,Line,Pos);
 end;

//---------------------------------------------------------------------------
// Create - Creates HNUserFilter object.
//---------------------------------------------------------------------------
constructor THNUserFilter.Create();
 begin
   hFlt := BpfCreate();
   BpfSetOnBPFAsmError(hFlt,@IntOnBPFAsmError,Self);
 end;

//---------------------------------------------------------------------------
// Destroy - Destroys HNUserFilter object.
//---------------------------------------------------------------------------
destructor THNUserFilter.Destroy();
 begin
   BpfSetOnBPFAsmError(hFlt,nil,nil);
   BpfDestroy(hFlt);
 end;

//---------------------------------------------------------------------------
// Method - IsValid - Checks if the HNUserFilter object has been created correctly.
//---------------------------------------------------------------------------
function THNUserFilter.IsValid: BOOL;
 begin
   Result := (hFlt <> nil);
 end;

//---------------------------------------------------------------------------
// Property - InstructionsCount - Returns the total number of instructions in the user-settable packet filtering program.
//---------------------------------------------------------------------------
function THNUserFilter.GetInstructionsCount: DWORD;
 begin
   Result := BpfGetInstructionsCount(hFlt);
 end;

//---------------------------------------------------------------------------
// Method - Clear - Clears the user-settable packet filtering program.
//---------------------------------------------------------------------------
function THNUserFilter.Clear: DWORD;
 begin
   Result := BpfClear(hFlt);
 end;

//---------------------------------------------------------------------------
// Method - AddCmd - Adds an instruction to the user-settable packet filtering program.
//---------------------------------------------------------------------------
function THNUserFilter.AddCmd(Code: WORD; k: DWORD): DWORD;
 begin
   Result := BpfAddCmd(hFlt,Code,k);
 end;

//---------------------------------------------------------------------------
// Method - AddJmp - Adds JMP instruction to the user-settable packet filtering program.
//---------------------------------------------------------------------------
function THNUserFilter.AddJmp(Code: WORD; k: DWORD; TrueOffset: WORD; FalseOffset: WORD): DWORD;
 begin
   Result := BpfAddJmp(hFlt,Code,k,TrueOffset,FalseOffset);
 end;

//---------------------------------------------------------------------------
// Method - CheckFilter - Checks the BPF program code accuracy.
//---------------------------------------------------------------------------
function THNUserFilter.CheckFilter: DWORD;
 begin
   Result := BpfCheckFilter(hFlt);
 end;

//---------------------------------------------------------------------------
// Property - ErrInstruction - Returns the number of the first erroneous instruction in the user-settable packet filtering program.
//---------------------------------------------------------------------------
function THNUserFilter.GetErrInstruction: DWORD;
 begin
   Result := BpfGetErrInstruction(hFlt);
 end;

//---------------------------------------------------------------------------
// Method - CheckPacket - Checks the packet by the user-settable packet filtering program.
//---------------------------------------------------------------------------
function THNUserFilter.CheckPacket(pPacket: Pointer; PacketSize: DWORD): DWORD;
 begin
   Result := BpfCheckPacket(hFlt,pPacket,PacketSize);
 end;

//---------------------------------------------------------------------------
// Method - LoadFromFile - Loads the BPF program from the file.
//---------------------------------------------------------------------------
function THNUserFilter.LoadFromFile(FileName: PChar): DWORD;
 begin
   Result := BpfLoadFromFile(hFlt,FileName);
 end;

//---------------------------------------------------------------------------
// Method - SaveToFile - Saves the BPF program to the file.
//---------------------------------------------------------------------------
function THNUserFilter.SaveToFile(FileName: PChar): DWORD;
 begin
   Result := BpfSaveToFile(hFlt,FileName);
 end;

//---------------------------------------------------------------------------
// Method - LoadFromMemory - Loads the BPF program from the memory block.
//---------------------------------------------------------------------------
function THNUserFilter.LoadFromMemory(pProgramm: PHN_BPF_PROGRAMM; Size: DWORD): DWORD;
 begin
   Result := BpfLoadFromMemory(hFlt,pProgramm,Size);
 end;

//---------------------------------------------------------------------------
// Method - SaveToMemory - Saves the BPF program to memory.
//---------------------------------------------------------------------------
function THNUserFilter.SaveToMemory(pProgramm: PHN_BPF_PROGRAMM; Size: DWORD): DWORD;
 begin
   Result := BpfSaveToMemory(hFlt,pProgramm,Size);
 end;

//---------------------------------------------------------------------------
// Property - ProgrammSize - Returns the size of the user-settable packet filtering program in bytes.
//---------------------------------------------------------------------------
function THNUserFilter.GetProgrammSize: DWORD;
 begin
   Result := BpfGetProgrammSize(hFlt);
 end;

//---------------------------------------------------------------------------
// v2.2 - Property - UseFastUserFilter - Turns on/off FastBPF.
//---------------------------------------------------------------------------
procedure THNUserFilter.SetUseFastUserFilter(Value: BOOL);
 begin
    BpfSetUseFastBPF(hFlt,Value);
 end;

//---------------------------------------------------------------------------
// v2.2 - Property - UseFastUserFilter - Returns the status flag of the FastBPF using.
//---------------------------------------------------------------------------
function THNUserFilter.GetUseFastUserFilter: BOOL;
 begin
   Result := BpfGetUseFastBPF(hFlt);
 end;

//---------------------------------------------------------------------------
// v2.2 - Method - CompileToFastBPF - Compiles BPF filter in a 32-bit optimized CPU code.
//---------------------------------------------------------------------------
function THNUserFilter.CompileToFastBPF() : DWORD;
 begin
   Result := BpfCompileToFastBPF(hFlt);
 end;

//---------------------------------------------------------------------------
// v2.2 - Method - CompileBPFDefinesFromFile - Compiles constants definitions from a file.
//---------------------------------------------------------------------------
function THNUserFilter.CompileBPFDefinesFromFile(FileName : PChar) : DWORD;
 begin
   Result := BpfCompileBPFDefinesFromFile(hFlt,FileName);
 end;

//---------------------------------------------------------------------------
// v2.2 - Method - CompileBPFDefinesFromStr - Compiles constants definitions from an ASCIIZ string.
//---------------------------------------------------------------------------
function THNUserFilter.CompileBPFDefinesFromStr(Str : PChar) : DWORD;
 begin
   Result := BpfCompileBPFDefinesFromStr(hFlt,Str);
 end;

//---------------------------------------------------------------------------
// v2.2 - Method - CompileBPFAsmFromFile - Compiles the BPF filtering program written in BPF assembler from a specified file.
//---------------------------------------------------------------------------
function THNUserFilter.CompileBPFAsmFromFile(FileName : PChar) : DWORD;
 begin
   Result := BpfCompileBPFAsmFromFile(hFlt,FileName);
 end;

//---------------------------------------------------------------------------
// v2.2 - Method - CompileBPFAsmFromStr - Compiles the BPF filtering program written in BPF assembler from a string.
//---------------------------------------------------------------------------
function THNUserFilter.CompileBPFAsmFromStr(Str : PChar) : DWORD;
 begin
   Result := BpfCompileBPFAsmFromStr(hFlt,Str);
 end;

//---------------------------------------------------------------------------
// v2.2 - Method - DecompileBPFAsmToFile - Decompiles BPF program into BPF assembler source and saves it in a file.
//---------------------------------------------------------------------------
function THNUserFilter.DecompileBPFAsmToFile(FileName : PChar; StrLabels : BOOL) : DWORD;
 begin
   Result := BpfDecompileBPFAsmToFile(hFlt,FileName,StrLabels);
 end;

//---------------------------------------------------------------------------
// v2.2 - Method - DecompileBPFAsmToStr - Decompiles BPF program into BPF assembler source and saves it in the memory allocated by application.
//---------------------------------------------------------------------------
function THNUserFilter.DecompileBPFAsmToStr(Str : PChar; var pSize : DWORD; StrLabels : BOOL) : DWORD;
 begin
   Result := BpfDecompileBPFAsmToStr(hFlt,Str,pSize,StrLabels);
 end;

end.
See also:HNUserFilter, All wrappers in one ZIP archive