Click here to Skip to main content
Click here to Skip to main content
Go to top

Easy Port Forwarding and Managing Router with UPnP

, 24 Jun 2008
Rate this:
Please Sign up or sign in to vote.
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 awaySmile | :)

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); //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:

#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)

Share

About the Author

upredsun
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberJOE MIS19-Dec-13 13:51 
GeneralRe: My vote of 5 PinmemberPeter Eugene Coleman27-Mar-14 1:36 
GeneralRe: My vote of 5 PinmemberJOE MIS1-Apr-14 14:49 
QuestionUPNP PinmemberPeter Eugene Coleman30-Oct-13 4:55 
QuestionI'm a newbie and I might be missing something PinmemberPeter Eugene Coleman28-May-13 11:35 
QuestionUpnp enabled Gateway simulation on a windows XP PC PinmemberMember 972771917-Jan-13 2:34 
QuestionWhat Language is this? Pinmemberbegi2128-Oct-12 10:24 
Generaldiscovery error is Fail to find an UPNP NAT. PinmemberSebastian__17-May-11 8:29 
GeneralMy vote of 1 Pinmemberbrianbacon20-Jan-11 15:49 
Question"Double forwarding" PinmemberMember 330389117-Aug-09 10:39 
AnswerRe: "Double forwarding" PinmemberERAI5-Sep-10 3:55 
Generalplease check code formatting... Pinmemberemilio_grv25-Jun-08 21:12 
GeneralRe: please check code formatting... Pinmemberupredsun25-Jun-08 21:20 
GeneralRe: please check code formatting... Pinmemberemilio_grv26-Jun-08 20:55 
GeneralSome suggestions PinmemberEd.Poore25-Jun-08 0:57 
GeneralRe: Some suggestions PinmemberPeter Eugene Coleman28-May-13 11:28 
GeneralRe: Some suggestions PinmemberHans77Lindgren21-Feb-14 23:57 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 25 Jun 2008
Article Copyright 2008 by upredsun
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid