Programming a simple firewall in C#

  • Thread starter Thread starter Paul Steele
  • Start date Start date
P

Paul Steele

I'm working on a programming project that needs to control network traffic.
What I would like to do implement a mini-firewall as part of my program.
What would be involved with writing a firewall like program in C#? I realize
it's not really a simple project, but the firewall doesn't have to be very
smart. Just block data to all hosts except a few specific hosts/ports. Any
suggestions on how I should proceed would be greatly appreciated!
 
Paul:

Are you trying to control the traffic to and or from just your
application, or the entire machine?

If for the entire machine, your best bet might be to look at the
Routing and Remote Access Service APIs in the Platform SDK. You'll
have to PInvoke functions like PfCreateInterface,
PfAddFiltersToInterface, and so on:

http://msdn.microsoft.com/library/d...n-us/rras/rras/packet_filtering_reference.asp

I'm sure you could get some help if you google for some of the API
names.
 
Thanks for the information. My primary goal is to control outgoing traffic
for the entire machine. Incoming isn't as important but probably will need
some control as well. I'll check out your references and see what I can
find.
 
Scott Allen said:
Paul:

Are you trying to control the traffic to and or from just your
application, or the entire machine?

If for the entire machine, your best bet might be to look at the
Routing and Remote Access Service APIs in the Platform SDK. You'll
have to PInvoke functions like PfCreateInterface,
PfAddFiltersToInterface, and so on:

http://msdn.microsoft.com/library/d...n-us/rras/rras/packet_filtering_reference.asp

I'm sure you could get some help if you google for some of the API
names.

I found this solution at Code Project. It appears to be exactly what I want:

http://www.thecodeproject.com/managedcpp/packetfilteringnet.asp
 
Scott Allen said:
Paul:

Are you trying to control the traffic to and or from just your
application, or the entire machine?

If for the entire machine, your best bet might be to look at the
Routing and Remote Access Service APIs in the Platform SDK. You'll
have to PInvoke functions like PfCreateInterface,
PfAddFiltersToInterface, and so on:

http://msdn.microsoft.com/library/d...n-us/rras/rras/packet_filtering_reference.asp

I'm sure you could get some help if you google for some of the API names.

I've found the information on the various packet filtering functions and
they seem relatively straightforward to use. However, I'm not entirely clear
how to set up access to the functions in C#. By PInvoke I presume you mean
dllimport, so for a function like this:

DWORD PfCreateInterface(
DWORD dwName,
PFFORWARD_ACTION inAction,
PFFORWARD_ACTION outAction,
BOOL bUseLog,
BOOL bMustBeUnique,
INTERFACE_HANDLE* ppInterface
);

I presume the set up to dllimport would be something like this (and this is
a big guess):

[dllimport ("iphlpapi.dll")]
public static extern int pfCreateInterface(int dwName, int inAction, int
OutAction, bool bUseLog, bool bMustBeUnique, intptr ppInterface);

The actual information on the MSDN sites for these packet filtering
functions is pretty sparse, but I think if I can find enough code samples I
can figure it out. Any pointers would be appreciated.
 
Back
Top