QtNat is a lightweight C++ library built with Qt 6 that simplifies NAT port mapping using UPnP (Universal Plug and Play). It is designed to help developers easily expose local services to external networks without requiring manual router configuration for users.
By leveraging UPnP, QtNat communicates with compatible routers to automatically create port forwarding rules at runtime. This makes it especially useful for peer-to-peer applications, multiplayer games, remote access tools, and any software that requires reliable inbound connectivity behind NAT.
QtNat provides a simplified API to automatically perform all steps: discovery and mapping. This has been tested on my local device. Feel free to test it and improve it.
use it
UpnpNat nat;
QObject::connect(&nat, &UpnpNat::statusChanged, [&nat, &app]() {
switch(nat.status())
{
case UpnpNat::NAT_STAT::NAT_IDLE:
case UpnpNat::NAT_STAT::NAT_DISCOVERY:
case UpnpNat::NAT_STAT::NAT_GETDESCRIPTION:
case UpnpNat::NAT_STAT::NAT_DESCRIPTION_FOUND:
break;
case UpnpNat::NAT_STAT::NAT_FOUND:
nat.requestDescription();
break;
case UpnpNat::NAT_STAT::NAT_READY:
nat.addPortMapping("UpnpTest", nat.localIp(), 6664, 6664, "TCP");
break;
case UpnpNat::NAT_STAT::NAT_ADD:
qDebug() << "It worked!";
app.quit();
break;
case UpnpNat::NAT_STAT::NAT_ERROR:
qDebug() <<"Error:" <
- We create object (l:0)
- We connect to the StatusChanged signal to get notified (L:2)
- when the situation is NAT_FOUNDWe request details (L:11)
- when the situation is NAT_READYWe request port mapping (L:14)
- when the situation is NAT_ADDThis means that the port mapping request has been added, it worked! The application closes.(l:17)
- when the situation is NAT_ERRORError occurred and display the error text. The application exits when an error occurs. (L:21)
- To detect errors we connect to error transformation. (L:14)
- We start searching. (L:28)
technical explanations
Search
Basically, we need to know if there is a upnp server nearby or not. To do this, we send a find m Make a request to a multicast address.
Here is the code:
#define HTTPMU_HOST_ADDRESS "239.255.255.250"
#define HTTPMU_HOST_PORT 1900
#define SEARCH_REQUEST_STRING "M-SEARCH * HTTP/1.1\n" \
"ST:UPnP:rootdevice\n" \
"MX: 3\n" \
"Man:\"ssdp:discover\"\n" \
"HOST: 239.255.255.250:1900\n" \
"\n"
void UpnpNat::discovery()
{
setStatus(NAT_STAT::NAT_DISCOVERY);
m_udpSocketV4.reset(new QUdpSocket(this));
QHostAddress broadcastIpV4(HTTPMU_HOST_ADDRESS);
m_udpSocketV4->bind(QHostAddress(QHostAddress::AnyIPv4), 0);
QByteArray datagram(SEARCH_REQUEST_STRING);
connect(m_udpSocketV4.get(), &QTcpSocket::readyRead, this, [this]() {
QByteArray datagram;
while(m_udpSocketV4->hasPendingDatagrams())
{
datagram.resize(int(m_udpSocketV4->pendingDatagramSize()));
m_udpSocketV4->readDatagram(datagram.data(), datagram.size());
}
QString result(datagram);
auto start= result.indexOf("http://");
if(start < 0)
{
setError(tr("Unable to read the beginning of server answer"));
setStatus(NAT_STAT::NAT_ERROR);
return;
}
auto end= result.indexOf("\r", start);
if(end < 0)
{
setError(tr("Unable to read the end of server answer"));
setStatus(NAT_STAT::NAT_ERROR);
return;
}
m_describeUrl= result.sliced(start, end - start);
setStatus(NAT_STAT::NAT_FOUND);
m_udpSocketV4->close();
});
connect(m_udpSocketV4.get(), &QUdpSocket::errorOccurred, this, [this](QAbstractSocket::SocketError) {
setError(m_udpSocketV4->errorString());
setStatus(NAT_STAT::NAT_ERROR);
});
m_udpSocketV4->writeDatagram(datagram, broadcastIpV4, HTTPMU_HOST_PORT);
}
The overall goal of the search is to achieve Description File from server with all available tools and services. result is stored m_describeUrl,
Request description file
Simple requests using QNetworkAccessManager.
void UpnpNat::requestDescription()
{
setStatus(NAT_STAT::NAT_GETDESCRIPTION);
QNetworkRequest request;
request.setUrl(QUrl(m_describeUrl));
m_manager.get(request);
}
Parsing the description file
Your physical network device can act as multiple Upnp devices. You are looking for one of these device types:
- urn:schemas-unp-org:device:InternetGatewayDevice
- urn:schema-unp-org:device:WANDevice
- urn:schemas-upnp-org:device:WANConnectionDevice
Those types are followed with a number (1 or 2), this is the Upnp protocol version supported by the device.
void UpnpNat::processXML(QNetworkReply* reply)
{
auto data= reply->readAll();
if(data.isEmpty()) {
setError(tr("Description file is empty"));
setStatus(NAT_STAT::NAT_ERROR);
return;
}
setStatus(NAT_STAT::NAT_DESCRIPTION_FOUND);
/*
Boring XML parsing in order to find devices and services.
Devices:
constexpr auto deviceType1{"urn:schemas-upnp-org:device:InternetGatewayDevice"};
constexpr auto deviceType2{"urn:schemas-upnp-org:device:WANDevice"};
constexpr auto deviceType3{"urn:schemas-upnp-org:device:WANConnectionDevice"};
Services:
constexpr auto serviceTypeWanIP{"urn:schemas-upnp-org:service:WANIPConnection"};
constexpr auto serviceTypeWANPPP{"urn:schemas-upnp-org:service:WANPPPConnection"};
*/
m_controlUrl = /* Most important thing to find the controlUrl of the proper service.*/
setStatus(NAT_STAT::NAT_READY);
}
send mapping request
Sending a request is simply sending an HTTP request with the appropriate data.
I use inja to generate http data properly.
This is Inja template.
{{ port }}
{{ protocol }}
{{ port }}
{{ ip }}
1
{{ description }}
0
Then, let’s create a json object with all the data. As a final step, we need to create a request, set up its data, and then post it.
void UpnpNat::addPortMapping(const QString& description, const QString& destination_ip, unsigned short int port_ex,
unsigned short int port_in, const QString& protocol)
{
inja::json subdata;
subdata["description"]= description.toStdString();
subdata["protocol"]= protocol.toStdString();
subdata["service"]= m_serviceType.toStdString();
subdata["port"]= port_in;
subdata["ip"]= destination_ip.toStdString();
auto text= QByteArray::fromStdString(inja::render(loadFile(key::envelop).toStdString(), subdata));
QNetworkRequest request;
request.setUrl(QUrl(m_controlUrl));
QHttpHeaders headers;
headers.append(QHttpHeaders::WellKnownHeader::ContentType, "text/xml; charset=\"utf-8\"");
headers.append("SOAPAction", QString("\"%1#AddPortMapping\"").arg(m_serviceType));
request.setHeaders(headers);
m_manager.post(request, text);
}
Finally, just check the answer
There is no error in the answer, it works, the situation changes NAT_ADDOtherwise, the status changes to an error,
void UpnpNat::processAnswer(QNetworkReply* reply)
{
if(reply->error() != QNetworkReply::NoError)
{
setError(tr("Something went wrong: %1").arg(reply->errorString()));
setStatus(NAT_STAT::NAT_ERROR);
return;
}
setStatus(NAT_STAT::NAT_ADD);
}
Don’t hesitate to test it on your device. Just to verify, it works everywhere. Any comments or change requests, please use Github for that.
source code
<a href
Найкращі казіно з бонусами — депозитні акції, бездепозитні пропозиції та турніри із призами. Огляди та порівняння умов участі.