Here is some working code to open the specified TCP port on the gateway
nat device or firewall, and forward it to the calling machine. Great
for p2p apps. The newsgroups are such a great resource and have helped
me so much, I hope this helps others. Let me know if you find it
useful!
Lee Carlson
Lee (at) Carlson (dot) net
-----------------------------------
using System;
using System.Text;
using System.Diagnost ics;
using System.Net.Sock ets;
using System.Net;
namespace Woodchop.Net
{
public class UPnP
{
public UPnP()
{
}
public static void OpenFirewallPor t(int port)
{
System.Net.Netw orkInformation. NetworkInterfac e[] nics =
System.Net.Netw orkInformation. NetworkInterfac e.GetAllNetwork Interfaces();
//for each nic in computer...
foreach (System.Net.Net workInformation .NetworkInterfa ce nic
in nics)
{
try
{
string machineIP =
nic.GetIPProper ties().UnicastA ddresses[0].Address.ToStri ng();
//send msg to each gateway configured on this nic
foreach
(System.Net.Net workInformation .GatewayIPAddre ssInformation gwInfo in
nic.GetIPProper ties().GatewayA ddresses)
{
try
{
OpenFirewallPor t(machineIP,
gwInfo.Address. ToString(), port);
}
catch
{ }
}
}
catch { }
}
}
public static void OpenFirewallPor t(string machineIP, string
firewallIP, int openPort)
{
string svc = getServicesFrom Device(firewall IP);
openPortFromSer vice(svc,"urn:s chemas-upnp-org:service:WAN IPConnection:1" ,machineIP,
firewallIP, 80, openPort);
openPortFromSer vice(svc,
"urn:schema s-upnp-org:service:WAN PPPConnection:1 ", machineIP,
firewallIP, 80, openPort);
}
private static string getServicesFrom Device(string firewallIP)
{
//To send a broadcast and get responses from all, send to
239.255.255.250
string queryResponse = "";
try
{
string query = "M-SEARCH * HTTP/1.1\r\n" +
"Host:" + firewallIP + ":1900\r\n" +
"ST:upnp:rootde vice\r\n" +
"Man:\"ssdp:dis cover\"\r\n" +
"MX:3\r\n" +
"\r\n" +
"\r\n";
//use sockets instead of UdpClient so we can set a
timeout easier
Socket client = new Socket(AddressF amily.InterNetw ork,
SocketType.Dgra m, ProtocolType.Ud p);
IPEndPoint endPoint = new
IPEndPoint(IPAd dress.Parse(fir ewallIP), 1900);
//1.5 second timeout because firewall should be on same
segment (fast)
client.SetSocke tOption(SocketO ptionLevel.Sock et,
SocketOptionNam e.ReceiveTimeou t, 1500);
byte[] q = Encoding.ASCII. GetBytes(query) ;
client.SendTo(q , q.Length, SocketFlags.Non e, endPoint);
IPEndPoint sender = new IPEndPoint(IPAd dress.Any, 0);
EndPoint senderEP = (EndPoint)sende r;
byte[] data = new byte[1024];
int recv = client.ReceiveF rom(data, ref senderEP);
queryResponse = Encoding.ASCII. GetString(data) ;
}
catch { }
if(queryRespons e.Length == 0)
return "";
/* QueryResult is somthing like this:
*
HTTP/1.1 200 OK
Cache-Control:max-age=60
Location:http://10.10.10.1:80/upnp/service/des_ppp.xml
Server:NT/5.0 UPnP/1.0
ST:upnp:rootdev ice
EXT:
USN:uuid:upnp-InternetGateway Device-1_0-00095bd945a2::u pnp:rootdevice
*/
string location = "";
string[] parts = queryResponse.S plit(new string[] {
System.Environm ent.NewLine }, StringSplitOpti ons.RemoveEmpty Entries);
foreach (string part in parts)
{
if (part.ToLower() .StartsWith("lo cation"))
{
location = part.Substring( part.IndexOf(': ') + 1);
break;
}
}
if (location.Lengt h == 0)
return "";
//then using the location url, we get more information:
System.Net.WebC lient webClient = new WebClient();
try
{
string ret = webClient.Downl oadString(locat ion);
Debug.WriteLine (ret);
return ret;//return services
}
catch (System.Excepti on ex)
{
Debug.WriteLine (ex.Message);
}
finally
{
webClient.Dispo se();
}
return "";
}
private static void openPortFromSer vice(string services, string
serviceType, string machineIP, string firewallIP, int gatewayPort, int
portToForward)
{
if (services.Lengt h == 0)
return;
int svcIndex = services.IndexO f(serviceType);
if (svcIndex == -1)
return;
string controlUrl = services.Substr ing(svcIndex);
string tag1 = "<controlUR L>";
string tag2 = "</controlURL>";
controlUrl = controlUrl.Subs tring(controlUr l.IndexOf(tag1)
+ tag1.Length);
controlUrl =
controlUrl.Subs tring(0,control Url.IndexOf(tag 2));
string soapBody = "<s:Envelop e " +
"xmlns:s=\"http ://schemas.xmlsoap .org/soap/envelope/ \" " +
"s:encodingStyl e=\"http://schemas.xmlsoap .org/soap/encoding/ \">" +
"<s:Body>" +
"<u:AddPortMapp ing xmlns:u=\"" + serviceType + "\">" +
"<NewRemoteHost ></NewRemoteHost>" +
"<NewExternalPo rt>" + portToForward.T oString() +
"</NewExternalPort >" +
"<NewProtocol>T CP</NewProtocol>" +
"<NewInternalPo rt>" + portToForward.T oString() +
"</NewInternalPort >" +
"<NewInternalCl ient>" + machineIP +
"</NewInternalClie nt>" +
"<NewEnable d>1</NewEnabled>" +
"<NewPortMappin gDescription>Wo odchop
Client</NewPortMappingD escription>" +
"<NewLeaseDurat ion>0</NewLeaseDuratio n>" +
"</u:AddPortMappin g>" +
"</s:Body>" +
"</s:Envelope>";
byte[] body =
System.Text.UTF 8Encoding.ASCII .GetBytes(soapB ody);
string url = "http://" + firewallIP + ":" +
gatewayPort.ToS tring() + controlUrl;
System.Net.WebR equest wr =
System.Net.WebR equest.Create(u rl);//+ controlUrl);
wr.Method = "POST";
wr.Headers.Add( "SOAPAction","\ "" + serviceType +
"#AddPortMappin g\"");
wr.ContentType = "text/xml;charset=\"u tf-8\"";
wr.ContentLengt h = body.Length;
System.IO.Strea m stream = wr.GetRequestSt ream();
stream.Write(bo dy, 0, body.Length);
stream.Flush();
stream.Close();
WebResponse wres = wr.GetResponse( );
System.IO.Strea mReader sr = new
System.IO.Strea mReader(wres.Ge tResponseStream ());
string ret = sr.ReadToEnd();
sr.Close();
Debug.WriteLine ("Setting port forwarding:" +
portToForward.T oString() + "\r\r" + ret);
}
}
}