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