Introduction
The article will let you know what port forwarding is and how to add a port forwarding entry into your router with UPnP technology.
What is port forwarding
Port forwarding or port triggering allows remote computers (e.g., public machines on the Internet) to connect to a specific computer within a private LAN.
Basically, port forwarding works so that you can set your router to let internet users access your computer that is behind your router. You router has a public IP and your computer just has a private IP. Internet users can just directly access your router, but not your computer. So if you are using utorrent or other P2P software and if you want get a high download speed, you must let internet users directly access your computer. Then, you must set your router and add port mapping. Often, you must login into your router's administrator page and manually add port mapping. If you download movies or files from the internet using azureus, utorrent, or BitComet and want a very high download speed, you must know how to operate your router to add or delete port mappings.
Manual port forwarding vs. auto port forwarding
Anyone can configure a router for port forwarding, but it's a manual process that's usually not simple, and it's a process that differs for each different model of router. There are a few websites out there that centralizes instructions for configuring most common routers -- the best of these is probably www.PortForward.com. Take a look at that site, and you'll see the great variety in instructions for configuring routers to port-forward incoming connections. The UPnP technology makes it easier to configure routers for port forwarding and NAT traversal, since the configuration can be done programmatically for you, without needing manual steps from you.
What is UPnP?
Universal Plug and Play (UPnP) is a set of computer network protocols promulgated by the UPnP Forum. The goals of UPnP are to allow devices to connect seamlessly and to simplify the implementation of networks in home (data sharing, communications, and entertainment) and corporate environments. UPnP achieves this by defining and publishing UPnP device control protocols built upon open, Internet-based communication standards. If you want to buy a router, you should check the specification: if you don't see UPnP supported, then throw it away:)
How to implement a UPnP engine?
Actually, UPnP is just a SOAP message. You can check the attachment and find the source code to learn how to add a port forwarding entry into a router.
#ifndef UPNPNAT_H
#define UPNPNAT_H
#include <string>
#include <vector>
#pragma warning(disable: 4251)
#define DefaultTimeOut 10
#define DefaultInterval 200
class __declspec (dllexport) UPNPNAT
{
public:
bool init(int time_out=DefaultTimeOut,int interval=DefaultInterval); bool discovery();
bool add_port_mapping(
char * _description, char * _destination_ip, unsigned short int _port_ex,
unsigned short int _destination_port, char * _protocal);
const char * get_last_error(){ return last_error.c_str();}private:
bool get_description(); bool parser_description(); bool tcp_connect(const char * _addr,unsigned short int _port);
bool parse_mapping_info();
int udp_socket_fd;
int tcp_socket_fd;
typedef enum
{
NAT_INIT=0,
NAT_FOUND,
NAT_TCP_CONNECTED,
NAT_GETDESCRIPTION,
NAT_GETCONTROL,
NAT_ADD,
NAT_DEL,
NAT_GET,
NAT_ERROR
} NAT_STAT;
NAT_STAT status;
int time_out;
int interval;
std::string service_type;
std::string describe_url;
std::string control_url;
std::string base_url;
std::string service_describe_url;
std::string description_info;
std::string last_error;
std::string mapping_info;
};
#endif
Here is the demo source code to learn how to use the UPnp engine:
#include "upnpnat.h"
int main (int argc,char * argv[]){
UPNPNAT nat;
nat.init(5,10);
if(!nat.discovery()){
printf("discovery error is %s\n",nat.get_last_error());
return -1;
}
if(!nat.add_port_mapping("test","192.168.12.117",1234,1234,"TCP")){
printf("add_port_mapping error is %s\n",nat.get_last_error());
return -1;
}
printf("add port mapping succ.\n");
return 0;
}
Reference
Here is a good port forwarding tool and port forwarding blog for you.