summaryrefslogtreecommitdiff
path: root/protocols/MSN/src/msn_ftold.cpp
blob: bdb3ebd46f7c1dea2f96d03d2b52b0b61f6a9dbe (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
/*
Plugin of Miranda IM for communicating with users of the MSN Messenger protocol.

Copyright (c) 2012-2014 Miranda NG Team
Copyright (c) 2006-2012 Boris Krasnovskiy.
Copyright (c) 2003-2005 George Hazan.
Copyright (c) 2002-2003 Richard Hughes (original version).

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, see <http://www.gnu.org/licenses/>.
*/

#include "stdafx.h"
#include "msn_proto.h"
#ifdef OBSOLETE

void CMsnProto::msnftp_sendAcceptReject(filetransfer *ft, bool acc)
{
	ThreadData* thread = MSN_GetThreadByContact(ft->p2p_dest);
	if (thread == NULL) return;

	if (acc) {
		thread->sendPacket("MSG",
			"U %d\r\nMIME-Version: 1.0\r\n"
			"Content-Type: text/x-msmsgsinvite; charset=UTF-8\r\n\r\n"
			"Invitation-Command: ACCEPT\r\n"
			"Invitation-Cookie: %s\r\n"
			"Launch-Application: FALSE\r\n"
			"Request-Data: IP-Address:\r\n\r\n",
			172 + 4 + mir_strlen(ft->szInvcookie), ft->szInvcookie);
	}
	else {
		thread->sendPacket("MSG",
			"U %d\r\nMIME-Version: 1.0\r\n"
			"Content-Type: text/x-msmsgsinvite; charset=UTF-8\r\n\r\n"
			"Invitation-Command: CANCEL\r\n"
			"Invitation-Cookie: %s\r\n"
			"Cancel-Code: REJECT\r\n\r\n",
			172 - 33 + 4 + mir_strlen(ft->szInvcookie), ft->szInvcookie);
	}
}

void CMsnProto::msnftp_invite(filetransfer *ft)
{
	bool isOffline;
	ThreadData* thread = MSN_StartSB(ft->p2p_dest, isOffline);
	if (isOffline) return;
	if (thread != NULL) thread->mMsnFtp = ft;

	TCHAR* pszFiles = _tcsrchr(ft->std.ptszFiles[0], '\\');
	if (pszFiles)
		pszFiles++;
	else
		pszFiles = *ft->std.ptszFiles;

	char msg[1024];
	mir_snprintf(msg,
		"Content-Type: text/x-msmsgsinvite; charset=UTF-8\r\n\r\n"
		"Application-Name: File Transfer\r\n"
		"Application-GUID: {5D3E02AB-6190-11d3-BBBB-00C04F795683}\r\n"
		"Invitation-Command: INVITE\r\n"
		"Invitation-Cookie: %i\r\n"
		"Application-File: %s\r\n"
		"Application-FileSize: %I64u\r\n\r\n",
		MSN_GenRandom(), UTF8(pszFiles), ft->std.currentFileSize);

	if (thread == NULL)
		MsgQueue_Add(ft->p2p_dest, 'S', msg, -1, ft);
	else
		thread->sendMessage('S', NULL, NETID_MSN, msg, MSG_DISABLE_HDR);
}


/////////////////////////////////////////////////////////////////////////////////////////
//	MSN File Transfer Protocol commands processing

int CMsnProto::MSN_HandleMSNFTP(ThreadData *info, char *cmdString)
{
	char* params = "";
	filetransfer* ft = info->mMsnFtp;

	if (cmdString[3])
		params = cmdString + 4;

	switch ((*(PDWORD)cmdString & 0x00FFFFFF) | 0x20000000) {
	case ' EYB':    //********* BYE
		ft->complete();
		return 1;

	case ' LIF':    //********* FIL
		char filesize[30];
		if (sscanf(params, "%s", filesize) < 1)
			goto LBL_InvalidCommand;

		info->mCaller = 1;
		info->send("TFR\r\n", 5);
		break;

	case ' RFT':    //********* TFR
		{
			char *sendpacket = (char*)alloca(2048);
			filetransfer* ft = info->mMsnFtp;

			info->mCaller = 3;

			while (ft->std.currentFileProgress < ft->std.currentFileSize) {
				if (ft->bCanceled) {
					sendpacket[0] = 0x01;
					sendpacket[1] = 0x00;
					sendpacket[2] = 0x00;
					info->send(sendpacket, 3);
					return 0;
				}

				int wPlace = 0;
				sendpacket[wPlace++] = 0x00;
				unsigned __int64 packetLen = ft->std.currentFileSize - ft->std.currentFileProgress;
				if (packetLen > 2045) packetLen = 2045;

				sendpacket[wPlace++] = (char)(packetLen & 0x00ff);
				sendpacket[wPlace++] = (char)((packetLen & 0xff00) >> 8);
				_read(ft->fileId, &sendpacket[wPlace], packetLen);

				info->send(&sendpacket[0], packetLen + 3);

				ft->std.totalProgress += packetLen;
				ft->std.currentFileProgress += packetLen;

				ProtoBroadcastAck(ft->std.hContact, ACKTYPE_FILE, ACKRESULT_DATA, ft, (LPARAM)&ft->std);
			}

			ft->complete();
		}
		break;

	case ' RSU':    //********* USR
		char email[130], authcookie[14];
		if (sscanf(params, "%129s %13s", email, authcookie) < 2) {
			debugLogA("Invalid USR OK command, ignoring");
			break;
		}

		char tCommand[30];
		size_t tCommandLen;
		tCommandLen = mir_snprintf(tCommand, "FIL %i\r\n", info->mMsnFtp->std.totalBytes);
		info->send(tCommand, tCommandLen);
		break;

	case ' REV':    //********* VER
		char protocol1[7];
		if (sscanf(params, "%6s", protocol1) < 1) {
LBL_InvalidCommand:
			debugLogA("Invalid %.3s command, ignoring", cmdString);
			break;
		}

		if (mir_strcmp(protocol1, "MSNFTP") != 0) {
			int tempInt;
			int tFieldCount = sscanf(params, "%d %6s", &tempInt, protocol1);
			if (tFieldCount != 2 || mir_strcmp(protocol1, "MSNFTP") != 0) {
				debugLogA("Another side requested the unknown protocol (%s), closing thread", params);
				return 1;
			}
		}

		if (info->mCaller == 0) { //receive
			char tCommand[MSN_MAX_EMAIL_LEN + 50];
			size_t tCommandLen;
			tCommandLen = mir_snprintf(tCommand, "USR %s %s\r\n", MyOptions.szEmail, info->mCookie);
			info->send(tCommand, tCommandLen);
		}
		else if (info->mCaller == 2) { //send
			static const char sttCommand[] = "VER MSNFTP\r\n";
			info->send(sttCommand, mir_strlen(sttCommand));
		}
		break;

	default:		// receiving file
		HReadBuffer tBuf(info, int(cmdString - info->mData));

		for (;;) {
			if (ft->bCanceled) {
				info->send("CCL\r\n", 5);
				ft->close();
				return 1;
			}

			BYTE* p = tBuf.surelyRead(3);
			if (p == NULL) {
LBL_Error:
				ft->close();
				MSN_ShowError("file transfer is canceled by remote host");
				return 1;
			}

			BYTE tIsTransitionFinished = *p++;
			WORD dataLen = *p++;
			dataLen |= (*p++ << 8);

			if (tIsTransitionFinished) {
LBL_Success:
				static const char sttCommand[] = "BYE 16777989\r\n";
				info->send(sttCommand, mir_strlen(sttCommand));
				return 1;
			}

			p = tBuf.surelyRead(dataLen);
			if (p == NULL)
				goto LBL_Error;

			_write(ft->fileId, p, dataLen);
			ft->std.totalProgress += dataLen;
			ft->std.currentFileProgress += dataLen;

			ProtoBroadcastAck(ft->std.hContact, ACKTYPE_FILE, ACKRESULT_DATA, ft, (LPARAM)&ft->std);

			if (ft->std.currentFileProgress == ft->std.totalBytes) {
				ft->complete();
				goto LBL_Success;
			}
		}
	}

	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
//	ft_startFileSend - sends a file using the old f/t protocol

void __cdecl CMsnProto::msnftp_sendFileThread(void* arg)
{
	ThreadData* info = (ThreadData*)arg;

	debugLogA("Waiting for an incoming connection to '%s'...", info->mServer);

	switch (WaitForSingleObject(info->hWaitEvent, 60000)) {
	case WAIT_TIMEOUT:
	case WAIT_FAILED:
		debugLogA("Incoming connection timed out, closing file transfer");
		return;
	}

	info->mBytesInData = 0;

	for (;;) {
		int recvResult = info->recv(info->mData + info->mBytesInData, 1000 - info->mBytesInData);
		if (recvResult == SOCKET_ERROR || !recvResult)
			break;

		info->mBytesInData += recvResult;

		//pull off each line for parsing
		if (info->mCaller == 3 && info->mType == SERVER_FILETRANS) {
			if (MSN_HandleMSNFTP(info, info->mData))
				break;
		}
		else   // info->mType!=SERVER_FILETRANS
		{
			for (;;) {
				char* peol = strchr(info->mData, '\r');
				if (peol == NULL)
					break;

				if (info->mBytesInData < peol - info->mData + 2)
					break;  //wait for full line end

				char msg[sizeof(info->mData)];
				memcpy(msg, info->mData, peol - info->mData); msg[peol - info->mData] = 0;
				if (*++peol != '\n')
					debugLogA("Dodgy line ending to command: ignoring");
				else
					peol++;

				info->mBytesInData -= peol - info->mData;
				memmove(info->mData, peol, info->mBytesInData);

				debugLogA("RECV:%s", msg);

				if (!isalnum(msg[0]) || !isalnum(msg[1]) || !isalnum(msg[2]) || (msg[3] && msg[3] != ' ')) {
					debugLogA("Invalid command name");
					continue;
				}

				if (MSN_HandleMSNFTP(info, msg))
					break;
			}
		}

		if (info->mBytesInData == info->mDataSize) {
			char *mData = (char*)mir_realloc(info->mData, (info->mDataSize*=2)+1);
			if (mData) info->mData = mData;  else {
				debugLogA("sizeof(data) is too small: the longest line won't fit");
				break;
			}
		}
	}

	debugLogA("Closing file transfer thread");
}

void CMsnProto::msnftp_startFileSend(ThreadData* info, const char* Invcommand, const char* Invcookie)
{
	if (_stricmp(Invcommand, "ACCEPT"))
		return;

	NETLIBBIND nlb = { 0 };
	HANDLE sb = NULL;

	filetransfer* ft = info->mMsnFtp; info->mMsnFtp = NULL;
	if (ft != NULL && MyConnection.extIP) {
		nlb.cbSize = sizeof(nlb);
		nlb.pfnNewConnectionV2 = MSN_ConnectionProc;
		nlb.pExtra = this;

		sb = (HANDLE)CallService(MS_NETLIB_BINDPORT, (WPARAM)m_hNetlibUser, (LPARAM)&nlb);
		if (sb == NULL)
			debugLogA("Unable to bind the port for incoming transfers");
	}

	char hostname[256] = "";
	gethostname(hostname, sizeof(hostname));
	PHOSTENT he = gethostbyname(hostname);

	const PIN_ADDR addr = (PIN_ADDR)he->h_addr_list[0];
	if (addr)
		mir_strcpy(hostname, inet_ntoa(*addr));
	else
		hostname[0] = 0;

	info->sendPacketPayload("MSG", "N",
		"MIME-Version: 1.0\r\n"
		"Content-Type: text/x-msmsgsinvite; charset=UTF-8\r\n\r\n"
		"Invitation-Command: %s\r\n"
		"Invitation-Cookie: %s\r\n"
		"IP-Address: %s\r\n"
		"IP-Address-Internal: %s\r\n"
		"Port: %i\r\n"
		"PortX: %i\r\n"
		"PortX-Internal: %i\r\n"
		"AuthCookie: %i\r\n"
		"Launch-Application: FALSE\r\n"
		"Request-Data: IP-Address:\r\n\r\n",
		sb && MyConnection.extIP ? "ACCEPT" : "CANCEL",
		Invcookie, MyConnection.GetMyExtIPStr(), hostname,
		nlb.wExPort, nlb.wExPort ^ 0x3141, nlb.wPort ^ 0x3141,
		MSN_GenRandom());

	if (sb) {
		ThreadData* newThread = new ThreadData;
		newThread->mType = SERVER_FILETRANS;
		newThread->mCaller = 2;
		newThread->mMsnFtp = ft;
		newThread->mIncomingBoundPort = sb;
		newThread->mIncomingPort = nlb.wPort;
		newThread->startThread(&CMsnProto::msnftp_sendFileThread, this);
	}
	else delete ft;
}
#endif