Mercurial > pidgin
comparison libgaim/protocols/qq/file_trans.c @ 14192:60b1bc8dbf37
[gaim-migrate @ 16863]
Renamed 'core' to 'libgaim'
committer: Tailor Script <tailor@pidgin.im>
author | Evan Schoenberg <evan.s@dreskin.net> |
---|---|
date | Sat, 19 Aug 2006 01:50:10 +0000 |
parents | |
children | 85f3808ca472 |
comparison
equal
deleted
inserted
replaced
14191:009db0b357b5 | 14192:60b1bc8dbf37 |
---|---|
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" | |
41 | |
42 extern gchar *hex_dump_to_str (const guint8 *buffer, gint bytes); | |
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, (guint8 *) 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 guint8 *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(guint8, 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); | |
118 | |
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 | |
169 * but it will spend plenty of memory, so do not use it in a low-memory host */ | |
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 | |
193 static gint _qq_xfer_read_file(guint8 *buffer, guint index, guint len, GaimXfer *xfer) | |
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 | |
204 static gint _qq_xfer_write_file(guint8 *buffer, guint index, guint len, GaimXfer *xfer) | |
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 | |
219 static int _qq_xfer_open_file(const gchar *filename, const gchar *method, GaimXfer *xfer) | |
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 | |
229 static gint _qq_xfer_read_file(guint8 *buffer, guint index, guint len, GaimXfer *xfer) | |
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 | |
237 static gint _qq_xfer_write_file(guint8 *buffer, guint index, guint len, GaimXfer *xfer) | |
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 | |
252 static gint _qq_send_file(GaimConnection *gc, guint8 *data, gint len, guint16 packet_type, guint32 to_uid) | |
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 | |
285 extern gchar *_gen_session_md5(gint uid, guint8 *session_key); | |
286 | |
287 /* send a file to udp channel with QQ_FILE_CONTROL_PACKET_TAG */ | |
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); | |
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 */ | |
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); | |
360 /*debug: try to decrypt it */ | |
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 | |
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) | |
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 ft_info *info; | |
402 | |
403 g_return_if_fail(gc != NULL && gc->proto_data != NULL); | |
404 qd = (qq_data *) gc->proto_data; | |
405 info = (ft_info *) qd->xfer->data; | |
406 | |
407 filename = (gchar *) gaim_xfer_get_filename(qd->xfer); | |
408 filesize = gaim_xfer_get_size(qd->xfer); | |
409 | |
410 raw_data = g_newa(guint8, MAX_PACKET_SIZE); | |
411 cursor = raw_data; | |
412 bytes = 0; | |
413 | |
414 bytes += create_packet_b(raw_data, &cursor, 0x00); | |
415 bytes += create_packet_w(raw_data, &cursor, packet_type); | |
416 switch (packet_type) { | |
417 case QQ_FILE_BASIC_INFO: | |
418 case QQ_FILE_DATA_INFO: | |
419 case QQ_FILE_EOF: | |
420 bytes += create_packet_w(raw_data, &cursor, 0x0000); | |
421 bytes += create_packet_b(raw_data, &cursor, 0x00); | |
422 break; | |
423 case QQ_FILE_CMD_FILE_OP: | |
424 switch(sub_type) | |
425 { | |
426 case QQ_FILE_BASIC_INFO: | |
427 filename_len = strlen(filename); | |
428 _fill_filename_md5(filename, filename_md5); | |
429 _fill_file_md5(gaim_xfer_get_local_filename(qd->xfer), | |
430 gaim_xfer_get_size(qd->xfer), | |
431 file_md5); | |
432 | |
433 info->fragment_num = (filesize - 1) / QQ_FILE_FRAGMENT_MAXLEN + 1; | |
434 info->fragment_len = QQ_FILE_FRAGMENT_MAXLEN; | |
435 | |
436 gaim_debug(GAIM_DEBUG_INFO, "QQ", | |
437 "start transfering data, %d fragments with %d length each\n", | |
438 info->fragment_num, info->fragment_len); | |
439 /* Unknown */ | |
440 bytes += create_packet_w(raw_data, &cursor, 0x0000); | |
441 /* Sub-operation type */ | |
442 bytes += create_packet_b(raw_data, &cursor, sub_type); | |
443 /* Length of file */ | |
444 bytes += create_packet_dw(raw_data, &cursor, filesize); | |
445 /* Number of fragments */ | |
446 bytes += create_packet_dw(raw_data, &cursor, info->fragment_num); | |
447 /* Length of a single fragment */ | |
448 bytes += create_packet_dw(raw_data, &cursor, info->fragment_len); | |
449 bytes += create_packet_data(raw_data, &cursor, file_md5, 16); | |
450 bytes += create_packet_data(raw_data, &cursor, filename_md5, 16); | |
451 /* Length of filename */ | |
452 bytes += create_packet_w(raw_data, &cursor, filename_len); | |
453 /* 8 unknown bytes */ | |
454 bytes += create_packet_dw(raw_data, &cursor, 0x00000000); | |
455 bytes += create_packet_dw(raw_data, &cursor, 0x00000000); | |
456 /* filename */ | |
457 bytes += create_packet_data(raw_data, &cursor, (guint8 *) filename, | |
458 filename_len); | |
459 break; | |
460 case QQ_FILE_DATA_INFO: | |
461 gaim_debug(GAIM_DEBUG_INFO, "QQ", | |
462 "sending %dth fragment with length %d, offset %d\n", | |
463 fragment_index, len, (fragment_index-1)*fragment_size); | |
464 /* bytes += create_packet_w(raw_data, &cursor, ++(qd->send_seq)); */ | |
465 bytes += create_packet_w(raw_data, &cursor, info->send_seq); | |
466 bytes += create_packet_b(raw_data, &cursor, sub_type); | |
467 /* bytes += create_packet_dw(raw_data, &cursor, fragment_index); */ | |
468 bytes += create_packet_dw(raw_data, &cursor, fragment_index - 1); | |
469 bytes += create_packet_dw(raw_data, &cursor, (fragment_index - 1) * fragment_size); | |
470 bytes += create_packet_w(raw_data, &cursor, len); | |
471 bytes += create_packet_data(raw_data, &cursor, data, len); | |
472 break; | |
473 case QQ_FILE_EOF: | |
474 gaim_debug(GAIM_DEBUG_INFO, "QQ", "end of sending data\n"); | |
475 /* bytes += create_packet_w(raw_data, &cursor, info->fragment_num + 1); */ | |
476 bytes += create_packet_w(raw_data, &cursor, info->fragment_num); | |
477 bytes += create_packet_b(raw_data, &cursor, sub_type); | |
478 /* gaim_xfer_set_completed(qd->xfer, TRUE); */ | |
479 } | |
480 break; | |
481 case QQ_FILE_CMD_FILE_OP_ACK: | |
482 switch (sub_type) | |
483 { | |
484 case QQ_FILE_BASIC_INFO: | |
485 bytes += create_packet_w(raw_data, &cursor, 0x0000); | |
486 bytes += create_packet_b(raw_data, &cursor, sub_type); | |
487 bytes += create_packet_dw(raw_data, &cursor, 0x00000000); | |
488 break; | |
489 case QQ_FILE_DATA_INFO: | |
490 bytes += create_packet_w(raw_data, &cursor, seq); | |
491 bytes += create_packet_b(raw_data, &cursor, sub_type); | |
492 bytes += create_packet_dw(raw_data, &cursor, fragment_index); | |
493 break; | |
494 case QQ_FILE_EOF: | |
495 bytes += create_packet_w(raw_data, &cursor, filesize / QQ_FILE_FRAGMENT_MAXLEN + 2); | |
496 bytes += create_packet_b(raw_data, &cursor, sub_type); | |
497 break; | |
498 } | |
499 } | |
500 gaim_debug(GAIM_DEBUG_INFO, "QQ", "<== send %s packet\n", qq_get_file_cmd_desc(packet_type)); | |
501 _qq_send_file(gc, raw_data, bytes, QQ_FILE_DATA_PACKET_TAG, info->to_uid); | |
502 } | |
503 | |
504 /* A conversation starts like this: | |
505 * Sender ==> Receiver [QQ_FILE_CMD_PING] | |
506 * Sender <== Receiver [QQ_FILE_CMD_PONG] | |
507 * Sender ==> Receiver [QQ_FILE_CMD_SENDER_SAY_HELLO] | |
508 * Sender <== Receiver [QQ_FILE_CMD_SENDER_SAY_HELLO_ACK] | |
509 * Sender <== Receiver [QQ_FILE_CMD_RECEIVER_SAY_HELLO] | |
510 * Sender ==> Receiver [QQ_FILE_CMD_RECEIVER_SAY_HELLO_ACK] | |
511 * Sender ==> Receiver [QQ_FILE_CMD_FILE_OP, QQ_FILE_BASIC_INFO] | |
512 * Sender <== Receiver [QQ_FILE_CMD_FILE_OP_ACK, QQ_FILE_BASIC_INFO] | |
513 * Sender ==> Receiver [QQ_FILE_CMD_FILE_OP, QQ_FILE_DATA_INFO] | |
514 * Sender <== Receiver [QQ_FILE_CMD_FILE_OP_ACK, QQ_FILE_DATA_INFO] | |
515 * Sender ==> Receiver [QQ_FILE_CMD_FILE_OP, QQ_FILE_DATA_INFO] | |
516 * Sender <== Receiver [QQ_FILE_CMD_FILE_OP_ACK, QQ_FILE_DATA_INFO] | |
517 * ...... | |
518 * Sender ==> Receiver [QQ_FILE_CMD_FILE_OP, QQ_FILE_EOF] | |
519 * Sender <== Receiver [QQ_FILE_CMD_FILE_OP_ACK, QQ_FILE_EOF] | |
520 */ | |
521 | |
522 | |
523 static void _qq_process_recv_file_ctl_packet(GaimConnection *gc, guint8 *data, guint8 *cursor, | |
524 gint len, qq_file_header *fh) | |
525 { | |
526 guint8 *decrypted_data; | |
527 gint decrypted_len; | |
528 qq_data *qd = (qq_data *) gc->proto_data; | |
529 guint16 packet_type; | |
530 guint16 seq; | |
531 guint8 hellobyte; | |
532 gchar *md5; | |
533 ft_info *info = (ft_info *) qd->xfer->data; | |
534 | |
535 decrypted_data = g_newa(guint8, len); | |
536 decrypted_len = len; | |
537 | |
538 md5 = _gen_session_md5(qd->uid, qd->session_key); | |
539 if (qq_crypt(DECRYPT, cursor, len - (cursor - data), md5, decrypted_data, &decrypted_len)) { | |
540 cursor = decrypted_data + 16; /* skip md5 section */ | |
541 read_packet_w(decrypted_data, &cursor, decrypted_len, &packet_type); | |
542 read_packet_w(decrypted_data, &cursor, decrypted_len, &seq); | |
543 cursor += 4+1+1+19+1; | |
544 gaim_debug(GAIM_DEBUG_INFO, "QQ", "==> [%d] receive %s packet\n", seq, qq_get_file_cmd_desc(packet_type)); | |
545 gaim_debug(GAIM_DEBUG_INFO, "QQ", "decrypted control packet received: \n%s", | |
546 hex_dump_to_str(decrypted_data, decrypted_len)); | |
547 switch (packet_type) { | |
548 case QQ_FILE_CMD_NOTIFY_IP_ACK: | |
549 cursor = decrypted_data; | |
550 qq_get_conn_info(decrypted_data, &cursor, decrypted_len, info); | |
551 /* qq_send_file_ctl_packet(gc, QQ_FILE_CMD_PING, fh->sender_uid, 0); */ | |
552 qq_send_file_ctl_packet(gc, QQ_FILE_CMD_SENDER_SAY_HELLO, fh->sender_uid, 0); | |
553 break; | |
554 case QQ_FILE_CMD_SENDER_SAY_HELLO: | |
555 /* I'm receiver, if we receive SAY_HELLO from sender, we send back the ACK */ | |
556 cursor += 47; | |
557 read_packet_b(decrypted_data, &cursor, | |
558 decrypted_len, &hellobyte); | |
559 | |
560 qq_send_file_ctl_packet(gc, QQ_FILE_CMD_SENDER_SAY_HELLO_ACK, fh->sender_uid, hellobyte); | |
561 qq_send_file_ctl_packet(gc, QQ_FILE_CMD_RECEIVER_SAY_HELLO, fh->sender_uid, 0); | |
562 break; | |
563 case QQ_FILE_CMD_SENDER_SAY_HELLO_ACK: | |
564 /* I'm sender, do nothing */ | |
565 break; | |
566 case QQ_FILE_CMD_RECEIVER_SAY_HELLO: | |
567 /* I'm sender, ack the hello packet and send the first data */ | |
568 cursor += 47; | |
569 read_packet_b(decrypted_data, &cursor, | |
570 decrypted_len, &hellobyte); | |
571 qq_send_file_ctl_packet(gc, QQ_FILE_CMD_RECEIVER_SAY_HELLO_ACK, fh->sender_uid, hellobyte); | |
572 _qq_send_file_data_packet(gc, QQ_FILE_CMD_FILE_OP, QQ_FILE_BASIC_INFO, 0, 0, NULL, 0); | |
573 break; | |
574 case QQ_FILE_CMD_RECEIVER_SAY_HELLO_ACK: | |
575 /* I'm receiver, do nothing */ | |
576 break; | |
577 case QQ_FILE_CMD_PING: | |
578 /* I'm receiver, ack the PING */ | |
579 qq_send_file_ctl_packet(gc, QQ_FILE_CMD_PONG, fh->sender_uid, 0); | |
580 break; | |
581 case QQ_FILE_CMD_PONG: | |
582 qq_send_file_ctl_packet(gc, QQ_FILE_CMD_SENDER_SAY_HELLO, fh->sender_uid, 0); | |
583 break; | |
584 default: | |
585 gaim_debug(GAIM_DEBUG_INFO, "QQ", "unprocess file command %d\n", packet_type); | |
586 } | |
587 } | |
588 g_free(md5); | |
589 } | |
590 | |
591 static void _qq_recv_file_progess(GaimConnection *gc, guint8 *buffer, guint16 len, guint32 index, guint32 offset) | |
592 { | |
593 qq_data *qd = (qq_data *) gc->proto_data; | |
594 GaimXfer *xfer = qd->xfer; | |
595 ft_info *info = (ft_info *) xfer->data; | |
596 guint32 mask; | |
597 | |
598 gaim_debug(GAIM_DEBUG_INFO, "QQ", | |
599 "receiving %dth fragment with length %d, slide window status %o, max_fragment_index %d\n", | |
600 index, len, info->window, info->max_fragment_index); | |
601 if (info->window == 0 && info->max_fragment_index == 0) { | |
602 if (_qq_xfer_open_file(gaim_xfer_get_local_filename(xfer), "wb", xfer) == -1) { | |
603 gaim_xfer_cancel_local(xfer); | |
604 return; | |
605 } | |
606 gaim_debug(GAIM_DEBUG_INFO, "QQ", "object file opened for writing\n"); | |
607 } | |
608 mask = 0x1 << (index % sizeof(info->window)); | |
609 if (index < info->max_fragment_index || (info->window & mask)) { | |
610 gaim_debug(GAIM_DEBUG_INFO, "QQ", "duplicate %dth fragment, drop it!\n", index+1); | |
611 return; | |
612 } | |
613 | |
614 info->window |= mask; | |
615 | |
616 _qq_xfer_write_file(buffer, index, len, xfer); | |
617 | |
618 xfer->bytes_sent += len; | |
619 xfer->bytes_remaining -= len; | |
620 gaim_xfer_update_progress(xfer); | |
621 | |
622 mask = 0x1 << (info->max_fragment_index % sizeof(info->window)); | |
623 while (info->window & mask) | |
624 { | |
625 info->window &= ~mask; | |
626 info->max_fragment_index ++; | |
627 if (mask & 0x8000) mask = 0x0001; | |
628 else mask = mask << 1; | |
629 } | |
630 gaim_debug(GAIM_DEBUG_INFO, "QQ", "procceed %dth fragment, slide window status %o, max_fragment_index %d\n", | |
631 index, info->window, info->max_fragment_index); | |
632 } | |
633 | |
634 static void _qq_send_file_progess(GaimConnection *gc) | |
635 { | |
636 qq_data *qd = (qq_data *) gc->proto_data; | |
637 GaimXfer *xfer = qd->xfer; | |
638 ft_info *info = (ft_info *) xfer->data; | |
639 guint32 mask; | |
640 guint8 *buffer; | |
641 guint i; | |
642 gint readbytes; | |
643 | |
644 if (gaim_xfer_get_bytes_remaining(xfer) <= 0) return; | |
645 if (info->window == 0 && info->max_fragment_index == 0) | |
646 { | |
647 if (_qq_xfer_open_file(gaim_xfer_get_local_filename(xfer), "rb", xfer) == -1) { | |
648 gaim_xfer_cancel_local(xfer); | |
649 return; | |
650 } | |
651 } | |
652 buffer = g_newa(guint8, info->fragment_len); | |
653 mask = 0x1 << (info->max_fragment_index % sizeof(info->window)); | |
654 for (i = 0; i < sizeof(info->window); i++) { | |
655 if ((info->window & mask) == 0) { | |
656 readbytes = _qq_xfer_read_file(buffer, info->max_fragment_index + i, info->fragment_len, xfer); | |
657 if (readbytes > 0) | |
658 _qq_send_file_data_packet(gc, QQ_FILE_CMD_FILE_OP, QQ_FILE_DATA_INFO, | |
659 info->max_fragment_index + i + 1, 0, buffer, readbytes); | |
660 } | |
661 if (mask & 0x8000) mask = 0x0001; | |
662 else mask = mask << 1; | |
663 } | |
664 } | |
665 | |
666 static void _qq_update_send_progess(GaimConnection *gc, guint32 fragment_index) | |
667 { | |
668 guint32 mask; | |
669 guint8 *buffer; | |
670 gint readbytes; | |
671 qq_data *qd = (qq_data *) gc->proto_data; | |
672 GaimXfer *xfer = qd->xfer; | |
673 ft_info *info = (ft_info *) xfer->data; | |
674 | |
675 gaim_debug(GAIM_DEBUG_INFO, "QQ", | |
676 "receiving %dth fragment ack, slide window status %o, max_fragment_index %d\n", | |
677 fragment_index, info->window, info->max_fragment_index); | |
678 if (fragment_index < info->max_fragment_index || | |
679 fragment_index >= info->max_fragment_index + sizeof(info->window)) { | |
680 gaim_debug(GAIM_DEBUG_INFO, "QQ", "duplicate %dth fragment, drop it!\n", fragment_index+1); | |
681 return; | |
682 } | |
683 mask = 0x1 << (fragment_index % sizeof(info->window)); | |
684 if ((info->window & mask) == 0) | |
685 { | |
686 info->window |= mask; | |
687 if (fragment_index + 1 != info->fragment_num) { | |
688 xfer->bytes_sent += info->fragment_len; | |
689 } else { | |
690 xfer->bytes_sent += gaim_xfer_get_size(xfer) % info->fragment_len; | |
691 } | |
692 xfer->bytes_remaining = gaim_xfer_get_size(xfer) - gaim_xfer_get_bytes_sent(xfer); | |
693 gaim_xfer_update_progress(xfer); | |
694 if (gaim_xfer_get_bytes_remaining(xfer) <= 0) { | |
695 /* We have finished sending the file */ | |
696 gaim_xfer_set_completed(xfer, TRUE); | |
697 return; | |
698 } | |
699 mask = 0x1 << (info->max_fragment_index % sizeof(info->window)); | |
700 while (info->window & mask) | |
701 { | |
702 /* move the slide window */ | |
703 info->window &= ~mask; | |
704 | |
705 buffer = g_newa(guint8, info->fragment_len); | |
706 readbytes = _qq_xfer_read_file(buffer, info->max_fragment_index + sizeof(info->window), | |
707 info->fragment_len, xfer); | |
708 if (readbytes > 0) | |
709 _qq_send_file_data_packet(gc, QQ_FILE_CMD_FILE_OP, QQ_FILE_DATA_INFO, | |
710 info->max_fragment_index + sizeof(info->window) + 1, 0, buffer, readbytes); | |
711 | |
712 info->max_fragment_index ++; | |
713 if (mask & 0x8000) mask = 0x0001; | |
714 else mask = mask << 1; | |
715 } | |
716 } | |
717 gaim_debug(GAIM_DEBUG_INFO, "QQ", | |
718 "procceed %dth fragment ack, slide window status %o, max_fragment_index %d\n", | |
719 fragment_index, info->window, info->max_fragment_index); | |
720 } | |
721 | |
722 static void _qq_process_recv_file_data(GaimConnection *gc, guint8 *data, guint8 *cursor, | |
723 gint len, guint32 to_uid) | |
724 { | |
725 guint16 packet_type; | |
726 guint16 packet_seq; | |
727 guint8 sub_type; | |
728 guint32 fragment_index; | |
729 guint16 fragment_len; | |
730 guint32 fragment_offset; | |
731 qq_data *qd = (qq_data *) gc->proto_data; | |
732 ft_info *info = (ft_info *) qd->xfer->data; | |
733 | |
734 cursor += 1; /* skip an unknown byte */ | |
735 read_packet_w(data, &cursor, len, &packet_type); | |
736 switch(packet_type) | |
737 { | |
738 case QQ_FILE_CMD_FILE_OP: | |
739 read_packet_w(data, &cursor, len, &packet_seq); | |
740 read_packet_b(data, &cursor, len, &sub_type); | |
741 switch (sub_type) | |
742 { | |
743 case QQ_FILE_BASIC_INFO: | |
744 cursor += 4; /* file length, we have already known it from xfer */ | |
745 read_packet_dw(data, &cursor, len, &info->fragment_num); | |
746 read_packet_dw(data, &cursor, len, &info->fragment_len); | |
747 | |
748 /* FIXME: We must check the md5 here, if md5 doesn't match | |
749 * we will ignore the packet or send sth as error number | |
750 */ | |
751 | |
752 info->max_fragment_index = 0; | |
753 info->window = 0; | |
754 gaim_debug(GAIM_DEBUG_INFO, "QQ", | |
755 "start receiving data, %d fragments with %d length each\n", | |
756 info->fragment_num, info->fragment_len); | |
757 _qq_send_file_data_packet(gc, QQ_FILE_CMD_FILE_OP_ACK, sub_type, | |
758 0, 0, NULL, 0); | |
759 break; | |
760 case QQ_FILE_DATA_INFO: | |
761 read_packet_dw(data, &cursor, len, &fragment_index); | |
762 read_packet_dw(data, &cursor, len, &fragment_offset); | |
763 read_packet_w(data, &cursor, len, &fragment_len); | |
764 gaim_debug(GAIM_DEBUG_INFO, "QQ", | |
765 "received %dth fragment with length %d, offset %d\n", | |
766 fragment_index, fragment_len, fragment_offset); | |
767 | |
768 _qq_send_file_data_packet(gc, QQ_FILE_CMD_FILE_OP_ACK, sub_type, | |
769 fragment_index, packet_seq, NULL, 0); | |
770 _qq_recv_file_progess(gc, cursor, fragment_len, fragment_index, fragment_offset); | |
771 break; | |
772 case QQ_FILE_EOF: | |
773 gaim_debug(GAIM_DEBUG_INFO, "QQ", "end of receiving\n"); | |
774 _qq_send_file_data_packet(gc, QQ_FILE_CMD_FILE_OP_ACK, sub_type, | |
775 0, 0, NULL, 0); | |
776 break; | |
777 } | |
778 break; | |
779 case QQ_FILE_CMD_FILE_OP_ACK: | |
780 read_packet_w(data, &cursor, len, &packet_seq); | |
781 read_packet_b(data, &cursor, len, &sub_type); | |
782 switch (sub_type) | |
783 { | |
784 case QQ_FILE_BASIC_INFO: | |
785 info->max_fragment_index = 0; | |
786 info->window = 0; | |
787 /* It is ready to send file data */ | |
788 _qq_send_file_progess(gc); | |
789 break; | |
790 case QQ_FILE_DATA_INFO: | |
791 read_packet_dw(data, &cursor, len, &fragment_index); | |
792 _qq_update_send_progess(gc, fragment_index); | |
793 if (gaim_xfer_is_completed(qd->xfer)) | |
794 _qq_send_file_data_packet(gc, QQ_FILE_CMD_FILE_OP, QQ_FILE_EOF, 0, 0, NULL, 0); | |
795 /* else | |
796 _qq_send_file_progess(gc); */ | |
797 break; | |
798 case QQ_FILE_EOF: | |
799 /* FIXME: OK, we can end the connection successfully */ | |
800 | |
801 _qq_send_file_data_packet(gc, QQ_FILE_EOF, 0, 0, 0, NULL, 0); | |
802 gaim_xfer_set_completed(qd->xfer, TRUE); | |
803 break; | |
804 } | |
805 break; | |
806 case QQ_FILE_EOF: | |
807 _qq_send_file_data_packet(gc, QQ_FILE_EOF, 0, 0, 0, NULL, 0); | |
808 gaim_xfer_set_completed(qd->xfer, TRUE); | |
809 gaim_xfer_end(qd->xfer); | |
810 break; | |
811 case QQ_FILE_BASIC_INFO: | |
812 gaim_debug(GAIM_DEBUG_INFO, "QQ", "here\n"); | |
813 _qq_send_file_data_packet(gc, QQ_FILE_DATA_INFO, 0, 0, 0, NULL, 0); | |
814 break; | |
815 default: | |
816 gaim_debug(GAIM_DEBUG_INFO, "QQ", "_qq_process_recv_file_data: unknown packet type [%d]\n", | |
817 packet_type); | |
818 break; | |
819 } | |
820 } | |
821 | |
822 void qq_process_recv_file(GaimConnection *gc, guint8 *data, gint len) | |
823 { | |
824 guint8 *cursor; | |
825 qq_file_header fh; | |
826 qq_data *qd; | |
827 | |
828 g_return_if_fail(gc != NULL && gc->proto_data != NULL); | |
829 qd = (qq_data *) gc->proto_data; | |
830 | |
831 cursor = data; | |
832 _qq_get_file_header(data, &cursor, len, &fh); | |
833 | |
834 switch (fh.tag) { | |
835 case QQ_FILE_CONTROL_PACKET_TAG: | |
836 _qq_process_recv_file_ctl_packet(gc, data, cursor, len, &fh); | |
837 break; | |
838 case QQ_FILE_DATA_PACKET_TAG: | |
839 _qq_process_recv_file_data(gc, data, cursor, len, fh.sender_uid); | |
840 break; | |
841 default: | |
842 gaim_debug(GAIM_DEBUG_INFO, "QQ", "unknown packet tag"); | |
843 } | |
844 } |