Click here to Skip to main content
15,605,987 members
Articles / Programming Languages / C++
Posted 28 Apr 2009

Tagged as


26 bookmarked

Portmappings on UPnP-NAT(s) using C++, winsock2, xerces2.8 (Linux friendly code)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
28 Apr 2009CPOL
Portmappings on UPnP-NAT(s) using C++, winsock2, xerces2.8 (Linux friendly code)


This article introducing a portmapping method and some IGD's UPnP SOAP actions using C++ without the Microsoft's COM library for the UPnP. So sorry, you have to setup libraries and VC paths like an image[red square] below. This project using boost-cpplib and xerces (DOM parsing). But I couldn't upload libraries with my source code because of an upload timeout with large file size.


I had just tested soap responses from UPnP-enabled routers. I had not tested the connections and data streams as points of this article.

Sorry for potential bugs and my poor English.

  • Multiple NICs and multiple routers are supported. But CASCADED NATs are not supported.


I want to implement the portmapping without third party (UPnP) libraries and attempt to write platform independent code AS POSSIBLE.

Using the Code

#include "stdafx.h"

#include "Manager/ApplicationManager.h"
#include "Manager/COMManager.h"

#include "Network/NetworkManager.h"
#include "Network/NetworkHelper.h"

#include "NATBreak/UPNPNATServiceTypes.h"
#include "NATBreak/IGDDiscoverProcess.h"
#include "NATBreak/UPNPNATHTTPClient.h"

using namespace std;
using namespace boost;

thread_group tg;

int _tmain(int argc, _TCHAR* argv[])
	ApplicationManager am;
	NetworkManager nm;

		IGDDiscoverProcess discoverer( 500000 );

		tg.create_thread( ref( discoverer ) );

		IGDDiscoverProcess::IGDControlInformations cinfos = 
		boost_foreach( IGDDiscoverProcess::ControlInfo info, cinfos )
			if( info.has_problem )
				cout << "has problem" << endl;

			if( info.service_type == UPNPSERVICE_LAYER3FORWARDING1 )
				// Layer3Forwarding:1 srvice

				UPNPNATHTTPClient soap( info.ip, info.port );

				( info.control_url, info.service_type );
			else if( info.service_type == UPNPSERVICE_WANIPCONNECTION1 )
				// WANIPConnection service
				UPNPNATHTTPClient soap( info.ip, info.port );
				string ip;
				UPNPNATHTTPClient::SoapResult res = 
					ip, info.control_url, info.service_type 
				cout << "WAN IP : " << ip << endl;

				UPNPNATHTTPClient::SoapResult soap_res;
				soap_res = soap.AddPortMapping( 
				    string("test"), 12345, 12345, string("UDP"), 
				    string(""), // own nic's address 

                                         info.control_url, info.service_type 
				if( UPNPNATHTTPClient::SoapSucceeded == soap_res )
					cout << "portmapping succeeded." << endl;

				soap_res = soap.DeletePortMapping(
					12345, string("UDP"), info.control_url, 
				if( UPNPNATHTTPClient::SoapSucceeded == soap_res )
					cout << "delete portmapping succeeded." 
						<< endl;
			else if( info.service_type == 
				// WANCommonInterafaceConfig:1 srvice

				UPNPNATHTTPClient soap( info.ip, info.port );

				string ip;
				soap.GetActiveConnection( 0,  
					info.service_type, info.control_url );
	catch( IGDDiscoverProcess::exception &e )
		cout << e.what() << endl;

	return 0;

Points of Interest

  • In case when multiple UPnP-enabled NATs are cascaded
  • IPv6 support


This is the first version. 

The sample code has been fixed. The fixed part seems "string(""),". This address means an own nic's address to send a m-search message.


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

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

Comments and Discussions

GeneralExcellent article Pin
xComaWhitex1-Dec-10 21:33
xComaWhitex1-Dec-10 21:33 
QuestionHow to compile the code with Boost? Pin
Tage Lejon28-Apr-09 22:45
Tage Lejon28-Apr-09 22:45 
AnswerRe: How to compile the code with Boost? Pin
Yohei Murakami6-May-09 2:40
Yohei Murakami6-May-09 2:40 

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.