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