summaryrefslogtreecommitdiff
path: root/src/modules/netlib/netlibbind.cpp
blob: 86bbc70b20001f434c1bdf98aca5ba562448bbc2 (plain)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2012 Miranda ICQ/IM project, 
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#include "..\..\core\commonheaders.h"
#include "netlib.h"

bool BindSocketToPort(const char *szPorts, SOCKET s, SOCKET s6, int* portn)
{
	SOCKADDR_IN sin = {0};
	sin.sin_family = AF_INET;

	SOCKADDR_IN6 sin6 = {0};
	sin6.sin6_family = AF_INET6;

	mir_cslock lck(csNetlibUser);

	if (--*portn < 0 && (s != INVALID_SOCKET || s6 != INVALID_SOCKET)) {
		BindSocketToPort(szPorts, INVALID_SOCKET, INVALID_SOCKET, portn);
		if (*portn == 0)
			return false;

		WORD num;
		CallService(MS_UTILS_GETRANDOM, sizeof(WORD), (LPARAM)&num);
		*portn = num % *portn;
	}

	bool before = false;
	while (true) {
		const char *psz;
		char *pszEnd;
		int portMin, portMax, port, portnum = 0;

		for (psz = szPorts;*psz;) {
			while (*psz == ' ' || *psz == ',') psz++;
			portMin = strtol(psz, &pszEnd, 0);
			if (pszEnd == psz)
				break;
			while (*pszEnd == ' ')
				pszEnd++;
			if (*pszEnd == '-') {
				psz = pszEnd + 1;
				portMax = strtol(psz, &pszEnd, 0);
				if (pszEnd == psz) portMax = 65535;
				if (portMin > portMax) {
					port = portMin;
					portMin = portMax;
					portMax = port;
				}
			}
			else portMax = portMin;
			if (portMax >= 1) {
				if (portMin <= 0) portMin = 1;
				for (port = portMin; port <= portMax; port++) {
					if (port > 65535)
						break;

					++portnum;

					if (s == INVALID_SOCKET) continue;
					if ( !before && portnum <= *portn) continue;
					if (before  && portnum >= *portn) 
						return false;

					sin.sin_port = htons((WORD)port);
					bool bV4Mapped = s == INVALID_SOCKET || bind(s, (SOCKADDR*)&sin, sizeof(sin)) == 0;

					sin6.sin6_port = htons((WORD)port);
					bool bV6Mapped = s6 == INVALID_SOCKET || bind(s6, (PSOCKADDR)&sin6, sizeof(sin6)) == 0;

					if (bV4Mapped && bV6Mapped) {
						*portn = portnum + 1;
						return true;
					}
				}
			}
			psz = pszEnd;
		}

		if (*portn < 0) {
			*portn = portnum;
			return true;
		}

		if (*portn >= portnum)
			*portn = 0;
		else
			before = true;
	}
}

int NetlibFreeBoundPort(struct NetlibBoundPort *nlbp)
{
	closesocket(nlbp->s);
	closesocket(nlbp->s6);
	WaitForSingleObject(nlbp->hThread, INFINITE);
	CloseHandle(nlbp->hThread);
	NetlibLogf(nlbp->nlu, "(%u) Port %u closed for incoming connections", nlbp->s, nlbp->wPort);
	mir_free(nlbp);
	return 1;
}

static unsigned __stdcall NetlibBindAcceptThread(void* param)
{
	SOCKET s;
	SOCKADDR_INET_M sin;
	int sinLen;
	struct NetlibConnection *nlc;
	struct NetlibBoundPort *nlbp = (NetlibBoundPort*)param;

	NetlibLogf(nlbp->nlu, "(%u) Port %u opened for incoming connections", nlbp->s, nlbp->wPort);
	for (;;)
	{
		fd_set r;
		FD_ZERO(&r);
		FD_SET(nlbp->s, &r);
		FD_SET(nlbp->s6, &r);

		if (select(0, &r, NULL, NULL, NULL) == SOCKET_ERROR)
			break;

		sinLen = sizeof(sin);
		memset(&sin, 0, sizeof(sin));

		if (FD_ISSET(nlbp->s, &r))
		{
			s = accept(nlbp->s, (struct sockaddr*)&sin, &sinLen);
			if (s == INVALID_SOCKET) break;
		}
		else if (FD_ISSET(nlbp->s6, &r))
		{
			s = accept(nlbp->s6, (struct sockaddr*)&sin, &sinLen);
			if (s == INVALID_SOCKET) break;
		}

		char *szHostA = NetlibAddressToString(&sin);
		NetlibLogf(nlbp->nlu, "New incoming connection on port %u from %s (%p)", nlbp->wPort, szHostA, s);
		mir_free(szHostA);
		nlc = (NetlibConnection*)mir_calloc(sizeof(NetlibConnection));
		nlc->handleType = NLH_CONNECTION;
		nlc->nlu = nlbp->nlu;
		nlc->s = s;
		nlc->s2 = INVALID_SOCKET;
		InitializeCriticalSection(&nlc->csHttpSequenceNums);
		nlc->hOkToCloseEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
		NetlibInitializeNestedCS(&nlc->ncsSend);
		NetlibInitializeNestedCS(&nlc->ncsRecv);

		if (nlbp->pfnNewConnectionV2)
			nlbp->pfnNewConnectionV2(nlc, ntohl(sin.Ipv4.sin_addr.S_un.S_addr), nlbp->pExtra);
	}
	NetlibUPnPDeletePortMapping(nlbp->wExPort, "TCP");
	return 0;
}

INT_PTR NetlibBindPort(WPARAM wParam, LPARAM lParam)
{
	NETLIBBIND *nlb = (NETLIBBIND*)lParam;
	struct NetlibUser *nlu = (struct NetlibUser*)wParam;
	struct NetlibBoundPort *nlbp;
	SOCKADDR_IN sin = {0};
	SOCKADDR_IN6 sin6 = {0};
	int foundPort = 0;
	UINT dwThreadId;

	if (GetNetlibHandleType(nlu) != NLH_USER || !(nlu->user.flags & NUF_INCOMING) ||
		nlb == NULL || nlb->pfnNewConnection == NULL)
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return 0;
	}
	if (nlb->cbSize != sizeof(NETLIBBIND))
		return 0;

	nlbp = (NetlibBoundPort*)mir_calloc(sizeof(NetlibBoundPort));
	nlbp->handleType = NLH_BOUNDPORT;
	nlbp->nlu = nlu;
	nlbp->pfnNewConnectionV2 = nlb->pfnNewConnectionV2;

	nlbp->s = socket(PF_INET, SOCK_STREAM, 0);
	nlbp->s6 = socket(PF_INET6, SOCK_STREAM, 0);
	nlbp->pExtra = (nlb->cbSize != NETLIBBIND_SIZEOF_V1) ? nlb->pExtra : NULL;
	if (nlbp->s == INVALID_SOCKET && nlbp->s6 == INVALID_SOCKET)
	{
		NetlibLogf(nlu, "%s %d: %s() failed (%u)", __FILE__, __LINE__, "socket", WSAGetLastError());
		mir_free(nlbp);
		return 0;
	}
	sin.sin_family = AF_INET;
	sin6.sin6_family = AF_INET6;

	/* if the netlib user wanted a free port given in the range, then
	they better have given wPort == 0, let's hope so */
	if (nlu->settings.specifyIncomingPorts && nlu->settings.szIncomingPorts && nlb->wPort == 0)
	{
		if ( !BindSocketToPort(nlu->settings.szIncomingPorts, nlbp->s, nlbp->s6, &nlu->outportnum))
		{
			NetlibLogf(nlu, "Netlib bind: Not enough ports for incoming connections specified");
			SetLastError(WSAEADDRINUSE);
		}
		else
			foundPort = 1;
	}
	else
	{
		/* if ->wPort == 0 then they'll get any free port, otherwise they'll
		be asking for whatever was in nlb->wPort*/
		if (nlb->wPort != 0)
		{
			NetlibLogf(nlu, "%s %d: trying to bind port %d, this 'feature' can be abused, please be sure you want to allow it.", __FILE__, __LINE__, nlb->wPort);
			sin.sin_port = htons(nlb->wPort);
			sin6.sin6_port = htons(nlb->wPort);
		}
		if (bind(nlbp->s, (PSOCKADDR)&sin, sizeof(sin)) == 0)
		{
			SOCKADDR_IN sin = {0};
			int len = sizeof(sin);
			if ( !getsockname(nlbp->s, (PSOCKADDR)&sin, &len))
				sin6.sin6_port = sin.sin_port;
			foundPort = 1;
		}

		if (bind(nlbp->s6, (PSOCKADDR)&sin6, sizeof(sin6)) == 0)
			foundPort = 1;
	}
	if ( !foundPort)
	{
		NetlibLogf(nlu, "%s %d: %s() failed (%u)", __FILE__, __LINE__, "bind", WSAGetLastError());
		closesocket(nlbp->s);
		closesocket(nlbp->s6);
		mir_free(nlbp);
		return 0;
	}

	if (nlbp->s != INVALID_SOCKET && listen(nlbp->s, 5))
	{
		NetlibLogf(nlu, "%s %d: %s() failed (%u)", __FILE__, __LINE__, "listen", WSAGetLastError());
		closesocket(nlbp->s);
		closesocket(nlbp->s6);
		mir_free(nlbp);
		return 0;
	}

	if (nlbp->s6 != INVALID_SOCKET && listen(nlbp->s6, 5))
	{
		NetlibLogf(nlu, "%s %d: %s() failed (%u)", __FILE__, __LINE__, "listen", WSAGetLastError());
		closesocket(nlbp->s);
		closesocket(nlbp->s6);
		mir_free(nlbp);
		return 0;
	}

	{
		SOCKADDR_INET_M sin = {0};
		int len = sizeof(sin);
		if ( !getsockname(nlbp->s, (PSOCKADDR)&sin, &len))
		{
			nlb->wPort = ntohs(sin.Ipv4.sin_port);
			nlb->dwInternalIP = ntohl(sin.Ipv4.sin_addr.S_un.S_addr);
		}
		else if ( !getsockname(nlbp->s6, (PSOCKADDR)&sin, &len))
			nlb->wPort = ntohs(sin.Ipv6.sin6_port);
		else
		{
			NetlibLogf(nlu, "%s %d: %s() failed (%u)", __FILE__, __LINE__, "getsockname", WSAGetLastError());
			closesocket(nlbp->s);
			closesocket(nlbp->s6);
			mir_free(nlbp);
			return 0;
		}
		nlbp->wPort = nlb->wPort;

		if (nlb->dwInternalIP == 0)
		{
			char hostname[64] = "";
			gethostname(hostname, SIZEOF(hostname));

			PHOSTENT he = gethostbyname(hostname);
			if (he && he->h_addr)
				nlb->dwInternalIP = ntohl(*(PDWORD)he->h_addr);
		}

		DWORD extIP;
		if (nlu->settings.enableUPnP && 
			NetlibUPnPAddPortMapping(nlb->wPort, "TCP", &nlbp->wExPort, &extIP, nlb->cbSize > NETLIBBIND_SIZEOF_V2))
		{
			NetlibLogf(NULL, "UPnP port mapping succeeded. Internal Port: %u External Port: %u\n", 
				nlb->wPort, nlbp->wExPort);
			if (nlb->cbSize > NETLIBBIND_SIZEOF_V2)
			{
				nlb->wExPort = nlbp->wExPort;
				nlb->dwExternalIP = extIP;
			}
		}
		else
		{
			if (nlu->settings.enableUPnP)
				NetlibLogf(NULL, "UPnP port mapping failed. Internal Port: %u\n", nlb->wPort);
			else
				NetlibLogf(NULL, "UPnP disabled. Internal Port: %u\n", nlb->wPort);

			nlbp->wExPort = 0;
			if (nlb->cbSize > NETLIBBIND_SIZEOF_V2)
			{
				nlb->wExPort = nlb->wPort;
				nlb->dwExternalIP = nlb->dwInternalIP;
			}
		}
	}

	nlbp->hThread = (HANDLE)mir_forkthreadex(NetlibBindAcceptThread, nlbp, &dwThreadId);
	return (INT_PTR)nlbp;
}