aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/upnp/WanConnectionService.cpp
blob: efbe87b73eeafa16e378d63981b17629dada56ee (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//=============================================================================
//
//   File : WanConnectionService.cpp
//   Creation date : Fri Aug 08 18:00:00 2008 GMT by Fabio Bas
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 2008 Fabio Bas (ctrlaltca at gmail dot com)
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your option) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//   Original Copyright follows:
//=============================================================================

/***************************************************************************
                          WanConnectionService.cpp -  description
                             -------------------
    begin                : Mon Jul 25 2005
    copyright            : (C) 2005 by Diederik van der Boor
    email                : vdboor --at-- codingdomain.com
 ***************************************************************************/

#include "WanConnectionService.h"
#include <QDebug>

namespace UPnP
{

	// The constructor
	WanConnectionService::WanConnectionService(const ServiceParameters & params)
	    : Service(params), m_bNatEnabled(false)
	{
	}

	// The destructor
	WanConnectionService::~WanConnectionService()
	    = default;

	// Add a port mapping
	void WanConnectionService::addPortMapping(const QString & protocol, const QString & remoteHost, int externalPort,
	    const QString & internalClient, int internalPort, const QString & description,
	    bool enabled, int leaseDuration)
	{
		QMap<QString, QString> arguments;
		arguments["NewProtocol"] = protocol;
		arguments["NewRemoteHost"] = remoteHost;
		arguments["NewExternalPort"] = QString::number(externalPort);
		arguments["NewInternalClient"] = internalClient;
		arguments["NewInternalPort"] = QString::number(internalPort);
		arguments["NewPortMappingDescription"] = description;
		arguments["NewEnabled"] = QString::number(enabled ? 1 : 0);
		arguments["NewLeaseDuration"] = QString::number(leaseDuration);
		callAction("AddPortMapping", arguments, "m");
	}

	// Delete a port mapping
	void WanConnectionService::deletePortMapping(const QString & protocol, const QString & remoteHost, int externalPort)
	{
		QMap<QString, QString> arguments;
		arguments["NewProtocol"] = protocol;
		arguments["NewRemoteHost"] = remoteHost;
		arguments["NewExternalPort"] = QString::number(externalPort);
		callAction("DeletePortMapping", arguments, "m");
	}

	// Return the external IP address
	QString WanConnectionService::getExternalIpAddress() const
	{
		return m_szExternalIpAddress;
	}

	// Return true if NAT is enabled
	bool WanConnectionService::getNatEnabled() const
	{
		return m_bNatEnabled;
	}

	// Return the port mappings
	const KviPointerList<PortMapping> & WanConnectionService::getPortMappings() const
	{
		return m_lPortMappings;
	}

	// The control point received a response to callAction()
	void WanConnectionService::gotActionResponse(const QString & responseType, const QMap<QString, QString> & resultValues)
	{
		qDebug() << "UPnP::WanConnectionService: parsing action response:"
		         << " type='" << responseType << "'." << endl;

		// Check the message type
		if(responseType == "GetExternalIPAddressResponse")
		{
			// Get the external IP address from the response
			m_szExternalIpAddress = resultValues["NewExternalIPAddress"];

			qDebug() << "UPnP::WanConnectionService: externalIp='" << m_szExternalIpAddress << "'." << endl;
		}
		else if(responseType == "GetNATRSIPStatusResponse")
		{
			// Get the nat status from the response
			m_bNatEnabled = (resultValues["NewNATEnabled"] == "1");

			qDebug() << "UPnP::WanConnectionService: natEnabled=" << m_bNatEnabled << "." << endl;
		}
		else if(responseType == "GetGenericPortMappingEntryResponse")
		{
			// Find a place to store the data
			PortMapping * map = new PortMapping;

			// Get the port mapping data from the response
			map->enabled = (resultValues["NewEnabled"] == "1");
			map->externalPort = resultValues["NewExternalPort"].toInt();
			map->internalClient = resultValues["NewInternalClient"];
			map->internalPort = resultValues["NewInternalPort"].toInt();
			map->leaseDuration = resultValues["NewLeaseDuration"].toInt();
			map->description = resultValues["NewPortMappingDescription"];
			map->protocol = resultValues["NewProtocol"];
			map->remoteHost = resultValues["NewRemoteHost"];

			// Register the mapping
			m_lPortMappings.append(map);

			qDebug() << "UPnP::WanConnectionService - Received mapping: " << map->protocol << " " << map->remoteHost << ":" << map->externalPort
			         << " to " << map->internalClient << ":" << map->internalPort
			         << "    max " << map->leaseDuration << "s '" << map->description << "' " << (map->enabled ? "enabled" : "disabled") << endl;
		}
		else if(responseType == "AddPortMappingResponse")
		{
			qDebug() << "UPnP::WanConnectionService - Received mapping enabled" << endl;
		}
		else if(responseType == "DeletePortMappingResponse")
		{
			qDebug() << "UPnP::WanConnectionService - Received mapping disabled" << endl;
		}
		else
		{
			qDebug() << "UPnP::WanConnectionService - Unexpected response type"
			         << " '" << responseType << "' encountered." << endl;
		}
	}

	// Query for the external IP address
	void WanConnectionService::queryExternalIpAddress()
	{
		callAction("GetExternalIPAddress", "u");
	}

	// Query for the Nat status
	void WanConnectionService::queryNatEnabled()
	{
		callAction("GetNATRSIPStatus", "u");
	}

	// Query for a port mapping entry
	void WanConnectionService::queryPortMappingEntry(int index)
	{
		QMap<QString, QString> arguments;
		arguments["NewPortMappingIndex"] = QString::number(index);
		callAction("GetGenericPortMappingEntry", arguments, "m");
	}

} // End of namespace