summaryrefslogtreecommitdiffstats
path: root/pica-client/directlistener.cpp
blob: d2a1fc7228e4851bcf23c9bf2f000d55fb1f8a1f (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
	(c) Copyright  2012 - 2026 Anton Sviridenko
	https://picapica.im

	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, version 3.

	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, see <https://www.gnu.org/licenses/>.
*/
#include "directlistener.h"
#include "directc2csettings.h"
#include "../PICA_client.h"
#include "../PICA_netconf.h"
#include <QByteArray>
#include <QList>
#include <vector>

// How soon a lost mapping is tried again, in seconds.
static const int kUpnpRetryInterval = 300;

DirectListener::DirectListener(QObject *parent)
	: QObject(parent), upnp(nullptr), upnpPort(0), upnpLost(false), refreshTimer(nullptr)
{
}

DirectListener::~DirectListener()
{
	// release() is expected to have run on this object's own thread already;
	// this is only for a mapping left behind when it did not
	PICA_upnp_free(upnp);
}

bool DirectListener::discoverUpnp()
{
	char addr[64];
	int ret;

	ret = PICA_upnp_discover(PICA_UPNP_DISCOVER_TIMEOUT, &upnp);

	if (ret != PICA_UPNP_OK)
	{
		upnp = nullptr;
		emit statusMsg(tr("UPnP: %1").arg(PICA_upnp_strerror(ret)), false);
		return false;
	}

	// An external address that is private - the router is behind the
	// provider's NAT - would lead nowhere; better not to forward anything
	// than to announce it.
	ret = PICA_upnp_external_addr(upnp, addr, sizeof(addr));

	if (ret != PICA_UPNP_OK)
	{
		emit statusMsg(tr("UPnP: %1").arg(PICA_upnp_strerror(ret)), false);
		dropUpnp();
		return false;
	}

	upnpAddr = QString::fromLatin1(addr);

	return true;
}

void DirectListener::dropUpnp()
{
	PICA_upnp_free(upnp);
	upnp = nullptr;
	upnpAddr.clear();
	upnpPort = 0;
}

void DirectListener::setup(quintptr accp, int addrConfig, bool useUpnp, int localPort, QString addrList)
{
	const struct PICA_acc *acc = (const struct PICA_acc*)accp;
	struct PICA_listener *l = nullptr;
	bool manual = addrConfig == DirectC2CSettings::AddrConfigManual;
	QList<DirectC2CSettings::Addr> addrs = DirectC2CSettings::parseAddrs(addrList);
	// In automatic configuration a port that cannot be had is not worth a
	// word: the next one is tried. The port of the manual one is the user's
	// choice and is used as it is.
	int firstPort = manual ? localPort : (int)DirectC2CSettings::DefaultPort;
	int tries = manual ? 1 : (int)PortTries;
	bool mapped = false;
	int port = 0;
	int i, ret;

	// the mapping made for a listener set up before, if it was not released
	release();

	if (useUpnp)
		discoverUpnp();

	for (i = 0; i < tries && firstPort + i <= 65535; i++)
	{
		port = firstPort + i;

		if (PICA_new_listener(acc, port, &l) != PICA_OK)
		{
			l = nullptr;
			continue;
		}

		if (!upnp)
			break;

		ret = PICA_upnp_map(upnp, port, PICA_UPNP_LEASE_DEFAULT, "Pica Pica");

		if (ret == PICA_UPNP_OK)
		{
			mapped = true;
			break;
		}

		// somebody else behind the router has this port number forwarded
		if (ret == PICA_UPNP_ERRCONFLICT && i + 1 < tries)
		{
			PICA_close_listener(l);
			l = nullptr;
			continue;
		}

		emit statusMsg(tr("UPnP: failed to forward port %1 - %2").arg(port).arg(PICA_upnp_strerror(ret)), false);
		dropUpnp();
		break;
	}

	if (!l)
	{
		if (manual)
			emit statusMsg(tr("Failed to open port %1 for incoming direct connections").arg(firstPort), true);
		else
			emit statusMsg(tr("Failed to open any of the ports %1 - %2 for incoming direct connections")
			               .arg(firstPort).arg(firstPort + tries - 1), true);

		dropUpnp();
		emit ready(0, QString(), 0);
		return;
	}

	if (!mapped)
		dropUpnp();

	if (manual)
	{
		QList<QByteArray> hosts;
		std::vector<const char *> hostPtrs;
		std::vector<int> ports;
		bool hasUpnpEntry = false;

		foreach (const DirectC2CSettings::Addr &a, addrs)
		{
			hosts.append(a.upnp ? QByteArray() : a.host.toUtf8());
			ports.push_back(a.upnp ? port : a.port);
			hasUpnpEntry = hasUpnpEntry || a.upnp;
		}

		// A mapping with nowhere to announce it goes at the end.
		if (mapped && !hasUpnpEntry)
		{
			hosts.append(QByteArray());
			ports.push_back(port);
			addrs.append(DirectC2CSettings::Addr {QString(), port, true});
		}

		for (int j = 0; j < hosts.size(); j++)
			hostPtrs.push_back(addrs[j].upnp ? nullptr : hosts[j].constData());

		// An empty list would make the listener fall back to automatic
		// configuration, announcing addresses the user did not choose.
		if (hostPtrs.empty())
		{
			emit statusMsg(tr("No addresses are configured for incoming direct connections"), true);
			PICA_close_listener(l);
			dropUpnp();
			emit ready(0, QString(), 0);
			return;
		}

		ret = PICA_listener_set_addrlist(l, hostPtrs.data(), ports.data(), (int)hostPtrs.size());

		if (ret != PICA_OK)
		{
			emit statusMsg(tr("Unable to resolve the addresses configured for incoming direct connections"), true);
			PICA_close_listener(l);
			dropUpnp();
			emit ready(0, QString(), 0);
			return;
		}
	}

	if (mapped)
	{
		upnpPort = port;
		upnpLost = false;
		scheduleRefresh();

		emit statusMsg(tr("Port %1 is forwarded by the UPnP router, external address %2").arg(port).arg(upnpAddr), false);
	}

	emit statusMsg(tr("Listening for incoming direct connections on port %1").arg(port), false);

	emit ready((quintptr)l, mapped ? upnpAddr : QString(), mapped ? port : 0);
}

void DirectListener::scheduleRefresh()
{
	unsigned int lease = PICA_upnp_lease(upnp);
	int secs;

	if (!refreshTimer)
	{
		refreshTimer = new QTimer(this);
		refreshTimer->setSingleShot(true);
		connect(refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
	}

	// Renewed at half the lease the router granted. A permanent mapping is
	// checked just as often: the router forgets it when it reboots.
	if (upnpLost)
		secs = kUpnpRetryInterval;
	else if (lease == 0)
		secs = PICA_UPNP_LEASE_DEFAULT / 2;
	else
		secs = (int)(lease / 2);

	if (secs < 30)
		secs = 30;

	refreshTimer->start(secs * 1000);
}

void DirectListener::refresh()
{
	char addr[64];
	int ret;

	if (!upnp)
		return;

	if (PICA_upnp_mapped(upnp))
		ret = PICA_upnp_refresh(upnp);
	else
		ret = PICA_upnp_map(upnp, upnpPort, PICA_UPNP_LEASE_DEFAULT, "Pica Pica");

	// The router's external address can change on a renewal of its own
	// lease with the provider, and is read again every time.
	if (ret == PICA_UPNP_OK)
		ret = PICA_upnp_external_addr(upnp, addr, sizeof(addr));

	if (ret == PICA_UPNP_OK)
	{
		QString newAddr = QString::fromLatin1(addr);

		if (upnpLost || newAddr != upnpAddr)
		{
			upnpAddr = newAddr;
			emit upnpChanged(upnpAddr, upnpPort);
		}

		upnpLost = false;
	}
	else if (!upnpLost)
	{
		upnpLost = true;
		emit upnpChanged(QString(), 0);
		emit statusMsg(tr("UPnP: the forwarding of port %1 was lost - %2").arg(upnpPort).arg(PICA_upnp_strerror(ret)), false);
	}

	scheduleRefresh();
}

void DirectListener::release()
{
	if (refreshTimer)
		refreshTimer->stop();

	dropUpnp();
}