Link to home
Start Free TrialLog in
Avatar of Marco_Panza
Marco_Panza

asked on

VB declare for PfAddFiltersToInterface

Hi ,

I was find this API 'PfAddFiltersToInterface' and  'PfRemoveFiltersFromInterface' that add and remove some filter (IP , port , etc..) but the VB declare is difficoult.

Can someone send me a VB declare for these API ?
ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marco_Panza
Marco_Panza

ASKER

OK

Very good !!!

Can you help me with this declare:

PfBindInterfaceToIndex
PfCreateInterface

Thanks !!
I think this is gonna take more work than that :)

SrcAddr etc are PBYTE not byte, they are pointers to an array of bytes.  How many bytes is dependant on whether IPV4 of IPV6 is being used.  So currently only IPV4 (which is 0) is likely to be the only one of interest the SrcAddr will be a pointer to an array of 4 bytes.  The same is true of SrcMask, DstAddr and DstMask.

Additionallly pfltIn and pFltOut parameters are also pointers to arrays of PF_FILTER_DESCRIPTORs and pfHandle is a pointer to an array of Filter handles.

Hence using these APIs are perhaps even more difficult than was first thought.

Anthony.
I was find this code:

**********************************************************************************************************************

// Creating the interface and associating it with
// a local ip address INTERFACE_HANDLE hInterface;
PfCreateInterface(0,
              PF_ACTION_FORWARD,
              PF_ACTION_FORWARD,
              FALSE,
              TRUE,
              &hInterface);


// look this byte order for ip address!!
BYTE localIp[]    = {172,29,16,2};
PfBindInterfaceToIPAddress(hInterface, PF_IPV4, localIp);

// We go to add a filter. Forbid outgoing http traffic, for example.
FILTER_HANDLE fHandle;

// Fill the filter rule data
PF_FILTER_DESCRIPTOR inFilter;
inFilter.dwFilterFlags       = FD_FLAGS_NOSYN;    //always this value
inFilter.dwRule              = 0;        //always this value
inFilter.pfatType            = PF_IPV4;    //using ipV4 addresses
inFilter.SrcAddr             = localIp;    //set local ip
inFilter.SrcMask             = "\xff\xff\xff\xff";   //mask for local ip
inFilter.wSrcPort            = FILTER_TCPUDP_PORT_ANY;  //any source port
inFilter.wSrcPortHighRange   = FILTER_TCPUDP_PORT_ANY;
inFilter.DstAddr             = 0;            //any destination
inFilter.DstMask             = 0;
inFilter.wDstPort            = 80;    //destination port 80(http service)
inFilter.wDstPortHighRange   = 80;
inFilter.dwProtocol          = FILTER_PROTO_TCP;    // Tcp protocol

// Add the filter
PfAddFiltersToInterface(hInterface, 1, &inFilter, 0, NULL, &fHandle);

//...............
//...............

// Remove the filter
PfRemoveFilterHandles(hInterface, 1, &fHandle);

// Unbind and delete interface
PfUnBindInterface(hInterface);
PfDeleteInterface(hInterface);

**************************************************************************************************************

The previous declare in VB is good ?
When I try to use this declere:

Declare Function PfAddFiltersToInterface Lib "Iphlpapi.dll" (ByVal ih As Long,  ByVal cInFilters As Long, pfiltIn As PPF_FILTER_DESCRIPTOR, ByVal cOutFilters As Long, pFiltOut As PPF_FILTER_DESCRIPTOR, ByVal pfHandle As Long) As Long

Apper a message like 'Impossible to find the input pointer ...'

Why ?
>Previous declare in VB is good?

Not according to the documentation. I'm afraid I don't know C++ at all well but I'm willing to bet that where the compilier sees the line:-

inFilter.SrcAddr = localIp

it spots that SrcAddr is defined as a PBYTE and takes appropriate action.  If not then the documentation for the API is misleading.  At the very least SrcAddr etc should be Long.  Even if it takes the IP address as a direct value Byte just isn't big enough.

Calling the APIs for each Filter you want to add or remove could work although the intention of the API is enable you assign a set of filters in one call.
You don't have to retrieve the set of Filter handles from AddFilters if your not going to use them.  BTW, the pfHandle parameter of the AddFilters call should be ByRef not ByVal since it is a _pointer_ to the array longs that will recieve the set of filter handles.  Of course this will only work if you only add one filter at at time. Calling this API with more than one filter would be a bad thing.

Here's my attempt at a wrapper for these API's

'mdlIPHlp
Option Explicit

Public Type PF_FILTER_DESCRIPTOR
  dwFilterFlags As Long
  dwRule As Long
  pfatType As Long
  SrcAddr As Long
  SrcMask As Long
  DstAddr As Long
  DstMask As Long
  dwProtocol As Long
  lfLateBound As Long
  wSrcPort As Integer
  wDstPort As Integer
  wSrcPortHighRange As Integer
  wDstPortHighRange As Integer
End Type

Private Declare Function PfAddFiltersToInterface Lib "IPHlpApi.dll" (ByVal ih As Long, ByVal cInFilters As Long, ByRef pfiltIn As Any, _
  ByVal cOutFilters As Long, ByRef pfiltOut As Any, ByRef pfHandle As Any) As Long

Private Declare Function PfRemoveFiltersFromInterface Lib "IPHlpApi.dll" (ByVal ih As Long, ByVal cInFilters As Long, ByRef pfiltIn As Any, _
  ByVal cOutFilters As Long, ByRef pfiltOut As Any) As Long

Public Function AddFiltersInput(ByVal ih As Long, ByRef ratFlt() As PF_FILTER_DESCRIPTOR, ByVal Handles As Boolean) As Long()

Dim atFlt() As PF_FILTER_DESCRIPTOR
Dim i As Long
Dim alHandles() As Long

atFlt = ratFlt

For i = 0 To UBound(ratFlt)
    atFlt(i).SrcAddr = VarPtr(ratFlt(i).SrcAddr)
    atFlt(i).SrcMask = VarPtr(ratFlt(i).SrcMask)
    atFlt(i).DstAddr = VarPtr(ratFlt(i).DstAddr)
    atFlt(i).DstMask = VarPtr(ratFlt(i).DstMask)
Next

If Handles Then
    ReDim alHandles(0 To UBound(ratFlt)) As Long
    PfAddFiltersToInterface ih, UBound(atFlt) + 1, atFlt(0), 0, ByVal 0&, alHandles(0)
    AddFiltersInput = alHandles
Else
    PfAddFiltersToInterface ih, UBound(atFlt) + 1, atFlt(0), 0, ByVal 0&, ByVal 0&
End If

End Function

Public Function AddFiltersOutput(ByVal ih As Long, ByRef ratFlt() As PF_FILTER_DESCRIPTOR, ByVal Handles As Boolean) As Long()

Dim atFlt() As PF_FILTER_DESCRIPTOR
Dim i As Long
Dim alHandles() As Long

atFlt = ratFlt

For i = 0 To UBound(ratFlt)
    atFlt(i).SrcAddr = VarPtr(ratFlt(i).SrcAddr)
    atFlt(i).SrcMask = VarPtr(ratFlt(i).SrcMask)
    atFlt(i).DstAddr = VarPtr(ratFlt(i).DstAddr)
    atFlt(i).DstMask = VarPtr(ratFlt(i).DstMask)
Next

If Handles Then
    ReDim alHandles(0 To UBound(ratFlt)) As Long
    PfAddFiltersToInterface ih, 0, ByVal 0&, UBound(atFlt) + 1, atFlt(0), alHandles(0)
    AddFiltersInput = alHandles
Else
    PfAddFiltersToInterface ih, 0, ByVal 0&, UBound(atFlt) + 1, atFlt(0), ByVal 0&
End If

End Function

Public Function AddFilters(ByVal ih As Long, ByRef ratFltIn() As PF_FILTER_DESCRIPTOR, ByRef ratFltOut() As PF_FILTER_DESCRIPTOR, ByVal Handles As Boolean) As Long()

Dim atFltIn() As PF_FILTER_DESCRIPTOR
Dim atFltOut() As PF_FILTER_DESCRIPTOR
Dim i As Long
Dim alHandles() As Long

atFltIn = ratFltIn

For i = 0 To UBound(ratFltIn)
    atFltIn(i).SrcAddr = VarPtr(ratFltIn(i).SrcAddr)
    atFltIn(i).SrcMask = VarPtr(ratFltIn(i).SrcMask)
    atFltIn(i).DstAddr = VarPtr(ratFltIn(i).DstAddr)
    atFltIn(i).DstMask = VarPtr(ratFltIn(i).DstMask)
Next

atFltOut = ratFltOut
For i = 0 To UBound(ratFltOut)
    atFltOut(i).SrcAddr = VarPtr(ratFltOut(i).SrcAddr)
    atFltOut(i).SrcMask = VarPtr(ratFltOut(i).SrcMask)
    atFltOut(i).DstAddr = VarPtr(ratFltOut(i).DstAddr)
    atFltOut(i).DstMask = VarPtr(ratFltOut(i).DstMask)
Next

If Handles Then
    ReDim alHandles(0 To UBound(ratFltIn) + UBound(ratFltOut) + 1) As Long
    PfAddFiltersToInterface ih, UBound(atFltIn) + 1, atFltIn(0), UBound(atFltOut) + 1, atFltOut(0), alHandles(0)
    AddFilters = alHandles
Else
    PfAddFiltersToInterface ih, UBound(atFltIn) + 1, atFltIn(0), UBound(atFltOut) + 1, atFltOut(0), ByVal 0&
End If

End Function

Public Sub RemoveFiltersInput(ByVal ih As Long, ByRef ratFlt() As PF_FILTER_DESCRIPTOR)

Dim atFlt() As PF_FILTER_DESCRIPTOR
Dim i As Long

atFlt = ratFlt

For i = 0 To UBound(ratFlt)
    atFlt(i).SrcAddr = VarPtr(ratFlt(i).SrcAddr)
    atFlt(i).SrcMask = VarPtr(ratFlt(i).SrcMask)
    atFlt(i).DstAddr = VarPtr(ratFlt(i).DstAddr)
    atFlt(i).DstMask = VarPtr(ratFlt(i).DstMask)
Next

PfRemoveFiltersFromInterface ih, UBound(atFlt) + 1, atFlt(0), 0, ByVal 0&, ByVal 0&

End Sub

Public Sub RemoveFiltersOutput(ByVal ih As Long, ByRef ratFlt() As PF_FILTER_DESCRIPTOR)

Dim atFlt() As PF_FILTER_DESCRIPTOR
Dim i As Long

atFlt = ratFlt

For i = 0 To UBound(ratFlt)
    atFlt(i).SrcAddr = VarPtr(ratFlt(i).SrcAddr)
    atFlt(i).SrcMask = VarPtr(ratFlt(i).SrcMask)
    atFlt(i).DstAddr = VarPtr(ratFlt(i).DstAddr)
    atFlt(i).DstMask = VarPtr(ratFlt(i).DstMask)
Next

PfRemoveFiltersFromInterface ih, 0, ByVal 0&, UBound(atFlt) + 1, atFlt(0), ByVal 0&

End Sub

Public Sub RemoveFilters(ByVal ih As Long, ByRef ratFltIn() As PF_FILTER_DESCRIPTOR, ByRef ratFltOut() As PF_FILTER_DESCRIPTOR)

Dim atFltIn() As PF_FILTER_DESCRIPTOR
Dim atFltOut() As PF_FILTER_DESCRIPTOR
Dim i As Long

atFltIn = ratFltIn

For i = 0 To UBound(ratFltIn)
    atFltIn(i).SrcAddr = VarPtr(ratFltIn(i).SrcAddr)
    atFltIn(i).SrcMask = VarPtr(ratFltIn(i).SrcMask)
    atFltIn(i).DstAddr = VarPtr(ratFltIn(i).DstAddr)
    atFltIn(i).DstMask = VarPtr(ratFltIn(i).DstMask)
Next

atFltOut = ratFltOut
For i = 0 To UBound(ratFltOut)
    atFltOut(i).SrcAddr = VarPtr(ratFltOut(i).SrcAddr)
    atFltOut(i).SrcMask = VarPtr(ratFltOut(i).SrcMask)
    atFltOut(i).DstAddr = VarPtr(ratFltOut(i).DstAddr)
    atFltOut(i).DstMask = VarPtr(ratFltOut(i).DstMask)
Next

PfRemoveFiltersFromInterface ih, UBound(atFltIn) + 1, atFltIn(0), UBound(atFltOut) + 1, atFltOut(0), ByVal 0&

End Sub

Anthony.
Anthony

I was try your code but apper a message like 'Impossible to find the input pointer of dll  PfAddFiltersToInterface in IPHlpApi.dll'

Why ?
Because for some reason they are named:-

_PfAddFiltersToInterface@24
_PfRemoveFiltersFromInterface@20

in the DLL.

It's disconcerting to see them named like that, it's as if MS doesn't want us mere mortals to be calling these.  They may have good reason.

Anthony.
Ok thanks !!!!

I was use this declere:

Private Declare Function PfCreateInterface Lib "Iphlpapi.dll" Alias "_PfAddFiltersToInterface@24" (ByVal dwName As String, ByVal inAction As Long, ByVal outAction As Long, ByVal bUseLog As Boolean, ByVal bMustBeUnique As Boolean, ByVal ppInterface As Long) As Long

Is good ?
Nope.

Use the depends.exe tool you should have installed with VB6 and open the IPHlpAPi.dll found in the System32 folder.  In here you can examine all the exported functions of the DLL to find the correct name for PfCreateInterface.

Anthony
Yes ,

I have just download it. It is very good.

Regards

Marco
>>_PfAddFiltersToInterface@24

Typical name mangling, but the 24 is useful because it means the function expects 24 bytes worth of parameters.  It may be that each parameter should be declared as long, since I believe they are almost all pointers to certain structures/variables.  The trick is, if it accepts that type of declaration, to copy the values pointed to into the appropriate structure (with the CopyMemory function).