Home | Chi sono | Contattami
 

Progr. lineare

Delphi
 
Componenti
  Database
 
Miei articoli

Windows

Miei articoli 

 

Unit Pascal di traduzione dell' header fltdefs.h: api per il packet filtering

 

La dll iphlpapi.dll contiene, tra le altre, le funzioni per il packet filtering che consentono di effettuare dei filtri sui pacchetti (TCP UDP ICMP) in entrata ed in uscita sulle singole interfacce di rete (come le regole IPSEC in pratica). La dichiarazione di tali api Ŕ situata nell' header fltdefs.h. Non avendo notato traduzioni in Pascal di tale header ho deciso di farmene una personalmente. Per l' elenco delle funzioni esportate da una dll ho usato come al solito "Dependency Walker". Le funzioni esportate relative al Packet Filtering sono le seguenti

_PfAddFiltersToInterface@24
_PfAddGlobalFilterToInterface@8
_PfBindInterfaceToIndex@16
_PfBindInterfaceToIPAddress@12
_PfCreateInterface@24
_PfDeleteInterface@4
_PfDeleteLog@0
_PfGetInterfaceStatistics@16
_PfMakeLog@4
_PfRebindFilters@8
_PfRemoveFilterHandles@12
_PfRemoveFiltersFromInterface@20
_PfRemoveGlobalFilterFromInterface@8
_PfSetLogBuffer@28
_PfTestPacket@20
_PfUnBindInterface@4

Per comoditÓ gli faremo corrispondere i nomi usati nell' MSDN ed in tutti gli esempi che si possono trovare in rete ossia

PfAddFiltersToInterface;
PfAddGlobalFilterToInterface;
PfBindInterfaceToIPAddress;
PfBindInterfaceToIndex;
PfCreateInterface;
PfDeleteInterface;
PfDeleteLog;
PfGetInterfaceStatistics;
PfMakeLog;
PfRebindFilters;
PfRemoveFilterHandles
PfRemoveFiltersFromInterface;
PfRemoveGlobalFilterFromInterface;
PfSetLogBuffer;
PfTestPacket;
PfUnBindInterface;

Di seguito la unit:

{******************************************************************}
{                                                                  }
{ Borland Delphi Runtime Library                                   }
{ Definitions for the WIN32 filter APIs                            }
{                                                                  }
{ Portions created by Microsoft are                                }
{ Copyright (C) 1995-1999 Microsoft Corporation.                   }
{ All Rights Reserved.                                             }
{                                                                  }
{ The original file is: fltdefs.h, released 24 Sept 1997.          }
{ The original Pascal code is: FLTDEFS.PAS, released 19 Oct 2004 . }
{ The initial developer of the Pascal code is Carlo Pasolini       }
{ (email: cdpasop@virgilio.it                                      }
{ (internet: http://utenti.lycos.it/carlpasolini  ).               }
{                                                                  }
{ Portions created by Carlo Pasolini are                           }
{ Copyright (C) 2004 Carlo Pasolini                                }
{                                                                  }
{ The contents of this file are used with permission, subject to   }
{ the Mozilla Public License Version 1.1 (the "License"); you may  }
{ not use this file except in compliance with the License. You may }
{ obtain a copy of the License at                                  }
{ http://www.mozilla.org/MPL/MPL-1.1.html                          }
{                                                                  }
{ Software distributed under the License is distributed on an      }
{ "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or   }
{ implied. See the License for the specific language governing     }
{ rights and limitations under the License.                        }
{                                                                  }
{******************************************************************}
unit FLTDEFS;

interface
uses
  Windows, JwaWinType;

type
  PVOID = Pointer;
  PPVOID = ^PVOID;
  PVOID64 = Pointer;
  PBYTE = ^BYTE;

type
  FILTER_HANDLE = PVOID;
  PFILTER_HANDLE = ^PVOID;
  INTERFACE_HANDLE = PVOID;
  PINTERFACE_HANDLE = ^PVOID;

//Packet Filtering Enumerated Types
type
  _GlobalFilter = (
    GF_FRAGMENTS = 2,
    GF_STRONGHOST = 8,
    GF_FRAGCACHE = 9 );
  {$EXTERNALSYM _GlobalFilter}
  GLOBAL_FILTER = _GlobalFilter;
  {$EXTERNALSYM GLOBAL_FILTER}
  PGLOBAL_FILTER = ^_GlobalFilter;
  {$EXTERNALSYM PGLOBAL_FILTER}
  TGlobal_Filter = GLOBAL_FILTER;
  PTGlobal_Filter = PGLOBAL_FILTER;

type
  _PfForwardAction = (
    PF_ACTION_FORWARD = 0,
    PF_ACTION_DROP  );
  {$EXTERNALSYM _PfForwardAction}
  PFFORWARD_ACTION = _PfForwardAction;
  {$EXTERNALSYM PFFORWARD_ACTION}
  PPFFORWARD_ACTION = ^_PfForwardAction;
  {$EXTERNALSYM PPFFORWARD_ACTION}
  TPfForward_Action = PFFORWARD_ACTION;
  PTPfForward_Action = PPFFORWARD_ACTION;

type
  _PfAddresType = (
    PF_IPV4,
    PF_IPV6  );
  {$EXTERNALSYM _PfAddresType}
  PFADDRESSTYPE = _PfAddresType;
  {$EXTERNALSYM PFADDRESSTYPE}
  PPFADDRESSTYPE = ^_PfAddresType;
  {$EXTERNALSYM PPFADDRESSTYPE}
  TPfAddressType = PFADDRESSTYPE;
  PTPfAddressType = PPFADDRESSTYPE;

type
  _PfFrameType = (
    PFFT_FILTER = 1,
    PFFT_FRAG = 2,
    PFFT_SPOOF = 3);
  {$EXTERNALSYM _PfFrameType}
  PFFRAMETYPE = _PfFrameType;
  {$EXTERNALSYM PFFRAMETYPE}
  PPFFRAMETYPE = ^_PfFrameType;
  {$EXTERNALSYM PPFFRAMETYPE}
  TPfFrameType = PFFRAMETYPE;
  PTPfFrameType = PPFFRAMETYPE;


//Packet Filtering Structures
type
  _PF_FILTER_DESCRIPTOR = record
    dwFilterFlags: DWORD;
    dwRule: DWORD;
    pfatType: PFADDRESSTYPE;
    SrcAddr: PBYTE;
    SrcMask: PBYTE;
    DstAddr: PBYTE;
    DstMask: PBYTE;
    dwProtocol: DWORD;
    fLateBound: DWORD;
    wSrcPort: WORD;
    wDstPort: WORD;
    wSrcPortHighRange: WORD;
    wDstPortHighRange: WORD;
  end;
  {$EXTERNALSYM _PF_FILTER_DESCRIPTOR}
  PF_FILTER_DESCRIPTOR = _PF_FILTER_DESCRIPTOR;
  {$EXTERNALSYM PF_FILTER_DESCRIPTOR}
  PPF_FILTER_DESCRIPTOR = ^_PF_FILTER_DESCRIPTOR;
  {$EXTERNALSYM PPF_FILTER_DESCRIPTOR}
  TPf_Filter_Descriptor = PF_FILTER_DESCRIPTOR;
  PTPf_Filter_Descriptor = PPF_FILTER_DESCRIPTOR;

type
  _PF_FILTER_STATS = record
    dwNumPacketsFiltered: DWORD;
    info: PF_FILTER_DESCRIPTOR;
  end;
  {$EXTERNALSYM _PF_FILTER_STATS}
  PF_FILTER_STATS = _PF_FILTER_STATS;
  {$EXTERNALSYM PF_FILTER_STATS}
  PPF_FILTER_STATS = ^_PF_FILTER_STATS;
  {$EXTERNALSYM PPF_FILTER_STATS}
  TPf_Filter_Stats = PF_FILTER_STATS;
  PTPf_Filter_Stats = PPF_FILTER_STATS;

type
  _PF_INTERFACE_STATS = record
    pvDriverContext: PVOID;
    dwFlags: DWORD;
    dwInDrops: DWORD;
    dwOutDrops: DWORD;
    eaInAction: PFFORWARD_ACTION;
    eaOutAction: PFFORWARD_ACTION;
    dwNumInFilters: DWORD;
    dwNumOutFilters: DWORD;
    dwFrag: DWORD;
    dwSpoof: DWORD;
    dwReserved1: DWORD;
    dwReserved2: DWORD;
    liSYN: DWORD;
    liTotalLogged: DWORD;
    dwLostLogEntries: DWORD;
    FilterInfo: array[0..0] of PF_FILTER_STATS;
  end;
  {$EXTERNALSYM _PF_INTERFACE_STATS}
  PF_INTERFACE_STATS = _PF_INTERFACE_STATS;
  {$EXTERNALSYM PF_INTERFACE_STATS}
  PPF_INTERFACE_STATS = ^_PF_INTERFACE_STATS;
  {$EXTERNALSYM PPF_INTERFACE_STATS}
  TPf_Interface_Stats = PF_INTERFACE_STATS;
  PTPf_Interface_Stats = PPF_INTERFACE_STATS;


type
  _PF_LATEBIND_INFO = record
    SrcAddr: PBYTE;
    DstAddr: PBYTE;
    Mask: PBYTE;
  end;
  {EXTERNALSYM _PF_LATEBIND_INFO}
  PF_LATEBIND_INFO = _PF_LATEBIND_INFO;
  {EXTERNALSYM PF_LATEBIND_INFO}
  PPF_LATEBIND_INFO = ^_PF_LATEBIND_INFO;
  {EXTERNALSYM PPF_LATEBIND_INFO}
  TPf_Latebind_Info = PF_LATEBIND_INFO;
  PTPf_Latebind_Info = PPF_LATEBIND_INFO;

type
  _pfLogFrame = record
    Timestamp: DWORD;
    pfeTypeOfFrame: PFFRAMETYPE;
    dwTotalSizeUsed: DWORD;
    dwFilterRule: DWORD;
    wSizeOfAdditionalData: WORD;
    wSizeOfIpHeader: WORD;
    dwInterfaceName: DWORD;
    dwIPIndex: DWORD;
    bPacketData: array[0..0] of BYTE;
  end;
  {EXTERNALSYM _pfLogFrame}
  PFLOGFRAME = _pfLogFrame;
  {EXTERNALSYM PFLOGFRAME}
  PPFLOGFRAME = ^_pfLogFrame;
  {EXTERNALSYM PPFLOGFRAME}
  TPfLogFrame = PFLOGFRAME;
  PTPfLogFrame = PPFLOGFRAME;

{/////////////////////////////////////////////////////////////////////////////// }
{/// // }
{/// The constants that should be used to set up the FILTER_INFO_STRUCTURE // }
{/// // }
{/////////////////////////////////////////////////////////////////////////////// }

const
  FILTER_PROTO_ANY = $00000000;
const
  FILTER_PROTO_ICMP = $00000001;
const
  FILTER_PROTO_TCP = $00000006;
const
  FILTER_PROTO_UDP = $00000011;

const
  FILTER_TCPUDP_PORT_ANY = $0000;

const
  FILTER_ICMP_TYPE_ANY = $ff;
const
  FILTER_ICMP_CODE_ANY = $ff;

const
  FD_FLAGS_NOSYN = $1;

const
  FD_FLAGS_ALLFLAGS = FD_FLAGS_NOSYN;

const
  LB_SRC_ADDR_USE_SRCADDR_FLAG = $00000001;
const
  LB_SRC_ADDR_USE_DSTADDR_FLAG = $00000002;
const
  LB_DST_ADDR_USE_SRCADDR_FLAG = $00000004;
const
  LB_DST_ADDR_USE_DSTADDR_FLAG = $00000008;
const
  LB_SRC_MASK_LATE_FLAG = $00000010;
const
  LB_DST_MASK_LATE_FLAG = $00000020;

const
  ERROR_BASE = 23000;

const
  PFERROR_NO_PF_INTERFACE = (ERROR_BASE + 0); {// never returned.}
const
  PFERROR_NO_FILTERS_GIVEN = (ERROR_BASE + 1);
const
  PFERROR_BUFFER_TOO_SMALL = (ERROR_BASE + 2);
const
  ERROR_IPV6_NOT_IMPLEMENTED = (ERROR_BASE + 3);

//Packet Filtering Functions
function PfAddFiltersToInterface(
                                ih: INTERFACE_HANDLE;
                                cInFilters: DWORD;
                                pfiltIn: PTPf_Filter_Descriptor;
                                cOutFilters: DWORD;
                                pfiltOut: PTPf_Filter_Descriptor;
                                pfHandle: PFILTER_HANDLE): DWORD; stdcall;

function PfAddGlobalFilterToInterface(
                                     pInterface: INTERFACE_HANDLE;
                                     gfFilter: TGlobal_Filter): DWORD; stdcall;
function PfBindInterfaceToIndex(
                               pInterface: INTERFACE_HANDLE;
                               dwIndex: DWORD;
                               pfatLinkType: TPfAddressType;
                               LinkIPAddress: PBYTE): DWORD; stdcall;

function PfBindInterfaceToIPAddress(
                                   pInterface: INTERFACE_HANDLE;
                                   pfatType: TPfAddressType;
                                   IPAddress: PBYTE): DWORD; stdcall;

function PfCreateInterface(
                          dwName: DWORD;
                          inAction: TPfForward_Action;
                          outAction: TPfForward_Action;
                          bUseLog: BOOL;
                          bMustBeUnique: BOOL;
                          ppInterface: PINTERFACE_HANDLE): DWORD; stdcall;

function PfDeleteInterface(
                          pInterface: INTERFACE_HANDLE): DWORD; stdcall;

function PfDeleteLog(): DWORD; stdcall;

function PfGetInterfaceStatistics(
                                 pInterface: INTERFACE_HANDLE;
                                 ppfStats: PTPf_Interface_Stats;
                                 pdwBufferSize: PDWORD;
                                 fResetCounters: BOOL): DWORD; stdcall;

function PfMakeLog(
                  hEvent: THANDLE): WORD; stdcall;

function PfRebindFilters(
                        pInterface: INTERFACE_HANDLE;
                        pLateBindInfo: PTPf_Latebind_Info): DWORD; stdcall;

function PfRemoveFilterHandles(
                              pInterface: INTERFACE_HANDLE;
                              cFilters: DWORD;
                              pvHandles: PFILTER_HANDLE): DWORD; stdcall;

function PfRemoveFiltersFromInterface(
                                     ih: INTERFACE_HANDLE;
                                     cInFilters: DWORD;
                                     pfiltIn: PTPf_Filter_Descriptor;
                                     cOutFilters: DWORD;
                                     pfiltOut: PTPf_Filter_Descriptor): DWORD; stdcall;

function PfRemoveGlobalFilterFromInterface(
                                          pInterface: INTERFACE_HANDLE;
                                          gfFilter: TGlobal_Filter): DWORD; stdcall;

function PfSetLogBuffer(
                       pbBuffer: PBYTE;
                       dwSize: DWORD;
                       dwThreshold: DWORD;
                       dwEntries: DWORD;
                       pdwLoggedEntries: PDWORD;
                       pdwLostEntries: PDWORD;
                       pdwSizeUsed: PDWORD): DWORD; stdcall;

function PfTestPacket(
                     pInInterface: INTERFACE_HANDLE;
                     pOutInterface: INTERFACE_HANDLE;
                     cBytes: DWORD;
                     pbPacket: PBYTE;
                     ppAction: PTPfForward_Action): DWORD; stdcall;

function PfUnBindInterface(
                          pInterface: INTERFACE_HANDLE): DWORD; stdcall;

var
  DLLHandle: THandle;

implementation

const
  iphlpapi = 'iphlpapi.dll';

{$IFDEF DYNAMIC_LINK}
var
  _PfAddFiltersToInterface: Pointer;

function PfAddFiltersToInterface;
begin
  GetProcedureAddress(_PfAddFiltersToInterface, iphlpapi, '_PfAddFiltersToInterface@24');
  asm
    mov esp, ebp
    pop ebp
    jmp [_PfAddFiltersToInterface]
  end;
end;
{$ELSE}
function PfAddFiltersToInterface; external iphlpapi name '_PfAddFiltersToInterface@24';
{$ENDIF DYNAMIC_LINK}

{$IFDEF DYNAMIC_LINK}
var
  _PfAddGlobalFilterToInterface: Pointer;

function PfAddGlobalFilterToInterface;
begin
  GetProcedureAddress(_PfAddGlobalFilterToInterface, iphlpapi, '_PfAddGlobalFilterToInterface@8');
  asm
    mov esp, ebp
    pop ebp
    jmp [_PfAddGlobalFilterToInterface]
  end;
end;
{$ELSE}
function PfAddGlobalFilterToInterface; external iphlpapi name '_PfAddGlobalFilterToInterface@8';
{$ENDIF DYNAMIC_LINK}

{$IFDEF DYNAMIC_LINK}
var
  _PfBindInterfaceToIPAddress: Pointer;

function PfBindInterfaceToIPAddress;
begin
  GetProcedureAddress(_PfBindInterfaceToIPAddress, iphlpapi, '_PfBindInterfaceToIPAddress@12');
  asm
    mov esp, ebp
    pop ebp
    jmp [_PfBindInterfaceToIPAddress]
  end;
end;
{$ELSE}
function PfBindInterfaceToIPAddress; external iphlpapi name '_PfBindInterfaceToIPAddress@12';
{$ENDIF DYNAMIC_LINK}

{$IFDEF DYNAMIC_LINK}
var
  _PfBindInterfaceToIndex: Pointer;

function PfBindInterfaceToIndex;
begin
  GetProcedureAddress(_PfBindInterfaceToIndex, iphlpapi, '_PfBindInterfaceToIndex@16');
  asm
    mov esp, ebp
    pop ebp
    jmp [_PfBindInterfaceToIndex]
  end;
end;
{$ELSE}
function PfBindInterfaceToIndex; external iphlpapi name '_PfBindInterfaceToIndex@16';
{$ENDIF DYNAMIC_LINK}

{$IFDEF DYNAMIC_LINK}
var
  _PfCreateInterface: Pointer;

function PfCreateInterface;
begin
  GetProcedureAddress(_PfCreateInterface, iphlpapi, '_PfCreateInterface@24');
  asm
    mov esp, ebp
    pop ebp
    jmp [_PfCreateInterface]
  end;
end;
{$ELSE}
function PfCreateInterface; external iphlpapi name '_PfCreateInterface@24';
{$ENDIF DYNAMIC_LINK}

{$IFDEF DYNAMIC_LINK}
var
  _PfDeleteInterface: Pointer;

function PfDeleteInterface;
begin
  GetProcedureAddress(_PfDeleteInterface, iphlpapi, '_PfDeleteInterface@4');
  asm
    mov esp, ebp
    pop ebp
    jmp [_PfDeleteInterface]
  end;
end;
{$ELSE}
function PfDeleteInterface; external iphlpapi name '_PfDeleteInterface@4';
{$ENDIF DYNAMIC_LINK}

{$IFDEF DYNAMIC_LINK}
var
  _PfDeleteLog: Pointer;

function PfDeleteLog;
begin
  GetProcedureAddress(_PfDeleteLog, iphlpapi, '_PfDeleteLog@0');
  asm
    mov esp, ebp
    pop ebp
    jmp [_PfDeleteLog]
  end;
end;
{$ELSE}
function PfDeleteLog; external iphlpapi name '_PfDeleteLog@0';
{$ENDIF DYNAMIC_LINK}

{$IFDEF DYNAMIC_LINK}
var
  _PfGetInterfaceStatistics: Pointer;

function PfGetInterfaceStatistics;
begin
  GetProcedureAddress(_PfGetInterfaceStatistics, iphlpapi, '_PfGetInterfaceStatistics@16');
  asm
    mov esp, ebp
    pop ebp
    jmp [_PfGetInterfaceStatistics]
  end;
end;
{$ELSE}
function PfGetInterfaceStatistics; external iphlpapi name '_PfGetInterfaceStatistics@16';
{$ENDIF DYNAMIC_LINK}

{$IFDEF DYNAMIC_LINK}
var
  _PfMakeLog: Pointer;

function PfMakeLog;
begin
  GetProcedureAddress(_PfMakeLog, iphlpapi, '_PfMakeLog@4');
  asm
    mov esp, ebp
    pop ebp
    jmp [_PfMakeLog]
  end;
end;
{$ELSE}
function PfMakeLog; external iphlpapi name '_PfMakeLog@4';
{$ENDIF DYNAMIC_LINK}

{$IFDEF DYNAMIC_LINK}
var
  _PfRebindFilters: Pointer;

function PfRebindFilters;
begin
  GetProcedureAddress(_PfRebindFilters, iphlpapi, '_PfRebindFilters@8');
  asm
    mov esp, ebp
    pop ebp
    jmp [_PfRebindFilters]
  end;
end;
{$ELSE}
function PfRebindFilters; external iphlpapi name '_PfRebindFilters@8';
{$ENDIF DYNAMIC_LINK}

{$IFDEF DYNAMIC_LINK}
var
  _PfRemoveFilterHandles: Pointer;

function PfRemoveFilterHandles;
begin
  GetProcedureAddress(_PfRemoveFilterHandles, iphlpapi, '_PfRemoveFilterHandles@12');
  asm
    mov esp, ebp
    pop ebp
    jmp [_PfRemoveFilterHandles]
  end;
end;
{$ELSE}
function PfRemoveFilterHandles; external iphlpapi name '_PfRemoveFilterHandles@12';
{$ENDIF DYNAMIC_LINK}

{$IFDEF DYNAMIC_LINK}
var
  _PfRemoveFiltersFromInterface: Pointer;

function PfRemoveFiltersFromInterface;
begin
  GetProcedureAddress(_PfRemoveFiltersFromInterface, iphlpapi, '_PfRemoveFiltersFromInterface@20');
  asm
    mov esp, ebp
    pop ebp
    jmp [_PfRemoveFiltersFromInterface]
  end;
end;
{$ELSE}
function PfRemoveFiltersFromInterface; external iphlpapi name '_PfRemoveFiltersFromInterface@20';
{$ENDIF DYNAMIC_LINK}

{$IFDEF DYNAMIC_LINK}
var
  _PfRemoveGlobalFilterFromInterface: Pointer;

function _PfRemoveGlobalFilterFromInterface;
begin
  GetProcedureAddress(_PfRemoveGlobalFilterFromInterface, iphlpapi, '_PfRemoveGlobalFilterFromInterface@8');
  asm
    mov esp, ebp
    pop ebp
    jmp [_PfRemoveGlobalFilterFromInterface@8]
  end;
end;
{$ELSE}
function PfRemoveGlobalFilterFromInterface; external iphlpapi name '_PfRemoveGlobalFilterFromInterface@8';
{$ENDIF DYNAMIC_LINK}

{$IFDEF DYNAMIC_LINK}
var
  _PfSetLogBuffer: Pointer;

function _PfSetLogBuffer;
begin
  GetProcedureAddress(_PfSetLogBuffer, iphlpapi, '_PfSetLogBuffer@28');
  asm
    mov esp, ebp
    pop ebp
    jmp [_PfSetLogBuffer]
  end;
end;
{$ELSE}
function PfSetLogBuffer; external iphlpapi name '_PfSetLogBuffer@28';
{$ENDIF DYNAMIC_LINK}

{$IFDEF DYNAMIC_LINK}
var
  _PfTestPacket: Pointer;

function _PfTestPacket;
begin
  GetProcedureAddress(_PfTestPacket, iphlpapi, '_PfTestPacket@20');
  asm
    mov esp, ebp
    pop ebp
    jmp [_PfTestPacket]
  end;
end;
{$ELSE}
function PfTestPacket; external iphlpapi name '_PfTestPacket@20';
{$ENDIF DYNAMIC_LINK}

{$IFDEF DYNAMIC_LINK}
var
  _PfUnBindInterface: Pointer;

function _PfUnBindInterface;
begin
  GetProcedureAddress(_PfUnBindInterface, iphlpapi, '_PfUnBindInterface@4');
  asm
    mov esp, ebp
    pop ebp
    jmp [_PfUnBindInterface]
  end;
end;
{$ELSE}
function PfUnBindInterface; external iphlpapi name '_PfUnBindInterface@4';
{$ENDIF DYNAMIC_LINK}

end.

 

FLTDEFS.PAS

 

 
 
Your Ad Here