summaryrefslogtreecommitdiff
path: root/protocols/IcqOscarJ/src/icq_server.cpp
blob: 5c7d5368eff0ceab2758d9121febfc0fc0beefa6 (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// ---------------------------------------------------------------------------80
//                ICQ plugin for Miranda Instant Messenger
//                ________________________________________
//
// Copyright © 2000-2001 Richard Hughes, Roland Rabien, Tristan Van de Vreede
// Copyright © 2001-2002 Jon Keating, Richard Hughes
// Copyright © 2002-2004 Martin Öberg, Sam Kothari, Robert Rainwater
// Copyright © 2004-2010 Joe Kucera
// Copyright © 2012-2014 Miranda NG Team
//
// 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.
// -----------------------------------------------------------------------------
//  DESCRIPTION:
//
//  Manages main server connection, low-level communication
// -----------------------------------------------------------------------------

#include "icqoscar.h"

void icq_newConnectionReceived(HANDLE hNewConnection, DWORD dwRemoteIP, void *pExtra);

/////////////////////////////////////////////////////////////////////////////////////////
// ICQ Server thread

void __cdecl CIcqProto::ServerThread(serverthread_start_info *infoParam)
{
	serverthread_info info = { 0 };
	info.isLoginServer = info.bReinitRecver = true;
	info.wAuthKeyLen = infoParam->wPassLen;
	null_strcpy((char*)info.szAuthKey, infoParam->szPass, info.wAuthKeyLen);
	// store server port
	info.wServerPort = infoParam->nloc.wPort;

	srand(time(NULL));

	ResetSettingsOnConnect();

	// Connect to the login server
	debugLogA("Authenticating to server");
	{
		NETLIBOPENCONNECTION nloc = infoParam->nloc;
		nloc.timeout = 6;
		if (m_bGatewayMode)
			nloc.flags |= NLOCF_HTTPGATEWAY;

		hServerConn = NetLib_OpenConnection(m_hNetlibUser, NULL, &nloc);

		SAFE_FREE((void**)&nloc.szHost);
		SAFE_FREE((void**)&infoParam);

		if (hServerConn && m_bSecureConnection) {
			if (!CallService(MS_NETLIB_STARTSSL, (WPARAM)hServerConn, 0)) {
				icq_LogMessage(LOG_ERROR, LPGEN("Unable to connect to ICQ login server, SSL could not be negotiated"));
				SetCurrentStatus(ID_STATUS_OFFLINE);
				NetLib_CloseConnection(&hServerConn, TRUE);
				return;
			}
		}
	}

	// Login error
	if (hServerConn == NULL) {
		DWORD dwError = GetLastError();

		SetCurrentStatus(ID_STATUS_OFFLINE);

		icq_LogUsingErrorCode(LOG_ERROR, dwError, LPGEN("Unable to connect to ICQ login server"));
		return;
	}

	// Initialize direct connection ports
	DWORD dwInternalIP;
	BYTE bConstInternalIP = getByte("ConstRealIP", 0);

	info.hDirectBoundPort = NetLib_BindPort(icq_newConnectionReceived, this, &wListenPort, &dwInternalIP);
	if (!info.hDirectBoundPort) {
		icq_LogUsingErrorCode(LOG_WARNING, GetLastError(), LPGEN("Miranda was unable to allocate a port to listen for direct peer-to-peer connections between clients. You will be able to use most of the ICQ network without problems but you may be unable to send or receive files.\n\nIf you have a firewall this may be blocking Miranda, in which case you should configure your firewall to leave some ports open and tell Miranda which ports to use in M->Options->ICQ->Network."));
		wListenPort = 0;
		if (!bConstInternalIP)
			delSetting("RealIP");
	}
	else if (!bConstInternalIP)
		setDword("RealIP", dwInternalIP);

	// Initialize rate limiting queues
	{
		icq_lock l(m_ratesMutex);
		m_ratesQueue_Request = new rates_queue(this, "request", RML_IDLE_30, RML_IDLE_50, 1);
		m_ratesQueue_Response = new rates_queue(this, "response", RML_IDLE_10, RML_IDLE_30, -1);
	}

	// This is the "infinite" loop that receives the packets from the ICQ server
	NETLIBPACKETRECVER packetRecv;
	info.hPacketRecver = (HANDLE)CallService(MS_NETLIB_CREATEPACKETRECVER, (WPARAM)hServerConn, 0x2400);

	while (serverThreadHandle) {
		if (info.bReinitRecver) { // we reconnected, reinit struct
			info.bReinitRecver = false;
			ZeroMemory(&packetRecv, sizeof(packetRecv));
			packetRecv.cbSize = sizeof(packetRecv);
			packetRecv.dwTimeout = 1000;
		}

		int recvResult = CallService(MS_NETLIB_GETMOREPACKETS, (WPARAM)info.hPacketRecver, (LPARAM)&packetRecv);
		if (recvResult == 0) {
			debugLogA("Clean closure of server socket");
			break;
		}

		if (recvResult == SOCKET_ERROR) {
			DWORD dwError = GetLastError();
			if (dwError == ERROR_TIMEOUT) {
				if (m_iDesiredStatus == ID_STATUS_OFFLINE)
					break;

				CheckKeepAlive(&info);
				continue;
			}
			if (dwError == WSAESHUTDOWN) // ok, we're going offline
				break;
			
			debugLogA("Abortive closure of server socket, error: %d", dwError);
			break;
		}

		// Deal with the packet
		packetRecv.bytesUsed = handleServerPackets(packetRecv.buffer, packetRecv.bytesAvailable, &info);
	}
	serverThreadHandle = NULL;

	// Time to shutdown
	debugLogA("Closing server connections...");
	NetLib_CloseConnection(&hServerConn, TRUE);

	// Close the packet receiver (connection may still be open)
	NetLib_SafeCloseHandle(&info.hPacketRecver);

	// Close DC port
	NetLib_SafeCloseHandle(&info.hDirectBoundPort);

	// disable auto info-update thread
	icq_EnableUserLookup(FALSE);

	if (m_iStatus != ID_STATUS_OFFLINE && m_iDesiredStatus != ID_STATUS_OFFLINE) {
		if (!info.bLoggedIn)
			icq_LogMessage(LOG_FATAL, LPGEN("Connection failed.\nLogin sequence failed for unknown reason.\nTry again later."));

		// set flag indicating we were kicked out
		m_bConnectionLost = true;

		SetCurrentStatus(ID_STATUS_OFFLINE);
	}

	// signal keep-alive thread to stop
	StopKeepAlive(&info);

	// Close all open DC connections
	CloseContactDirectConns(NULL);

	// Close avatar connection if any
	StopAvatarThread();

	// Offline all contacts
	for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) {
		if (getContactStatus(hContact) == ID_STATUS_OFFLINE)
			continue;
				
		setWord(hContact, "Status", ID_STATUS_OFFLINE);

		DWORD dwUIN;
		uid_str szUID;
		if (!getContactUid(hContact, &dwUIN, &szUID)) {
			char tmp = 0;
			handleXStatusCaps(dwUIN, szUID, hContact, (BYTE*)&tmp, 0, &tmp, 0);
		}
	}

	setDword("LogonTS", 0); // clear logon time

	servlistPendingFlushOperations(); // clear pending operations list

	// release rates queues
	{
		icq_lock l(m_ratesMutex);
		delete m_ratesQueue_Request; m_ratesQueue_Request = NULL;
		delete m_ratesQueue_Response;	m_ratesQueue_Response = NULL;
		delete m_rates; m_rates = NULL;
	}

	FlushServerIDs();         // clear server IDs list

	debugLogA("%s thread ended.", "Server");
}

void CIcqProto::icq_serverDisconnect()
{
	if (!hServerConn)
		return;

	debugLogA("Server shutdown requested");
	Netlib_Shutdown(hServerConn);

	debugLogA("Dropping server thread");
	if (serverThreadHandle) {
		debugLogA("Closing server thread handle: %08p", serverThreadHandle);
		CloseHandle(serverThreadHandle);
		serverThreadHandle = NULL;
	}

	SetCurrentStatus(ID_STATUS_OFFLINE);
}

int CIcqProto::handleServerPackets(BYTE *buf, int len, serverthread_info *info)
{
	BYTE channel;
	WORD sequence;
	WORD datalen;
	int bytesUsed = 0;

	while (len > 0)
	{
		if (info->bReinitRecver)
			break;

		// All FLAPS begin with 0x2a
		if (*buf++ != FLAP_MARKER)
			break;

		if (len < 6)
			break;

		unpackByte(&buf, &channel);
		unpackWord(&buf, &sequence);
		unpackWord(&buf, &datalen);

		if (len < 6 + datalen)
			break;

		debugLogA("Server FLAP: Channel %u, Seq %u, Length %u bytes", channel, sequence, datalen);

		switch (channel) {
		case ICQ_LOGIN_CHAN:
			handleLoginChannel(buf, datalen, info);
			break;

		case ICQ_DATA_CHAN:
			handleDataChannel(buf, datalen, info);
			break;

		case ICQ_ERROR_CHAN:
			handleErrorChannel(buf, datalen);
			break;

		case ICQ_CLOSE_CHAN:
			handleCloseChannel(buf, datalen, info);
			break; // we need this for walking thru proxy

		case ICQ_PING_CHAN:
			handlePingChannel(buf, datalen);
			break;

		default:
			debugLogA("Warning: Unhandled Server FLAP Channel: Channel %u, Seq %u, Length %u bytes", channel, sequence, datalen);
			break;
		}

		/* Increase pointers so we can check for more FLAPs */
		buf += datalen;
		len -= (datalen + 6);
		bytesUsed += (datalen + 6);
	}

	return bytesUsed;
}


void CIcqProto::sendServPacket(icq_packet *pPacket)
{
	// make sure to have the connection handle
	connectionHandleMutex->Enter();

	if (hServerConn) {
		int nSendResult;

		// This critsec makes sure that the sequence order doesn't get screwed up
		localSeqMutex->Enter();

		// :IMPORTANT:
		// The FLAP sequence must be a WORD. When it reaches 0xFFFF it should wrap to
		// 0x0000, otherwise we'll get kicked by server.
		wLocalSequence++;

		// Pack sequence number
		pPacket->pData[2] = ((wLocalSequence & 0xff00) >> 8);
		pPacket->pData[3] = (wLocalSequence & 0x00ff);

		nSendResult = Netlib_Send(hServerConn, (const char *)pPacket->pData, pPacket->wLen, 0);

		localSeqMutex->Leave();
		connectionHandleMutex->Leave();

		// Send error
		if (nSendResult == SOCKET_ERROR) {
			DWORD dwErrorCode = GetLastError();
			if (dwErrorCode != WSAESHUTDOWN)
				icq_LogUsingErrorCode(LOG_ERROR, GetLastError(), LPGEN("Your connection with the ICQ server was abortively closed"));
			icq_serverDisconnect();
		}
		else { // Rates management
			icq_lock l(m_ratesMutex);
			m_rates->packetSent(pPacket);
		}

	}
	else {
		connectionHandleMutex->Leave();
		debugLogA("Error: Failed to send packet (no connection)");
	}

	SAFE_FREE((void**)&pPacket->pData);
}


void __cdecl CIcqProto::SendPacketAsyncThread(icq_packet* pkt)
{
	sendServPacket( pkt );
	SAFE_FREE((void**)&pkt);
}


void CIcqProto::sendServPacketAsync(icq_packet *packet)
{
	icq_packet *pPacket;

	pPacket = (icq_packet*)SAFE_MALLOC(sizeof(icq_packet)); // This will be freed in the new thread
	memcpy(pPacket, packet, sizeof(icq_packet));

	ForkThread((MyThreadFunc)&CIcqProto::SendPacketAsyncThread, pPacket);
}


int CIcqProto::IsServerOverRate(WORD wFamily, WORD wCommand, int nLevel)
{
	icq_lock l(m_ratesMutex);

	if (m_rates) {
		WORD wGroup = m_rates->getGroupFromSNAC(wFamily, wCommand);

		// check if the rate is not over specified level
		if (m_rates->getNextRateLevel(wGroup) < m_rates->getLimitLevel(wGroup, nLevel))
			return TRUE;
	}

	return FALSE;
}


/////////////////////////////////////////////////////////////////////////////////////////
// ICQ Server thread

void CIcqProto::icq_login(const char* szPassword)
{
	DWORD dwUin = getContactUin(NULL);
	serverthread_start_info* stsi = (serverthread_start_info*)SAFE_MALLOC(sizeof(serverthread_start_info));

	// Server host name
	char szServer[MAX_PATH];
	if (getSettingStringStatic(NULL, "OscarServer", szServer, MAX_PATH))
		stsi->nloc.szHost = null_strdup(m_bSecureConnection ? DEFAULT_SERVER_HOST_SSL : DEFAULT_SERVER_HOST);
	else
		stsi->nloc.szHost = null_strdup(szServer);

	// Server port
	stsi->nloc.wPort = getWord("OscarPort", m_bSecureConnection ? DEFAULT_SERVER_PORT_SSL : DEFAULT_SERVER_PORT);
	if (stsi->nloc.wPort == 0)
		stsi->nloc.wPort = RandRange(1024, 65535);

	// User password
	stsi->wPassLen = strlennull(szPassword);
	if (stsi->wPassLen > 8) stsi->wPassLen = 8;
	null_strcpy(stsi->szPass, szPassword, stsi->wPassLen);

	// Randomize sequence
	wLocalSequence = generate_flap_sequence();

	m_dwLocalUIN = dwUin;

	// Initialize members
	m_avatarsConnectionPending = TRUE;

	serverThreadHandle = ForkThreadEx((MyThreadFunc)&CIcqProto::ServerThread, stsi, &serverThreadId);
}