?
Solved

VB declare for PfAddFiltersToInterface

Posted on 2005-03-30
12
Medium Priority
?
641 Views
Last Modified: 2008-02-26
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 ?
0
Comment
Question by:Marco_Panza
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 28

Accepted Solution

by:
AzraSound earned 2000 total points
ID: 13668847
Just guessing...

According to MS:

DWORD PfAddFiltersToInterface(
  INTERFACE_HANDLE ih,
  DWORD cInFilters,
  PPF_FILTER_DESCRIPTOR pfiltIn,
  DWORD cOutFilters,
  PPF_FILTER_DESCRIPTOR pfiltOut,
  PFILTER_HANDLE pfHandle
);



So, we need the structures/enums, first:

Enum PFADDRESSTYPE
    PF_IPV4 = ???   'COULD NOT FIND CONSTANT FOR THIS
    PF_IPV6 = ???   'COULD NOT FIND CONSTANT FOR THIS
End Enum


Type PPF_FILTER_DESCRIPTOR
    dwFilterFlags As Long
    dwRule As Long
    pfatType As PFADDRESSTYPE
    SrcAddr As Byte
    SrcMask As Byte
    DstAddr As Byte
    DstMask As Byte
    dwProtocol As Long
    fLateBound As Long
    wSrcPort As Integer
    wDstPort As Integer
    wSrcPortHighRange As Integer
    wDstPortHighRange As Integer
End Type



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
0
 

Author Comment

by:Marco_Panza
ID: 13669561
OK

Very good !!!

Can you help me with this declare:

PfBindInterfaceToIndex
PfCreateInterface

Thanks !!
0
 
LVL 8

Expert Comment

by:anthonywjones66
ID: 13669765
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Marco_Panza
ID: 13669845
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 ?
0
 

Author Comment

by:Marco_Panza
ID: 13669996
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 ?
0
 
LVL 8

Expert Comment

by:anthonywjones66
ID: 13670034
>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.
0
 

Author Comment

by:Marco_Panza
ID: 13670094
Anthony

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

Why ?
0
 
LVL 8

Expert Comment

by:anthonywjones66
ID: 13670157
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.
0
 

Author Comment

by:Marco_Panza
ID: 13670186
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 ?
0
 
LVL 8

Expert Comment

by:anthonywjones66
ID: 13670206
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
0
 

Author Comment

by:Marco_Panza
ID: 13670222
Yes ,

I have just download it. It is very good.

Regards

Marco
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 13671680
>>_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).
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question