Use CSharp and UPnP to open a Firewall port
While developing some of the P2P file transfer functionality for Filejack, I needed a way to allow inbound traffic to the client software without requiring the user to know how to configure their firewall, and the Universal Plug and Play (UPnP) protocol is one great way to accomplish this.
Nearly all firewalls support UPnP, which permits configuration of port forwarding policies by sending special SOAP packets to the firewall IP on the trusted side. I googled but never could find any simple code solutions which didn't require an external dll, but eventually I did gather enough info to create a simple CSharp class that does exactly what I need - open an external port and forward that traffic to an open port on an internal computer.
The class has one static method, so from your code all you have to do is call this:
OpenFirewallPort(int externalPort, int internalPort);
..Where externalPort is the Firewall port you want opened, and internalPort is the port on the computer you want it forwarded to (the computer which called the method).
The code sends the "open port" requests to each gateway IP configured on the calling computer, which works because the gateway is almost always the same IP as the Firewall.
If you want more control, just use the static overload of the same method where you can specify the gateway and the computer you want traffic forwarded to:
OpenFirewallPort(string machineIP, string firewallIP, int externalPort, int internalPort);
It's that easy! Download the source code here: UPnP.zip
Let me know how this works for you.

3 Comments:
The link for UPNP.zip does not work. Can you update this? Or send it by mail?
andreas.mattisson at gmail.com
Hi,
could update zip? or send it by mail at diegosz01@yahoo.es?
This post has been removed by a blog administrator.
Post a Comment
Links to this post:
Create a Link
<< Home