14192
|
1 /**
|
|
2 * The QQ2003C protocol plugin
|
|
3 *
|
|
4 * for gaim
|
|
5 *
|
|
6 * Author: Henry Ou <henry@linux.net>
|
|
7 *
|
|
8 * Copyright (C) 2004 Puzzlebird
|
|
9 *
|
|
10 * This program is free software; you can redistribute it and/or modify
|
|
11 * it under the terms of the GNU General Public License as published by
|
|
12 * the Free Software Foundation; either version 2 of the License, or
|
|
13 * (at your option) any later version.
|
|
14 *
|
|
15 * This program is distributed in the hope that it will be useful,
|
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 * GNU General Public License for more details.
|
|
19 *
|
|
20 * You should have received a copy of the GNU General Public License
|
|
21 * along with this program; if not, write to the Free Software
|
|
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
23 */
|
|
24
|
|
25 #ifdef _WIN32
|
|
26 #define random rand
|
|
27 #endif
|
|
28
|
|
29 #include "debug.h"
|
|
30 #include "ft.h"
|
|
31 #include "cipher.h"
|
|
32
|
|
33 #include "crypt.h"
|
|
34 #include "file_trans.h"
|
|
35 #include "header_info.h"
|
|
36 #include "im.h"
|
|
37 #include "packet_parse.h"
|
|
38 #include "proxy.h"
|
|
39 #include "send_core.h"
|
|
40 #include "send_file.h"
|
14242
|
41 #include "utils.h"
|
14192
|
42
|
|
43 struct _qq_file_header {
|
|
44 guint8 tag;
|
|
45 guint16 client_ver;
|
|
46 guint8 file_key;
|
|
47 guint32 sender_uid;
|
|
48 guint32 receiver_uid;
|
|
49 };
|
|
50
|
|
51 typedef struct _qq_file_header qq_file_header;
|
|
52
|
|
53 static guint32 _get_file_key(guint8 seed)
|
|
54 {
|
|
55 guint32 key;
|
|
56 key = seed | (seed << 8) | (seed << 16) | (seed << 24);
|
|
57 return key;
|
|
58 }
|
|
59
|
|
60 static guint32 _gen_file_key()
|
|
61 {
|
|
62 guint8 seed;
|
|
63
|
|
64 seed = random();
|
|
65 return _get_file_key(seed);
|
|
66 }
|
|
67
|
|
68 static guint32 _decrypt_qq_uid(guint32 uid, guint32 key)
|
|
69 {
|
|
70 return ~(uid ^ key);
|
|
71 }
|
|
72
|
|
73 static guint32 _encrypt_qq_uid(guint32 uid, guint32 key)
|
|
74 {
|
|
75 return (~uid) ^ key;
|
|
76 }
|
|
77
|
14242
|
78 static void _fill_filename_md5(const gchar *filename, guint8 *md5)
|
14192
|
79 {
|
|
80 GaimCipher *cipher;
|
|
81 GaimCipherContext *context;
|
|
82
|
|
83 g_return_if_fail(filename != NULL && md5 != NULL);
|
|
84
|
|
85 cipher = gaim_ciphers_find_cipher("md5");
|
|
86 context = gaim_cipher_context_new(cipher, NULL);
|
|
87 gaim_cipher_context_append(context, (guint8 *) filename, strlen(filename));
|
|
88 gaim_cipher_context_digest(context, 16, md5, NULL);
|
|
89 gaim_cipher_context_destroy(context);
|
|
90 }
|
|
91
|
14242
|
92 static void _fill_file_md5(const gchar *filename, gint filelen, guint8 *md5)
|
14192
|
93 {
|
|
94 FILE *fp;
|
|
95 guint8 *buffer;
|
|
96 GaimCipher *cipher;
|
|
97 GaimCipherContext *context;
|
|
98
|
|
99 const gint QQ_MAX_FILE_MD5_LENGTH = 10002432;
|
|
100
|
|
101 g_return_if_fail(filename != NULL && md5 != NULL);
|
|
102 if (filelen > QQ_MAX_FILE_MD5_LENGTH)
|
|
103 filelen = QQ_MAX_FILE_MD5_LENGTH;
|
|
104
|
|
105 fp = fopen(filename, "rb");
|
|
106 g_return_if_fail(fp != NULL);
|
|
107
|
|
108 buffer = g_newa(guint8, filelen);
|
|
109 g_return_if_fail(buffer != NULL);
|
|
110 fread(buffer, filelen, 1, fp);
|
|
111
|
|
112 cipher = gaim_ciphers_find_cipher("md5");
|
|
113 context = gaim_cipher_context_new(cipher, NULL);
|
|
114 gaim_cipher_context_append(context, buffer, filelen);
|
|
115 gaim_cipher_context_digest(context, 16, md5, NULL);
|
|
116 gaim_cipher_context_destroy(context);
|
|
117
|
|
118 fclose(fp);
|
|
119 }
|
|
120
|
|
121 static void _qq_get_file_header(guint8 *buf, guint8 **cursor, gint buflen, qq_file_header *fh)
|
|
122 {
|
|
123 read_packet_b(buf, cursor, buflen, &(fh->tag));
|
|
124 read_packet_w(buf, cursor, buflen, &(fh->client_ver));
|
|
125 read_packet_b(buf, cursor, buflen, &fh->file_key);
|
|
126 read_packet_dw(buf, cursor, buflen, &(fh->sender_uid));
|
|
127 read_packet_dw(buf, cursor, buflen, &(fh->receiver_uid));
|
|
128
|
|
129 fh->sender_uid = _decrypt_qq_uid(fh->sender_uid, _get_file_key(fh->file_key));
|
|
130 fh->receiver_uid = _decrypt_qq_uid(fh->receiver_uid, _get_file_key(fh->file_key));
|
|
131 }
|
|
132
|
|
133 static const gchar *qq_get_file_cmd_desc(gint type)
|
|
134 {
|
|
135 switch (type) {
|
|
136 case QQ_FILE_CMD_SENDER_SAY_HELLO:
|
|
137 return "QQ_FILE_CMD_SENDER_SAY_HELLO";
|
|
138 case QQ_FILE_CMD_SENDER_SAY_HELLO_ACK:
|
|
139 return "QQ_FILE_CMD_SENDER_SAY_HELLO_ACK";
|
|
140 case QQ_FILE_CMD_RECEIVER_SAY_HELLO:
|
|
141 return "QQ_FILE_CMD_RECEIVER_SAY_HELLO";
|
|
142 case QQ_FILE_CMD_RECEIVER_SAY_HELLO_ACK:
|
|
143 return "QQ_FILE_CMD_RECEIVER_SAY_HELLO_ACK";
|
|
144 case QQ_FILE_CMD_NOTIFY_IP_ACK:
|
|
145 return "QQ_FILE_CMD_NOTIFY_IP_ACK";
|
|
146 case QQ_FILE_CMD_PING:
|
|
147 return "QQ_FILE_CMD_PING";
|
|
148 case QQ_FILE_CMD_PONG:
|
|
149 return "QQ_FILE_CMD_PONG";
|
|
150 case QQ_FILE_CMD_INITATIVE_CONNECT:
|
|
151 return "QQ_FILE_CMD_INITATIVE_CONNECT";
|
|
152 case QQ_FILE_CMD_FILE_OP:
|
|
153 return "QQ_FILE_CMD_FILE_OP";
|
|
154 case QQ_FILE_CMD_FILE_OP_ACK:
|
|
155 return "QQ_FILE_CMD_FILE_OP_ACK";
|
|
156 case QQ_FILE_BASIC_INFO:
|
|
157 return "QQ_FILE_BASIC_INFO";
|
|
158 case QQ_FILE_DATA_INFO:
|
|
159 return "QQ_FILE_DATA_INFO";
|
|
160 case QQ_FILE_EOF:
|
|
161 return "QQ_FILE_EOF";
|
|
162 default:
|
|
163 return "UNKNOWN_TYPE";
|
|
164 }
|
|
165 }
|
|
166
|
|
167 /* The memmap version has better performance for big files transfering
|
|
168 * but it will spend plenty of memory, so do not use it in a low-memory host */
|
|
169 #ifdef USE_MMAP
|
|
170 #include <sys/mman.h>
|
|
171
|
|
172 static int _qq_xfer_open_file(const gchar *filename, const gchar *method, GaimXfer *xfer)
|
|
173 {
|
|
174 ft_info *info = xfer->data;
|
|
175 int fd;
|
|
176 if (method[0] == 'r') {
|
|
177 fd = open(gaim_xfer_get_local_filename(xfer), O_RDONLY);
|
|
178 info->buffer = mmap(0, gaim_xfer_get_size(xfer), PROT_READ, MAP_PRIVATE, fd, 0);
|
|
179 }
|
|
180 else
|
|
181 {
|
|
182 fd = open(gaim_xfer_get_local_filename(xfer), O_RDWR|O_CREAT, 0644);
|
|
183 info->buffer = mmap(0, gaim_xfer_get_size(xfer), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FILE, fd, 0);
|
|
184 }
|
|
185
|
|
186 if (info->buffer == NULL) {
|
|
187 return - 1;
|
|
188 }
|
|
189 return 0;
|
|
190 }
|
|
191
|
|
192 static gint _qq_xfer_read_file(guint8 *buffer, guint index, guint len, GaimXfer *xfer)
|
|
193 {
|
|
194 ft_info *info = xfer->data;
|
|
195 gint readbytes;
|
|
196
|
|
197 buffer = info->buffer + len * index;
|
|
198 readbytes = gaim_xfer_get_size(xfer) - (buffer - info->buffer);
|
|
199 if (readbytes > info->fragment_len) readbytes = info->fragment_len;
|
|
200 return readbytes;
|
|
201 }
|
|
202
|
|
203 static gint _qq_xfer_write_file(guint8 *buffer, guint index, guint len, GaimXfer *xfer)
|
|
204 {
|
|
205 ft_info *info = xfer->data;
|
|
206
|
|
207 memcpy(info->buffer + index * len, buffer, len);
|
|
208 return 0;
|
|
209 }
|
|
210
|
|
211 void qq_xfer_close_file(GaimXfer *xfer)
|
|
212 {
|
|
213 ft_info *info = xfer->data;
|
|
214
|
|
215 if (info->buffer) munmap(info->buffer, gaim_xfer_get_size(xfer));
|
|
216 }
|
|
217 #else
|
|
218 static int _qq_xfer_open_file(const gchar *filename, const gchar *method, GaimXfer *xfer)
|
|
219 {
|
|
220 ft_info *info = xfer->data;
|
|
221 info->dest_fp = fopen(gaim_xfer_get_local_filename(xfer), method);
|
|
222 if (info->dest_fp == NULL) {
|
|
223 return -1;
|
|
224 }
|
|
225 return 0;
|
|
226 }
|
|
227
|
|
228 static gint _qq_xfer_read_file(guint8 *buffer, guint index, guint len, GaimXfer *xfer)
|
|
229 {
|
|
230 ft_info *info = xfer->data;
|
|
231
|
|
232 fseek(info->dest_fp, index * len, SEEK_SET);
|
|
233 return fread(buffer, 1, len, info->dest_fp);
|
|
234 }
|
|
235
|
|
236 static gint _qq_xfer_write_file(guint8 *buffer, guint index, guint len, GaimXfer *xfer)
|
|
237 {
|
|
238 ft_info *info = xfer->data;
|
|
239 fseek(info->dest_fp, index * len, SEEK_SET);
|
|
240 return fwrite(buffer, 1, len, info->dest_fp);
|
|
241 }
|
|
242
|
|
243 void qq_xfer_close_file(GaimXfer *xfer)
|
|
244 {
|
|
245 ft_info *info = xfer->data;
|
|
246
|
|
247 if (info->dest_fp) fclose(info->dest_fp);
|
|
248 }
|
|
249 #endif
|
|
250
|
|
251 static gint _qq_send_file(GaimConnection *gc, guint8 *data, gint len, guint16 packet_type, guint32 to_uid)
|
|
252 {
|
|
253 gint bytes;
|
|
254 guint8 *cursor, *buf;
|
|
255 guint32 file_key;
|
|
256 qq_data *qd;
|
|
257 ft_info *info;
|
|
258
|
|
259 g_return_val_if_fail(gc != NULL && gc->proto_data != NULL, -1);
|
|
260 qd = (qq_data *) gc->proto_data;
|
|
261 g_return_val_if_fail(qd != NULL && qd->session_key != NULL, -1);
|
|
262 info = (ft_info *) qd->xfer->data;
|
|
263 bytes = 0;
|
|
264
|
|
265 buf = g_newa(guint8, MAX_PACKET_SIZE);
|
|
266 cursor = buf;
|
|
267 file_key = _gen_file_key();
|
|
268
|
|
269 bytes += create_packet_b(buf, &cursor, packet_type);
|
|
270 bytes += create_packet_w(buf, &cursor, QQ_CLIENT);
|
|
271 bytes += create_packet_b(buf, &cursor, file_key & 0xff);
|
|
272 bytes += create_packet_dw(buf, &cursor, _encrypt_qq_uid(qd->uid, file_key));
|
|
273 bytes += create_packet_dw(buf, &cursor, _encrypt_qq_uid(to_uid, file_key));
|
|
274 bytes += create_packet_data(buf, &cursor, data, len);
|
|
275
|
|
276 if (bytes == len + 12) {
|
|
277 _qq_xfer_write(buf, bytes, qd->xfer);
|
|
278 } else
|
|
279 gaim_debug(GAIM_DEBUG_INFO, "QQ", "send_file: want %d but got %d\n", len + 12, bytes);
|
|
280 return bytes;
|
|
281 }
|
|
282
|
|
283 /* send a file to udp channel with QQ_FILE_CONTROL_PACKET_TAG */
|
|
284 void qq_send_file_ctl_packet(GaimConnection *gc, guint16 packet_type, guint32 to_uid, guint8 hellobyte)
|
|
285 {
|
|
286 qq_data *qd;
|
|
287 gint bytes, bytes_expected, encrypted_len;
|
14242
|
288 guint8 *raw_data, *cursor, *encrypted_data, *md5;
|
14192
|
289 time_t now;
|
|
290 ft_info *info;
|
|
291
|
|
292 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
|
|
293 qd = (qq_data *) gc->proto_data;
|
|
294 info = (ft_info *) qd->xfer->data;
|
|
295
|
|
296 raw_data = g_new0 (guint8, 61);
|
|
297 cursor = raw_data;
|
|
298
|
|
299 bytes = 0;
|
|
300 now = time(NULL);
|
|
301 md5 = _gen_session_md5(qd->uid, qd->session_key);
|
|
302
|
|
303 bytes += create_packet_data(raw_data, &cursor, md5, 16);
|
|
304 bytes += create_packet_w(raw_data, &cursor, packet_type);
|
|
305 switch (packet_type) {
|
|
306 case QQ_FILE_CMD_SENDER_SAY_HELLO:
|
|
307 case QQ_FILE_CMD_SENDER_SAY_HELLO_ACK:
|
|
308 case QQ_FILE_CMD_RECEIVER_SAY_HELLO_ACK:
|
|
309 case QQ_FILE_CMD_NOTIFY_IP_ACK:
|
|
310 case QQ_FILE_CMD_RECEIVER_SAY_HELLO:
|
|
311 bytes += create_packet_w(raw_data, &cursor, info->send_seq);
|
|
312 break;
|
|
313 default:
|
|
314 bytes += create_packet_w(raw_data, &cursor, ++qd->send_seq);
|
|
315 }
|
|
316 bytes += create_packet_dw(raw_data, &cursor, (guint32) now);
|
|
317 bytes += create_packet_b(raw_data, &cursor, 0x00);
|
|
318 bytes += create_packet_b(raw_data, &cursor, qd->my_icon);
|
|
319 bytes += create_packet_dw(raw_data, &cursor, 0x00000000);
|
|
320 bytes += create_packet_dw(raw_data, &cursor, 0x00000000);
|
|
321 bytes += create_packet_dw(raw_data, &cursor, 0x00000000);
|
|
322 bytes += create_packet_dw(raw_data, &cursor, 0x00000000);
|
|
323 bytes += create_packet_w(raw_data, &cursor, 0x0000);
|
|
324 bytes += create_packet_b(raw_data, &cursor, 0x00);
|
|
325 /* 0x65: send a file, 0x6b: send a custom face */
|
|
326 bytes += create_packet_b(raw_data, &cursor, QQ_FILE_TRANSFER_FILE); /* FIXME temp by gfhuang */
|
|
327 switch (packet_type)
|
|
328 {
|
|
329 case QQ_FILE_CMD_SENDER_SAY_HELLO:
|
|
330 case QQ_FILE_CMD_RECEIVER_SAY_HELLO:
|
|
331 case QQ_FILE_CMD_SENDER_SAY_HELLO_ACK:
|
|
332 case QQ_FILE_CMD_RECEIVER_SAY_HELLO_ACK:
|
|
333 bytes += create_packet_b(raw_data, &cursor, 0x00);
|
|
334 bytes += create_packet_b(raw_data, &cursor, hellobyte);
|
|
335 bytes_expected = 48;
|
|
336 break;
|
|
337 case QQ_FILE_CMD_PING:
|
|
338 case QQ_FILE_CMD_PONG:
|
|
339 case QQ_FILE_CMD_NOTIFY_IP_ACK:
|
|
340 bytes += qq_fill_conn_info(raw_data, &cursor, info);
|
|
341 bytes_expected = 61;
|
|
342 break;
|
|
343 default:
|
|
344 gaim_debug(GAIM_DEBUG_INFO, "QQ", "qq_send_file_ctl_packet: Unknown packet type[%d]\n",
|
|
345 packet_type);
|
|
346 bytes_expected = 0;
|
|
347 }
|
|
348
|
|
349 if (bytes == bytes_expected) {
|
|
350 gaim_debug(GAIM_DEBUG_INFO, "QQ", "sending packet[%s]: \n%s", qq_get_file_cmd_desc(packet_type),
|
|
351 hex_dump_to_str(raw_data, bytes));
|
|
352 encrypted_len = bytes + 16;
|
|
353 encrypted_data = g_newa(guint8, encrypted_len);
|
|
354 qq_crypt(ENCRYPT, raw_data, bytes, info->file_session_key, encrypted_data, &encrypted_len);
|
|
355 /*debug: try to decrypt it */
|
|
356 /*
|
|
357 if (QQ_DEBUG) {
|
|
358 gaim_debug(GAIM_DEBUG_INFO, "QQ", "encrypted packet: \n%s",
|
|
359 hex_dump_to_str(encrypted_data, encrypted_len));
|
|
360 guint8 *buf;
|
|
361 int buflen;
|
|
362 buf = g_newa(guint8, MAX_PACKET_SIZE);
|
|
363 buflen = encrypted_len;
|
|
364 if (qq_crypt(DECRYPT, encrypted_data, encrypted_len, info->file_session_key, buf, &buflen)) {
|
|
365 gaim_debug(GAIM_DEBUG_INFO, "QQ", "decrypt success\n");
|
|
366 if (buflen == bytes && memcmp(raw_data, buf, buflen) == 0)
|
|
367 gaim_debug(GAIM_DEBUG_INFO, "QQ", "checksum ok\n");
|
|
368 gaim_debug(GAIM_DEBUG_INFO, "QQ", "decrypted packet: \n%s",
|
|
369 hex_dump_to_str(buf, buflen));
|
|
370 } else {
|
|
371 gaim_debug(GAIM_DEBUG_INFO, "QQ", "decrypt fail\n");
|
|
372 }
|
|
373 }
|
|
374 */
|
|
375
|
|
376 gaim_debug(GAIM_DEBUG_INFO, "QQ", "<== send %s packet\n", qq_get_file_cmd_desc(packet_type));
|
|
377 _qq_send_file(gc, encrypted_data, encrypted_len, QQ_FILE_CONTROL_PACKET_TAG, info->to_uid);
|
|
378 }
|
|
379 else
|
|
380 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "qq_send_file_ctl_packet: Expected to get %d bytes, but get %d",
|
|
381 bytes_expected, bytes);
|
|
382
|
|
383 g_free(md5);
|
|
384 }
|
|
385
|
|
386 /* send a file to udp channel with QQ_FILE_DATA_PACKET_TAG */
|
|
387 static void _qq_send_file_data_packet(GaimConnection *gc, guint16 packet_type, guint8 sub_type,
|
|
388 guint32 fragment_index, guint16 seq, guint8 *data, gint len)
|
|
389 {
|
|
390 gint bytes;
|
14242
|
391 guint8 *raw_data, *cursor, filename_md5[QQ_KEY_LENGTH], file_md5[QQ_KEY_LENGTH];
|
14192
|
392 guint32 fragment_size = 1000;
|
14242
|
393 gchar *filename;
|
14192
|
394 gint filename_len, filesize;
|
|
395 qq_data *qd;
|
|
396 ft_info *info;
|
|
397
|
|
398 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
|
|
399 qd = (qq_data *) gc->proto_data;
|
|
400 info = (ft_info *) qd->xfer->data;
|
|
401
|
|
402 filename = (gchar *) gaim_xfer_get_filename(qd->xfer);
|
|
403 filesize = gaim_xfer_get_size(qd->xfer);
|
|
404
|
|
405 raw_data = g_newa(guint8, MAX_PACKET_SIZE);
|
|
406 cursor = raw_data;
|
|
407 bytes = 0;
|
|
408
|
|
409 bytes += create_packet_b(raw_data, &cursor, 0x00);
|
|
410 bytes += create_packet_w(raw_data, &cursor, packet_type);
|
|
411 switch (packet_type) {
|
|
412 case QQ_FILE_BASIC_INFO:
|
|
413 case QQ_FILE_DATA_INFO:
|
|
414 case QQ_FILE_EOF:
|
|
415 bytes += create_packet_w(raw_data, &cursor, 0x0000);
|
|
416 bytes += create_packet_b(raw_data, &cursor, 0x00);
|
|
417 break;
|
|
418 case QQ_FILE_CMD_FILE_OP:
|
|
419 switch(sub_type)
|
|
420 {
|
|
421 case QQ_FILE_BASIC_INFO:
|
|
422 filename_len = strlen(filename);
|
|
423 _fill_filename_md5(filename, filename_md5);
|
|
424 _fill_file_md5(gaim_xfer_get_local_filename(qd->xfer),
|
|
425 gaim_xfer_get_size(qd->xfer),
|
|
426 file_md5);
|
|
427
|
|
428 info->fragment_num = (filesize - 1) / QQ_FILE_FRAGMENT_MAXLEN + 1;
|
|
429 info->fragment_len = QQ_FILE_FRAGMENT_MAXLEN;
|
|
430
|
|
431 gaim_debug(GAIM_DEBUG_INFO, "QQ",
|
|
432 "start transfering data, %d fragments with %d length each\n",
|
|
433 info->fragment_num, info->fragment_len);
|
|
434 /* Unknown */
|
|
435 bytes += create_packet_w(raw_data, &cursor, 0x0000);
|
|
436 /* Sub-operation type */
|
|
437 bytes += create_packet_b(raw_data, &cursor, sub_type);
|
|
438 /* Length of file */
|
|
439 bytes += create_packet_dw(raw_data, &cursor, filesize);
|
|
440 /* Number of fragments */
|
|
441 bytes += create_packet_dw(raw_data, &cursor, info->fragment_num);
|
|
442 /* Length of a single fragment */
|
|
443 bytes += create_packet_dw(raw_data, &cursor, info->fragment_len);
|
|
444 bytes += create_packet_data(raw_data, &cursor, file_md5, 16);
|
|
445 bytes += create_packet_data(raw_data, &cursor, filename_md5, 16);
|
|
446 /* Length of filename */
|
|
447 bytes += create_packet_w(raw_data, &cursor, filename_len);
|
|
448 /* 8 unknown bytes */
|
|
449 bytes += create_packet_dw(raw_data, &cursor, 0x00000000);
|
|
450 bytes += create_packet_dw(raw_data, &cursor, 0x00000000);
|
|
451 /* filename */
|
|
452 bytes += create_packet_data(raw_data, &cursor, (guint8 *) filename,
|
|
453 filename_len);
|
|
454 break;
|
|
455 case QQ_FILE_DATA_INFO:
|
|
456 gaim_debug(GAIM_DEBUG_INFO, "QQ",
|
|
457 "sending %dth fragment with length %d, offset %d\n",
|
|
458 fragment_index, len, (fragment_index-1)*fragment_size);
|
|
459 /* bytes += create_packet_w(raw_data, &cursor, ++(qd->send_seq)); */
|
|
460 bytes += create_packet_w(raw_data, &cursor, info->send_seq);
|
|
461 bytes += create_packet_b(raw_data, &cursor, sub_type);
|
|
462 /* bytes += create_packet_dw(raw_data, &cursor, fragment_index); */
|
|
463 bytes += create_packet_dw(raw_data, &cursor, fragment_index - 1);
|
|
464 bytes += create_packet_dw(raw_data, &cursor, (fragment_index - 1) * fragment_size);
|
|
465 bytes += create_packet_w(raw_data, &cursor, len);
|
|
466 bytes += create_packet_data(raw_data, &cursor, data, len);
|
|
467 break;
|
|
468 case QQ_FILE_EOF:
|
|
469 gaim_debug(GAIM_DEBUG_INFO, "QQ", "end of sending data\n");
|
|
470 /* bytes += create_packet_w(raw_data, &cursor, info->fragment_num + 1); */
|
|
471 bytes += create_packet_w(raw_data, &cursor, info->fragment_num);
|
|
472 bytes += create_packet_b(raw_data, &cursor, sub_type);
|
|
473 /* gaim_xfer_set_completed(qd->xfer, TRUE); */
|
|
474 }
|
|
475 break;
|
|
476 case QQ_FILE_CMD_FILE_OP_ACK:
|
|
477 switch (sub_type)
|
|
478 {
|
|
479 case QQ_FILE_BASIC_INFO:
|
|
480 bytes += create_packet_w(raw_data, &cursor, 0x0000);
|
|
481 bytes += create_packet_b(raw_data, &cursor, sub_type);
|
|
482 bytes += create_packet_dw(raw_data, &cursor, 0x00000000);
|
|
483 break;
|
|
484 case QQ_FILE_DATA_INFO:
|
|
485 bytes += create_packet_w(raw_data, &cursor, seq);
|
|
486 bytes += create_packet_b(raw_data, &cursor, sub_type);
|
|
487 bytes += create_packet_dw(raw_data, &cursor, fragment_index);
|
|
488 break;
|
|
489 case QQ_FILE_EOF:
|
|
490 bytes += create_packet_w(raw_data, &cursor, filesize / QQ_FILE_FRAGMENT_MAXLEN + 2);
|
|
491 bytes += create_packet_b(raw_data, &cursor, sub_type);
|
|
492 break;
|
|
493 }
|
|
494 }
|
|
495 gaim_debug(GAIM_DEBUG_INFO, "QQ", "<== send %s packet\n", qq_get_file_cmd_desc(packet_type));
|
|
496 _qq_send_file(gc, raw_data, bytes, QQ_FILE_DATA_PACKET_TAG, info->to_uid);
|
|
497 }
|
|
498
|
|
499 /* A conversation starts like this:
|
|
500 * Sender ==> Receiver [QQ_FILE_CMD_PING]
|
|
501 * Sender <== Receiver [QQ_FILE_CMD_PONG]
|
|
502 * Sender ==> Receiver [QQ_FILE_CMD_SENDER_SAY_HELLO]
|
|
503 * Sender <== Receiver [QQ_FILE_CMD_SENDER_SAY_HELLO_ACK]
|
|
504 * Sender <== Receiver [QQ_FILE_CMD_RECEIVER_SAY_HELLO]
|
|
505 * Sender ==> Receiver [QQ_FILE_CMD_RECEIVER_SAY_HELLO_ACK]
|
|
506 * Sender ==> Receiver [QQ_FILE_CMD_FILE_OP, QQ_FILE_BASIC_INFO]
|
|
507 * Sender <== Receiver [QQ_FILE_CMD_FILE_OP_ACK, QQ_FILE_BASIC_INFO]
|
|
508 * Sender ==> Receiver [QQ_FILE_CMD_FILE_OP, QQ_FILE_DATA_INFO]
|
|
509 * Sender <== Receiver [QQ_FILE_CMD_FILE_OP_ACK, QQ_FILE_DATA_INFO]
|
|
510 * Sender ==> Receiver [QQ_FILE_CMD_FILE_OP, QQ_FILE_DATA_INFO]
|
|
511 * Sender <== Receiver [QQ_FILE_CMD_FILE_OP_ACK, QQ_FILE_DATA_INFO]
|
|
512 * ......
|
|
513 * Sender ==> Receiver [QQ_FILE_CMD_FILE_OP, QQ_FILE_EOF]
|
|
514 * Sender <== Receiver [QQ_FILE_CMD_FILE_OP_ACK, QQ_FILE_EOF]
|
|
515 */
|
|
516
|
|
517
|
|
518 static void _qq_process_recv_file_ctl_packet(GaimConnection *gc, guint8 *data, guint8 *cursor,
|
|
519 gint len, qq_file_header *fh)
|
|
520 {
|
|
521 guint8 *decrypted_data;
|
|
522 gint decrypted_len;
|
|
523 qq_data *qd = (qq_data *) gc->proto_data;
|
|
524 guint16 packet_type;
|
|
525 guint16 seq;
|
|
526 guint8 hellobyte;
|
14242
|
527 guint8 *md5;
|
14192
|
528 ft_info *info = (ft_info *) qd->xfer->data;
|
|
529
|
|
530 decrypted_data = g_newa(guint8, len);
|
|
531 decrypted_len = len;
|
|
532
|
|
533 md5 = _gen_session_md5(qd->uid, qd->session_key);
|
|
534 if (qq_crypt(DECRYPT, cursor, len - (cursor - data), md5, decrypted_data, &decrypted_len)) {
|
|
535 cursor = decrypted_data + 16; /* skip md5 section */
|
|
536 read_packet_w(decrypted_data, &cursor, decrypted_len, &packet_type);
|
|
537 read_packet_w(decrypted_data, &cursor, decrypted_len, &seq);
|
|
538 cursor += 4+1+1+19+1;
|
|
539 gaim_debug(GAIM_DEBUG_INFO, "QQ", "==> [%d] receive %s packet\n", seq, qq_get_file_cmd_desc(packet_type));
|
|
540 gaim_debug(GAIM_DEBUG_INFO, "QQ", "decrypted control packet received: \n%s",
|
|
541 hex_dump_to_str(decrypted_data, decrypted_len));
|
|
542 switch (packet_type) {
|
|
543 case QQ_FILE_CMD_NOTIFY_IP_ACK:
|
|
544 cursor = decrypted_data;
|
|
545 qq_get_conn_info(decrypted_data, &cursor, decrypted_len, info);
|
|
546 /* qq_send_file_ctl_packet(gc, QQ_FILE_CMD_PING, fh->sender_uid, 0); */
|
|
547 qq_send_file_ctl_packet(gc, QQ_FILE_CMD_SENDER_SAY_HELLO, fh->sender_uid, 0);
|
|
548 break;
|
|
549 case QQ_FILE_CMD_SENDER_SAY_HELLO:
|
|
550 /* I'm receiver, if we receive SAY_HELLO from sender, we send back the ACK */
|
|
551 cursor += 47;
|
|
552 read_packet_b(decrypted_data, &cursor,
|
|
553 decrypted_len, &hellobyte);
|
|
554
|
|
555 qq_send_file_ctl_packet(gc, QQ_FILE_CMD_SENDER_SAY_HELLO_ACK, fh->sender_uid, hellobyte);
|
|
556 qq_send_file_ctl_packet(gc, QQ_FILE_CMD_RECEIVER_SAY_HELLO, fh->sender_uid, 0);
|
|
557 break;
|
|
558 case QQ_FILE_CMD_SENDER_SAY_HELLO_ACK:
|
|
559 /* I'm sender, do nothing */
|
|
560 break;
|
|
561 case QQ_FILE_CMD_RECEIVER_SAY_HELLO:
|
|
562 /* I'm sender, ack the hello packet and send the first data */
|
|
563 cursor += 47;
|
|
564 read_packet_b(decrypted_data, &cursor,
|
|
565 decrypted_len, &hellobyte);
|
|
566 qq_send_file_ctl_packet(gc, QQ_FILE_CMD_RECEIVER_SAY_HELLO_ACK, fh->sender_uid, hellobyte);
|
|
567 _qq_send_file_data_packet(gc, QQ_FILE_CMD_FILE_OP, QQ_FILE_BASIC_INFO, 0, 0, NULL, 0);
|
|
568 break;
|
|
569 case QQ_FILE_CMD_RECEIVER_SAY_HELLO_ACK:
|
|
570 /* I'm receiver, do nothing */
|
|
571 break;
|
|
572 case QQ_FILE_CMD_PING:
|
|
573 /* I'm receiver, ack the PING */
|
|
574 qq_send_file_ctl_packet(gc, QQ_FILE_CMD_PONG, fh->sender_uid, 0);
|
|
575 break;
|
|
576 case QQ_FILE_CMD_PONG:
|
|
577 qq_send_file_ctl_packet(gc, QQ_FILE_CMD_SENDER_SAY_HELLO, fh->sender_uid, 0);
|
|
578 break;
|
|
579 default:
|
|
580 gaim_debug(GAIM_DEBUG_INFO, "QQ", "unprocess file command %d\n", packet_type);
|
|
581 }
|
|
582 }
|
|
583 g_free(md5);
|
|
584 }
|
|
585
|
|
586 static void _qq_recv_file_progess(GaimConnection *gc, guint8 *buffer, guint16 len, guint32 index, guint32 offset)
|
|
587 {
|
|
588 qq_data *qd = (qq_data *) gc->proto_data;
|
|
589 GaimXfer *xfer = qd->xfer;
|
|
590 ft_info *info = (ft_info *) xfer->data;
|
|
591 guint32 mask;
|
|
592
|
|
593 gaim_debug(GAIM_DEBUG_INFO, "QQ",
|
|
594 "receiving %dth fragment with length %d, slide window status %o, max_fragment_index %d\n",
|
|
595 index, len, info->window, info->max_fragment_index);
|
|
596 if (info->window == 0 && info->max_fragment_index == 0) {
|
|
597 if (_qq_xfer_open_file(gaim_xfer_get_local_filename(xfer), "wb", xfer) == -1) {
|
|
598 gaim_xfer_cancel_local(xfer);
|
|
599 return;
|
|
600 }
|
|
601 gaim_debug(GAIM_DEBUG_INFO, "QQ", "object file opened for writing\n");
|
|
602 }
|
|
603 mask = 0x1 << (index % sizeof(info->window));
|
|
604 if (index < info->max_fragment_index || (info->window & mask)) {
|
|
605 gaim_debug(GAIM_DEBUG_INFO, "QQ", "duplicate %dth fragment, drop it!\n", index+1);
|
|
606 return;
|
|
607 }
|
|
608
|
|
609 info->window |= mask;
|
|
610
|
|
611 _qq_xfer_write_file(buffer, index, len, xfer);
|
|
612
|
|
613 xfer->bytes_sent += len;
|
|
614 xfer->bytes_remaining -= len;
|
|
615 gaim_xfer_update_progress(xfer);
|
|
616
|
|
617 mask = 0x1 << (info->max_fragment_index % sizeof(info->window));
|
|
618 while (info->window & mask)
|
|
619 {
|
|
620 info->window &= ~mask;
|
|
621 info->max_fragment_index ++;
|
|
622 if (mask & 0x8000) mask = 0x0001;
|
|
623 else mask = mask << 1;
|
|
624 }
|
|
625 gaim_debug(GAIM_DEBUG_INFO, "QQ", "procceed %dth fragment, slide window status %o, max_fragment_index %d\n",
|
|
626 index, info->window, info->max_fragment_index);
|
|
627 }
|
|
628
|
|
629 static void _qq_send_file_progess(GaimConnection *gc)
|
|
630 {
|
|
631 qq_data *qd = (qq_data *) gc->proto_data;
|
|
632 GaimXfer *xfer = qd->xfer;
|
|
633 ft_info *info = (ft_info *) xfer->data;
|
|
634 guint32 mask;
|
|
635 guint8 *buffer;
|
|
636 guint i;
|
|
637 gint readbytes;
|
|
638
|
|
639 if (gaim_xfer_get_bytes_remaining(xfer) <= 0) return;
|
|
640 if (info->window == 0 && info->max_fragment_index == 0)
|
|
641 {
|
|
642 if (_qq_xfer_open_file(gaim_xfer_get_local_filename(xfer), "rb", xfer) == -1) {
|
|
643 gaim_xfer_cancel_local(xfer);
|
|
644 return;
|
|
645 }
|
|
646 }
|
|
647 buffer = g_newa(guint8, info->fragment_len);
|
|
648 mask = 0x1 << (info->max_fragment_index % sizeof(info->window));
|
|
649 for (i = 0; i < sizeof(info->window); i++) {
|
|
650 if ((info->window & mask) == 0) {
|
|
651 readbytes = _qq_xfer_read_file(buffer, info->max_fragment_index + i, info->fragment_len, xfer);
|
|
652 if (readbytes > 0)
|
|
653 _qq_send_file_data_packet(gc, QQ_FILE_CMD_FILE_OP, QQ_FILE_DATA_INFO,
|
|
654 info->max_fragment_index + i + 1, 0, buffer, readbytes);
|
|
655 }
|
|
656 if (mask & 0x8000) mask = 0x0001;
|
|
657 else mask = mask << 1;
|
|
658 }
|
|
659 }
|
|
660
|
|
661 static void _qq_update_send_progess(GaimConnection *gc, guint32 fragment_index)
|
|
662 {
|
|
663 guint32 mask;
|
|
664 guint8 *buffer;
|
|
665 gint readbytes;
|
|
666 qq_data *qd = (qq_data *) gc->proto_data;
|
|
667 GaimXfer *xfer = qd->xfer;
|
|
668 ft_info *info = (ft_info *) xfer->data;
|
|
669
|
|
670 gaim_debug(GAIM_DEBUG_INFO, "QQ",
|
|
671 "receiving %dth fragment ack, slide window status %o, max_fragment_index %d\n",
|
|
672 fragment_index, info->window, info->max_fragment_index);
|
|
673 if (fragment_index < info->max_fragment_index ||
|
|
674 fragment_index >= info->max_fragment_index + sizeof(info->window)) {
|
|
675 gaim_debug(GAIM_DEBUG_INFO, "QQ", "duplicate %dth fragment, drop it!\n", fragment_index+1);
|
|
676 return;
|
|
677 }
|
|
678 mask = 0x1 << (fragment_index % sizeof(info->window));
|
|
679 if ((info->window & mask) == 0)
|
|
680 {
|
|
681 info->window |= mask;
|
|
682 if (fragment_index + 1 != info->fragment_num) {
|
|
683 xfer->bytes_sent += info->fragment_len;
|
|
684 } else {
|
|
685 xfer->bytes_sent += gaim_xfer_get_size(xfer) % info->fragment_len;
|
|
686 }
|
|
687 xfer->bytes_remaining = gaim_xfer_get_size(xfer) - gaim_xfer_get_bytes_sent(xfer);
|
|
688 gaim_xfer_update_progress(xfer);
|
|
689 if (gaim_xfer_get_bytes_remaining(xfer) <= 0) {
|
|
690 /* We have finished sending the file */
|
|
691 gaim_xfer_set_completed(xfer, TRUE);
|
|
692 return;
|
|
693 }
|
|
694 mask = 0x1 << (info->max_fragment_index % sizeof(info->window));
|
|
695 while (info->window & mask)
|
|
696 {
|
|
697 /* move the slide window */
|
|
698 info->window &= ~mask;
|
|
699
|
|
700 buffer = g_newa(guint8, info->fragment_len);
|
|
701 readbytes = _qq_xfer_read_file(buffer, info->max_fragment_index + sizeof(info->window),
|
|
702 info->fragment_len, xfer);
|
|
703 if (readbytes > 0)
|
|
704 _qq_send_file_data_packet(gc, QQ_FILE_CMD_FILE_OP, QQ_FILE_DATA_INFO,
|
|
705 info->max_fragment_index + sizeof(info->window) + 1, 0, buffer, readbytes);
|
|
706
|
|
707 info->max_fragment_index ++;
|
|
708 if (mask & 0x8000) mask = 0x0001;
|
|
709 else mask = mask << 1;
|
|
710 }
|
|
711 }
|
|
712 gaim_debug(GAIM_DEBUG_INFO, "QQ",
|
|
713 "procceed %dth fragment ack, slide window status %o, max_fragment_index %d\n",
|
|
714 fragment_index, info->window, info->max_fragment_index);
|
|
715 }
|
|
716
|
|
717 static void _qq_process_recv_file_data(GaimConnection *gc, guint8 *data, guint8 *cursor,
|
|
718 gint len, guint32 to_uid)
|
|
719 {
|
|
720 guint16 packet_type;
|
|
721 guint16 packet_seq;
|
|
722 guint8 sub_type;
|
|
723 guint32 fragment_index;
|
|
724 guint16 fragment_len;
|
|
725 guint32 fragment_offset;
|
|
726 qq_data *qd = (qq_data *) gc->proto_data;
|
|
727 ft_info *info = (ft_info *) qd->xfer->data;
|
|
728
|
|
729 cursor += 1; /* skip an unknown byte */
|
|
730 read_packet_w(data, &cursor, len, &packet_type);
|
|
731 switch(packet_type)
|
|
732 {
|
|
733 case QQ_FILE_CMD_FILE_OP:
|
|
734 read_packet_w(data, &cursor, len, &packet_seq);
|
|
735 read_packet_b(data, &cursor, len, &sub_type);
|
|
736 switch (sub_type)
|
|
737 {
|
|
738 case QQ_FILE_BASIC_INFO:
|
|
739 cursor += 4; /* file length, we have already known it from xfer */
|
|
740 read_packet_dw(data, &cursor, len, &info->fragment_num);
|
|
741 read_packet_dw(data, &cursor, len, &info->fragment_len);
|
|
742
|
|
743 /* FIXME: We must check the md5 here, if md5 doesn't match
|
|
744 * we will ignore the packet or send sth as error number
|
|
745 */
|
|
746
|
|
747 info->max_fragment_index = 0;
|
|
748 info->window = 0;
|
|
749 gaim_debug(GAIM_DEBUG_INFO, "QQ",
|
|
750 "start receiving data, %d fragments with %d length each\n",
|
|
751 info->fragment_num, info->fragment_len);
|
|
752 _qq_send_file_data_packet(gc, QQ_FILE_CMD_FILE_OP_ACK, sub_type,
|
|
753 0, 0, NULL, 0);
|
|
754 break;
|
|
755 case QQ_FILE_DATA_INFO:
|
|
756 read_packet_dw(data, &cursor, len, &fragment_index);
|
|
757 read_packet_dw(data, &cursor, len, &fragment_offset);
|
|
758 read_packet_w(data, &cursor, len, &fragment_len);
|
|
759 gaim_debug(GAIM_DEBUG_INFO, "QQ",
|
|
760 "received %dth fragment with length %d, offset %d\n",
|
|
761 fragment_index, fragment_len, fragment_offset);
|
|
762
|
|
763 _qq_send_file_data_packet(gc, QQ_FILE_CMD_FILE_OP_ACK, sub_type,
|
|
764 fragment_index, packet_seq, NULL, 0);
|
|
765 _qq_recv_file_progess(gc, cursor, fragment_len, fragment_index, fragment_offset);
|
|
766 break;
|
|
767 case QQ_FILE_EOF:
|
|
768 gaim_debug(GAIM_DEBUG_INFO, "QQ", "end of receiving\n");
|
|
769 _qq_send_file_data_packet(gc, QQ_FILE_CMD_FILE_OP_ACK, sub_type,
|
|
770 0, 0, NULL, 0);
|
|
771 break;
|
|
772 }
|
|
773 break;
|
|
774 case QQ_FILE_CMD_FILE_OP_ACK:
|
|
775 read_packet_w(data, &cursor, len, &packet_seq);
|
|
776 read_packet_b(data, &cursor, len, &sub_type);
|
|
777 switch (sub_type)
|
|
778 {
|
|
779 case QQ_FILE_BASIC_INFO:
|
|
780 info->max_fragment_index = 0;
|
|
781 info->window = 0;
|
|
782 /* It is ready to send file data */
|
|
783 _qq_send_file_progess(gc);
|
|
784 break;
|
|
785 case QQ_FILE_DATA_INFO:
|
|
786 read_packet_dw(data, &cursor, len, &fragment_index);
|
|
787 _qq_update_send_progess(gc, fragment_index);
|
|
788 if (gaim_xfer_is_completed(qd->xfer))
|
|
789 _qq_send_file_data_packet(gc, QQ_FILE_CMD_FILE_OP, QQ_FILE_EOF, 0, 0, NULL, 0);
|
|
790 /* else
|
|
791 _qq_send_file_progess(gc); */
|
|
792 break;
|
|
793 case QQ_FILE_EOF:
|
|
794 /* FIXME: OK, we can end the connection successfully */
|
|
795
|
|
796 _qq_send_file_data_packet(gc, QQ_FILE_EOF, 0, 0, 0, NULL, 0);
|
|
797 gaim_xfer_set_completed(qd->xfer, TRUE);
|
|
798 break;
|
|
799 }
|
|
800 break;
|
|
801 case QQ_FILE_EOF:
|
|
802 _qq_send_file_data_packet(gc, QQ_FILE_EOF, 0, 0, 0, NULL, 0);
|
|
803 gaim_xfer_set_completed(qd->xfer, TRUE);
|
|
804 gaim_xfer_end(qd->xfer);
|
|
805 break;
|
|
806 case QQ_FILE_BASIC_INFO:
|
|
807 gaim_debug(GAIM_DEBUG_INFO, "QQ", "here\n");
|
|
808 _qq_send_file_data_packet(gc, QQ_FILE_DATA_INFO, 0, 0, 0, NULL, 0);
|
|
809 break;
|
|
810 default:
|
|
811 gaim_debug(GAIM_DEBUG_INFO, "QQ", "_qq_process_recv_file_data: unknown packet type [%d]\n",
|
|
812 packet_type);
|
|
813 break;
|
|
814 }
|
|
815 }
|
|
816
|
|
817 void qq_process_recv_file(GaimConnection *gc, guint8 *data, gint len)
|
|
818 {
|
|
819 guint8 *cursor;
|
|
820 qq_file_header fh;
|
|
821 qq_data *qd;
|
|
822
|
|
823 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
|
|
824 qd = (qq_data *) gc->proto_data;
|
|
825
|
|
826 cursor = data;
|
|
827 _qq_get_file_header(data, &cursor, len, &fh);
|
|
828
|
|
829 switch (fh.tag) {
|
|
830 case QQ_FILE_CONTROL_PACKET_TAG:
|
|
831 _qq_process_recv_file_ctl_packet(gc, data, cursor, len, &fh);
|
|
832 break;
|
|
833 case QQ_FILE_DATA_PACKET_TAG:
|
|
834 _qq_process_recv_file_data(gc, data, cursor, len, fh.sender_uid);
|
|
835 break;
|
|
836 default:
|
|
837 gaim_debug(GAIM_DEBUG_INFO, "QQ", "unknown packet tag");
|
|
838 }
|
|
839 }
|