In writing network code, NAT can be a real problem. For most applications, requiring the user to add port forwarding rules to their router is not a good idea, or even acceptable, because often users are not allowed to change the settings of their router or do not know how to do so.
Fortunately, there are ways to automate the process of adding port forwarding rules. UPnP is one of the ways, to my knowledge the most commonly used way.
Another major problem with networking programs is that they appear to have no reliable way of finding the external IP address of the computer it is running on. UPnP partly solves this, but it is, of course, not completely reliable (not all routers support it, and routers that do may have UPnP turned off for security reasons).
If you have ever tried to use existing UPnP libraries, you may have found that most of them simply do not work, or do not cover the NAT aspects of it. This prompted me to write my own, very small, UPnP library that only covers the NAT aspects, and not even fully at that - only UPnP device discovery, basic port forwarding, and retrieving the external IP address. However, for many networking programs, this is all that is needed.
Using the code
In the source code, you will find a public class
NAT, containing the following public methods:
static bool Discover()
static IPAddress GetExternalIP()
static void ForwardPort(int port, ProtocolType protocol, string description)
static void DeleteForwardingRule(int port, ProtocolType protocol)
Please note that these methods only do limited error checking, and will fail if there is no UPnP device or if the first UPnP device is too picky about the command syntax. Before
ForwardPort will work,
Discover has to be called.
Discover can take up to 3 seconds by default (it will time out after that - you can change the time out value), possibly minutes if you set the time out high, so for any GUI application, it is advisable to place the call on a separate thread.
To forward a port the simplest way, code like this will suffice:
UPnP.NAT.ForwardPort(12345, ProtocolType.Tcp, "MyApp (TCP)");
In practice, it would be best to check for exceptions - and the value returned by
UPnP + NAT
For the people who want to do it themselves, or who are interested, here is what I found out about UPnP in combination with NAT routers.
The first thing to do is find the UPnP device you are interested in. Apparently, the best way to do that is by using SSDP.
Finding the UPnP device using SSDP comes down to broadcasting a single packet over UDP and examining the replies. The packet looks like this (or at least, when it looks like this, it'll work):
"M-SEARCH * HTTP/1.1\r\n" +
"HOST: 188.8.131.52:1900\r\n" +
in ASCII. While the Host is 184.108.40.206, the packet still has to be broadcasted to the standard
Examining the replies is pretty straightforward. You should only get one, but if you get more anyway, the right one is identifiable. A standard reply from a NAT device looks like this:
HTTP/1.1 200 OK
Server:IGD-HTTP/1.1 UPnP/1.0 UPnP-Device-Host/1.0
The ST should be "upnp:rootdevice"; if it's not, it isn't the right device, and it shouldn't have replied in the first place, but one never knows what happens in a network.
Anyhow, we're interested in the location, since at that location there will be an XML file containing information about the device - we will need this to find out how to send commands to the device.
Sending commands to a UPnP device is made possible through something they called a "service". The XML file describing the device is quite long, so suffice it to know that the XPath to the service we're interested in is:
Since it uses namespaces, you will have to use an
XmlNamespaceManager, but that's a whole different subject.
The result of this query should look somewhat like "/serv3.xml", once again an XML file. Note that this is a relative location, so you will need to append it to the base-URL, which is "http://192.168.123.254:80" in this example.
This file has a double purpose: first, its contents tell you something about what kinds of commands it accepts and how it will reply to them, and second, you will be POST-ing SOAP to it. That's correct, XML again.
Before this, I had never used a HTTP-POST on an XML file, and it greatly surprised me, but apparently, this is the way it should work.
Sending commands then is just using SOAP in the right way. However, this is not very straightforward - if you are using a
WebRequest (as am I), then you will not only have to set the Method to "POST", but also add a header, and change the content-type:
function + "\"");
ContentType = "text/xml; charset=\"utf-8\"";
function here must be the same as the function you are sending in the contents.
That is pretty much all there is to it. With this information, you should be able to implement your own UPnP NAT traversal methods.
Points of interest
While writing the code for this tiny library, I was absolutely stunned by the complete absence of clear information on the subject. Most sites deal only with UPnP for media and such, or explain the lack of security of UPnP NAT-traversal, without going very far into details on how to actually do it. When there is such information, it is generally written in such a way that at least I do not understand half of it.
In the end, I used WireShark to examine µTorrent's UPnP traffic, and reverse-engineered the process. I hope that this article changes matters for some people so that they will not have to reverse-engineer anything to get UPnP NAT-traversal working.
- July 18, 2008: Wrote the first version of the UPnP NAT library.
- July 22, 2008: Wrote the first version of this article.
- July 22, 2008 (later that day): Updated the source and the article to work on some stricter routers and in cases where the port is not 80 (with thanks to ajiau).
- July 23, 2008: Updated source to work with some additional routers (thanks to Chris Harper)
- January 14, 2009: Updated source and article to (hopefully) work with yet some more additional routers, with thanks to everyone who posted feedback in the past half year.