Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C++

Easy Port Forwarding and Managing Router with UPnP

Rate me:
Please Sign up or sign in to vote.
2.80/5 (21 votes)
24 Jun 2008CPOL2 min read 121.8K   5.6K   42   20
This article describes what port forwarding is. UPnP source code is also given.

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.

port forwarding,port forward

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.

C++
#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); //init
    bool discovery();//find router

    /****
     **** _description: port mapping name
     **** _destination_ip: internal ip address
     **** _port_ex:external: external listen port
     **** _destination_port: internal port
     **** _protocal: TCP or UDP
     ***/
    bool add_port_mapping(
             char * _description, char * _destination_ip, unsigned short int _port_ex,
             unsigned short int _destination_port, char * _protocal);//add port mapping

    const char * get_last_error(){ return last_error.c_str();}//get last error
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:

C++
#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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionUnresolved external symbol Pin
Rafael Davidson19-Jan-16 1:56
Rafael Davidson19-Jan-16 1:56 
GeneralMy vote of 5 Pin
JOE Heart Under Blade19-Dec-13 13:51
JOE Heart Under Blade19-Dec-13 13:51 
GeneralRe: My vote of 5 Pin
Peter Eugene Coleman27-Mar-14 1:36
Peter Eugene Coleman27-Mar-14 1:36 
GeneralRe: My vote of 5 Pin
JOE Heart Under Blade1-Apr-14 14:49
JOE Heart Under Blade1-Apr-14 14:49 
QuestionUPNP Pin
Peter Eugene Coleman30-Oct-13 4:55
Peter Eugene Coleman30-Oct-13 4:55 
QuestionI'm a newbie and I might be missing something Pin
Peter Eugene Coleman28-May-13 11:35
Peter Eugene Coleman28-May-13 11:35 
I am starting to learn Port Forwarding and this looks like it will give me the information I need - well done UpRedSun. As I understand it you have given us the code to put on the machine that's behind the router to set up the port forwarding but we haven't got the code that the other computer needs to access that local computer. Am I misunderstanding something? P. in Ireland.
QuestionUpnp enabled Gateway simulation on a windows XP PC Pin
Member 972771917-Jan-13 2:34
Member 972771917-Jan-13 2:34 
QuestionWhat Language is this? Pin
bEGI2328-Oct-12 10:24
bEGI2328-Oct-12 10:24 
Generaldiscovery error is Fail to find an UPNP NAT. Pin
Sebastian__17-May-11 8:29
Sebastian__17-May-11 8:29 
GeneralMy vote of 1 Pin
brianbacon20-Jan-11 15:49
brianbacon20-Jan-11 15:49 
Question"Double forwarding" Pin
Member 330389117-Aug-09 10:39
Member 330389117-Aug-09 10:39 
AnswerRe: "Double forwarding" Pin
ERAI5-Sep-10 3:55
ERAI5-Sep-10 3:55 
QuestionThanks Pin
doug655364-Aug-08 20:01
doug655364-Aug-08 20:01 
AnswerRe: Thanks Pin
upredsun4-Aug-08 23:47
upredsun4-Aug-08 23:47 
Generalplease check code formatting... Pin
Emilio Garavaglia25-Jun-08 21:12
Emilio Garavaglia25-Jun-08 21:12 
GeneralRe: please check code formatting... Pin
upredsun25-Jun-08 21:20
upredsun25-Jun-08 21:20 
GeneralRe: please check code formatting... Pin
Emilio Garavaglia26-Jun-08 20:55
Emilio Garavaglia26-Jun-08 20:55 
GeneralSome suggestions Pin
Ed.Poore25-Jun-08 0:57
Ed.Poore25-Jun-08 0:57 
GeneralRe: Some suggestions Pin
Peter Eugene Coleman28-May-13 11:28
Peter Eugene Coleman28-May-13 11:28 
GeneralRe: Some suggestions Pin
Hans77Lindgren21-Feb-14 23:57
Hans77Lindgren21-Feb-14 23:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.