comparison src/protocols/qq/send_file.c @ 13870:983fd420e86b

[gaim-migrate @ 16340] Performed minor cleanup of the OpenQ codebase and patched it into the Gaim trunk as a prpl, providing basic QQ functionality. committer: Tailor Script <tailor@pidgin.im>
author Mark Huetsch <markhuetsch>
date Mon, 26 Jun 2006 02:58:54 +0000
parents
children 2be9dfa9569b
comparison
equal deleted inserted replaced
13869:5642f4658b59 13870:983fd420e86b
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 //#include <network.h>, beta2, gfhuang
26
27 #include "send_file.h"
28 #include "debug.h"
29 #include "notify.h"
30 #include "network.h" //gaim_network_get_my_ip
31
32 #include "im.h" //qq_create_packet_im_header
33 #include "packet_parse.h"
34 #include "crypt.h"
35 #include "header_info.h"
36 #include "send_core.h"
37 #include "utils.h" // gaim_name_to_uid
38 #include "file_trans.h" // qq_send_file_ctl
39 #include "qq.h"
40 #include "buddy_status.h" // by gfhuang
41 #include "keep_alive.h" //by gfhuang
42
43 enum
44 {
45 QQ_FILE_TRANS_REQ = 0x0035,
46 QQ_FILE_TRANS_ACC_UDP = 0x0037,
47 QQ_FILE_TRANS_ACC_TCP = 0x0003,
48 QQ_FILE_TRANS_DENY_UDP = 0x0039,
49 QQ_FILE_TRANS_DENY_TCP = 0x0005,
50 QQ_FILE_TRANS_NOTIFY = 0x003b,
51 QQ_FILE_TRANS_NOTIFY_ACK = 0x003c,
52 QQ_FILE_TRANS_CANCEL = 0x0049,
53 QQ_FILE_TRANS_PASV = 0x003f
54 };
55
56 static int _qq_in_same_lan(ft_info *info)
57 {
58 if (info->remote_internet_ip == info->local_internet_ip) return 1;
59 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Not in the same LAN, remote internet ip[%x], local internet ip[%x]\n", info->remote_internet_ip
60 , info->local_internet_ip);
61 return 0;
62 }
63
64 static int _qq_xfer_init_udp_channel(ft_info *info)
65 {
66 struct sockaddr_in sin;
67 memset(&sin, 0, sizeof(sin));
68 sin.sin_family = AF_INET;
69 if (!_qq_in_same_lan(info)) {
70 sin.sin_port = htons(info->remote_major_port);
71 sin.sin_addr.s_addr = htonl(info->remote_internet_ip);
72 } else {
73 sin.sin_port = htons(info->remote_minor_port);
74 sin.sin_addr.s_addr = htonl(info->remote_real_ip);
75 }
76 return 0;
77 // return connect(info->sender_fd, (struct sockaddr *) &sin, sizeof(sin));
78 }
79
80 /* these 2 functions send and recv buffer from/to UDP channel */
81 static ssize_t _qq_xfer_udp_recv(char *buf, size_t len, GaimXfer *xfer)
82 {
83 struct sockaddr_in sin;
84 int sinlen;
85 ft_info *info;
86 int r;
87
88 info = (ft_info *) xfer->data;
89 sinlen = sizeof(sin);
90 r = recvfrom(info->recv_fd, buf, len, 0, (struct sockaddr *) &sin, &sinlen);
91 gaim_debug(GAIM_DEBUG_INFO, "QQ", "==> recv %d bytes from File UDP Channel, remote ip[%s], remote port[%d]\n",
92 r, inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
93 return r;
94 }
95
96 /*
97 static ssize_t _qq_xfer_udp_send(const char *buf, size_t len, GaimXfer *xfer)
98 {
99 ft_info *info;
100
101 info = (ft_info *) xfer->data;
102 return send(info->sender_fd, buf, len, 0);
103 }
104 */
105 static ssize_t _qq_xfer_udp_send(const char *buf, size_t len, GaimXfer *xfer)
106 {
107 struct sockaddr_in sin;
108 ft_info *info;
109
110 info = (ft_info *) xfer->data;
111 memset(&sin, 0, sizeof(sin));
112 sin.sin_family = AF_INET;
113 if (!_qq_in_same_lan(info)) {
114 sin.sin_port = htons(info->remote_major_port);
115 sin.sin_addr.s_addr = htonl(info->remote_internet_ip);
116 } else if (info->use_major) {
117 sin.sin_port = htons(info->remote_major_port);
118 sin.sin_addr.s_addr = htonl(info->remote_real_ip);
119 } else {
120 sin.sin_port = htons(info->remote_minor_port);
121 sin.sin_addr.s_addr = htonl(info->remote_real_ip);
122 }
123 gaim_debug(GAIM_DEBUG_INFO, "QQ", "sending to channel: %d.%d.%d.%d:%d\n",
124 sin.sin_addr.s_addr & 0xff,
125 (sin.sin_addr.s_addr >> 8) & 0xff,
126 (sin.sin_addr.s_addr >> 16) & 0xff,
127 sin.sin_addr.s_addr >> 24,
128 ntohs(sin.sin_port)
129 );
130 return sendto(info->sender_fd, buf, len, 0, (struct sockaddr *) &sin, sizeof(sin));
131 }
132
133 /* user-defined functions for gaim_xfer_read and gaim_xfer_write */
134
135 /*
136 static ssize_t _qq_xfer_read(char **buf, GaimXfer *xfer)
137 {
138 *buf = g_newa(char, QQ_FILE_FRAGMENT_MAXLEN + 100);
139 return _qq_xfer_udp_recv(*buf, QQ_FILE_FRAGMENT_MAXLEN + 100, xfer);
140 }
141 */
142
143 static gssize _qq_xfer_write(const guchar *buf, size_t len, GaimXfer *xfer) //gfhuang
144 {
145 return _qq_xfer_udp_send(buf, len, xfer);
146 }
147
148 static void
149 _qq_xfer_recv_packet(gpointer data, gint source, GaimInputCondition condition)
150 {
151 GaimXfer *xfer = (GaimXfer *) data;
152 GaimAccount *account = gaim_xfer_get_account(xfer);
153 GaimConnection *gc = gaim_account_get_connection(account);
154 guint8 *buf;
155 gint size;
156 /* FIXME: It seems that the transfer never use a packet
157 * larger than 1500 bytes, so if it happened to be a
158 * larger packet, either error occured or protocol should
159 * be modified
160 */
161 ft_info *info;
162 info = xfer->data;
163 g_return_if_fail (source == info->recv_fd);
164 buf = g_newa(guint8, 1500);
165 size = _qq_xfer_udp_recv(buf, 1500, xfer);
166 qq_process_recv_file(gc, buf, size);
167 }
168 /*****************************************************************************/
169 // start file transfer process
170 static void
171 _qq_xfer_send_start (GaimXfer * xfer)
172 {
173 GaimAccount *account;
174 GaimConnection *gc;
175 ft_info *info;
176
177 account = gaim_xfer_get_account(xfer);
178 gc = gaim_account_get_connection(account);
179 info = (ft_info *) xfer->data;
180 }
181
182 static void
183 _qq_xfer_send_ack (GaimXfer *xfer, const char *buffer, size_t len)
184 {
185
186 GaimAccount *account;
187 GaimConnection *gc;
188
189 account = gaim_xfer_get_account(xfer);
190 gc = gaim_account_get_connection(account);
191 qq_process_recv_file(gc, (guint8 *) buffer, len);
192 }
193
194 static void
195 _qq_xfer_recv_start(GaimXfer *xfer)
196 {
197 }
198
199 static void
200 _qq_xfer_end(GaimXfer *xfer)
201 {
202 ft_info *info;
203 g_return_if_fail(xfer != NULL && xfer->data != NULL);
204 info = (ft_info *) xfer->data;
205
206 qq_xfer_close_file(xfer);
207 if (info->dest_fp != NULL) {
208 fclose(info->dest_fp);
209 gaim_debug(GAIM_DEBUG_INFO, "QQ", "file closed\n");
210 }
211 if (info->major_fd != 0) {
212 close(info->major_fd);
213 gaim_debug(GAIM_DEBUG_INFO, "QQ", "major port closed\n");
214 }
215 if (info->minor_fd != 0) {
216 close(info->minor_fd);
217 gaim_debug(GAIM_DEBUG_INFO, "QQ", "minor port closed\n");
218 }
219 // if (info->buffer != NULL) {
220 // munmap(info->buffer, gaim_xfer_get_size(xfer));
221 // gaim_debug(GAIM_DEBUG_INFO, "QQ", "file mapping buffer is freed.\n");
222 // }
223 g_free(info);
224 }
225
226 void qq_show_conn_info(ft_info *info)
227 {
228 gchar *internet_ip_str, *real_ip_str;
229 guint32 ip;
230
231 ip = htonl(info->remote_real_ip);
232 real_ip_str = gen_ip_str((guint8 *) &ip);
233 ip = htonl(info->remote_internet_ip);
234 internet_ip_str = gen_ip_str((guint8 *) &ip);
235 gaim_debug(GAIM_DEBUG_INFO, "QQ", "remote internet ip[%s:%d], major port[%d], real ip[%s], minor port[%d]\n",
236 internet_ip_str, info->remote_internet_port,
237 info->remote_major_port, real_ip_str, info->remote_minor_port
238 );
239 g_free(real_ip_str);
240 g_free(internet_ip_str);
241 }
242
243 void qq_get_conn_info(guint8 *data, guint8 **cursor, gint data_len, ft_info *info)
244 {
245 read_packet_data(data, cursor, data_len, info->file_session_key, 16);
246 *cursor += 30;
247 read_packet_b(data, cursor, data_len, &info->conn_method);
248 read_packet_dw(data, cursor, data_len, &info->remote_internet_ip);
249 read_packet_w(data, cursor, data_len, &info->remote_internet_port);
250 read_packet_w(data, cursor, data_len, &info->remote_major_port);
251 read_packet_dw(data, cursor, data_len, &info->remote_real_ip);
252 read_packet_w(data, cursor, data_len, &info->remote_minor_port);
253 qq_show_conn_info(info);
254 }
255
256 gint qq_fill_conn_info(guint8 *raw_data, guint8 ** cursor, ft_info *info)
257 {
258 gint bytes;
259 bytes = 0;
260 // 064: connection method, UDP 0x00, TCP 0x03
261 bytes += create_packet_b (raw_data, cursor, info->conn_method);
262 // 065-068: outer ip address of sender (proxy address)
263 bytes += create_packet_dw (raw_data, cursor, info->local_internet_ip);
264 // 069-070: sender port
265 bytes += create_packet_w (raw_data, cursor, info->local_internet_port);
266 // 071-072: the first listening port(TCP doesn't have this part)
267 bytes += create_packet_w (raw_data, cursor, info->local_major_port);
268 // 073-076: real ip
269 bytes += create_packet_dw (raw_data, cursor, info->local_real_ip);
270 // 077-078: the second listening port
271 bytes += create_packet_w (raw_data, cursor, info->local_minor_port);
272 return bytes;
273 }
274
275
276 extern gchar *_gen_session_md5(gint uid, gchar *session_key);
277
278 /*****************************************************************************/
279 // fill in the common information of file transfer
280 static gint _qq_create_packet_file_header
281 (guint8 *raw_data, guint8 ** cursor, guint32 to_uid, guint16 message_type, qq_data *qd, gboolean seq_ack)
282 {
283 gint bytes;
284 time_t now;
285 gchar *md5;
286 guint16 seq;
287 ft_info *info;
288
289 bytes = 0;
290 now = time(NULL);
291 md5 = _gen_session_md5(qd->uid, qd->session_key);
292 if (!seq_ack) seq = qd->send_seq;
293 else {
294 info = (ft_info *) qd->xfer->data;
295 seq = info->send_seq;
296 }
297
298 // 000-003: receiver uid
299 bytes += create_packet_dw (raw_data, cursor, qd->uid);
300 // 004-007: sender uid
301 bytes += create_packet_dw (raw_data, cursor, to_uid);
302 // 008-009: sender client version
303 bytes += create_packet_w (raw_data, cursor, QQ_CLIENT);
304 // 010-013: receiver uid
305 bytes += create_packet_dw (raw_data, cursor, qd->uid);
306 // 014-017: sender uid
307 bytes += create_packet_dw (raw_data, cursor, to_uid);
308 // 018-033: md5 of (uid+session_key)
309 bytes += create_packet_data (raw_data, cursor, md5, 16);
310 // 034-035: message type
311 bytes += create_packet_w (raw_data, cursor, message_type);
312 // 036-037: sequence number
313 bytes += create_packet_w (raw_data, cursor, seq);
314 // 038-041: send time
315 bytes += create_packet_dw (raw_data, cursor, (guint32) now);
316 // 042-042: always 0x00
317 bytes += create_packet_b (raw_data, cursor, 0x00);
318 // 043-043: sender icon
319 bytes += create_packet_b (raw_data, cursor, qd->my_icon);
320 // 044-046: always 0x00
321 bytes += create_packet_w (raw_data, cursor, 0x0000);
322 bytes += create_packet_b (raw_data, cursor, 0x00);
323 // 047-047: we use font attr
324 bytes += create_packet_b (raw_data, cursor, 0x01);
325 // 048-051: always 0x00
326 bytes += create_packet_dw (raw_data, cursor, 0x00000000);
327
328 // 052-062: always 0x00
329 bytes += create_packet_dw (raw_data, cursor, 0x00000000);
330 bytes += create_packet_dw (raw_data, cursor, 0x00000000);
331 bytes += create_packet_w (raw_data, cursor, 0x0000);
332 bytes += create_packet_b (raw_data, cursor, 0x00);
333 // 063: transfer_type, 0x65: FILE 0x6b: FACE
334 bytes += create_packet_b (raw_data, cursor, QQ_FILE_TRANSFER_FILE); /* FIXME by gfhuang */
335
336 g_free (md5);
337 return bytes;
338 } //_qq_create_packet_file_header
339
340
341 #if 0
342 in_addr_t get_real_ip()
343 {
344 char hostname[40];
345 struct hostent *host;
346
347 gethostname(hostname, sizeof(hostname));
348 host = gethostbyname(hostname);
349 return *(host->h_addr);
350 }
351
352
353 #include <sys/ioctl.h>
354 #include <net/if.h>
355
356 #define MAXINTERFACES 16
357 in_addr_t get_real_ip()
358 {
359 int fd, intrface, i;
360 struct ifconf ifc;
361 struct ifreq buf[MAXINTERFACES];
362 in_addr_t ret;
363
364 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) return 0;
365 ifc.ifc_len = sizeof(buf);
366 ifc.ifc_buf = (caddr_t) buf;
367 if (ioctl(fd, SIOCGIFCONF, (char *) &ifc) < 0) return 0;
368 intrface = ifc.ifc_len / sizeof(struct ifreq);
369 for (i = 0; i < intrface; i++) {
370 //buf[intrface].ifr_name
371 if (ioctl(fd, SIOCGIFADDR, (char *) &buf[i]) >= 0)
372 {
373 ret = (((struct sockaddr_in *)(&buf[i].ifr_addr))->sin_addr).s_addr;
374 if (ret == ntohl(0x7f000001)) continue;
375 return ret;
376 }
377 }
378 return 0;
379 }
380 #endif
381
382 static void
383 _qq_xfer_init_socket(GaimXfer *xfer)
384 {
385 int sockfd, listen_port = 0, i, sin_len;
386 struct sockaddr_in sin;
387 ft_info *info;
388
389 g_return_if_fail(xfer != NULL);
390 g_return_if_fail(xfer->data != NULL);
391 info = (ft_info *) xfer->data;
392
393 // info->local_real_ip = ntohl(get_real_ip());
394 //debug
395 // info->local_real_ip = 0x7f000001;
396 info->local_real_ip = ntohl(inet_addr(gaim_network_get_my_ip(-1)));
397 gaim_debug(GAIM_DEBUG_INFO, "QQ", "local real ip is %x", info->local_real_ip);
398
399 for (i = 0; i < 2; i++) {
400 sockfd = socket(PF_INET, SOCK_DGRAM, 0);
401 g_return_if_fail(sockfd >= 0);
402
403 memset(&sin, 0, sizeof(sin));
404 sin.sin_family = AF_INET;
405 sin.sin_port = 0;
406 sin.sin_addr.s_addr = INADDR_ANY;
407 sin_len = sizeof(sin);
408 bind(sockfd, (struct sockaddr *) &sin, sin_len);
409 getsockname(sockfd, (struct sockaddr *) &sin, &sin_len);
410 listen_port = ntohs(sin.sin_port);
411
412 switch (i) {
413 case 0:
414 info->local_major_port = listen_port;
415 info->major_fd = sockfd;
416 gaim_debug(GAIM_DEBUG_INFO, "QQ", "UDP Major Channel created on port[%d]\n",
417 info->local_major_port);
418 break;
419 case 1:
420 info->local_minor_port = listen_port;
421 info->minor_fd = sockfd;
422 gaim_debug(GAIM_DEBUG_INFO, "QQ", "UDP Minor Channel created on port[%d]\n",
423 info->local_minor_port);
424 break;
425 }
426 }
427
428 if (_qq_in_same_lan(info)) {
429 info->sender_fd = info->recv_fd = info->minor_fd;
430 } else {
431 info->sender_fd = info->recv_fd = info->major_fd;
432 }
433
434 // xfer->watcher = gaim_input_add(info->recv_fd, GAIM_INPUT_READ, _qq_xfer_recv_packet, xfer);
435 }
436
437 /*****************************************************************************/
438 // create the QQ_FILE_TRANS_REQ packet with file infomations
439 static void
440 _qq_send_packet_file_request (GaimConnection * gc, guint32 to_uid,
441 gchar * filename, gint filesize)
442 {
443 qq_data *qd;
444 guint8 *cursor, *raw_data;
445 gchar *filelen_str;
446 gint filename_len, filelen_strlen, packet_len, bytes;
447 ft_info *info;
448
449 qd = (qq_data *) gc->proto_data;
450
451 info = g_new0(ft_info, 1);
452 info->to_uid = to_uid;
453 info->send_seq = qd->send_seq;
454 info->local_internet_ip = ntohl(inet_addr(qd->my_ip));
455 info->local_internet_port = qd->my_port;
456 info->local_real_ip = 0x00000000;
457 info->conn_method = 0x00;
458 qd->xfer->data = info;
459
460 filename_len = strlen (filename);
461 filelen_str = g_strdup_printf ("%d ×Ö½Ú", filesize);
462 filelen_strlen = strlen (filelen_str);
463
464 packet_len = 82 + filename_len + filelen_strlen;
465 raw_data = g_newa (guint8, packet_len);
466 cursor = raw_data;
467
468 bytes = _qq_create_packet_file_header(raw_data, &cursor, to_uid, QQ_FILE_TRANS_REQ, qd, FALSE);
469 bytes += qq_fill_conn_info(raw_data, &cursor, info);
470 // 079: 0x20
471 bytes += create_packet_b (raw_data, &cursor, 0x20);
472 // 080: 0x1f
473 bytes += create_packet_b (raw_data, &cursor, 0x1f);
474 // undetermined len: filename
475 bytes += create_packet_data (raw_data, &cursor, filename,
476 filename_len);
477 // 0x1f
478 bytes += create_packet_b (raw_data, &cursor, 0x1f);
479 // file length
480 bytes += create_packet_data (raw_data, &cursor, filelen_str,
481 filelen_strlen);
482
483 if (packet_len == bytes)
484 qq_send_cmd (gc, QQ_CMD_SEND_IM, TRUE, 0, TRUE, raw_data,
485 cursor - raw_data);
486 else
487 gaim_debug (GAIM_DEBUG_INFO, "qq_send_packet_file_request",
488 "%d bytes expected but got %d bytes\n",
489 packet_len, bytes);
490
491 g_free (filelen_str);
492 } //_qq_send_packet_file_request
493
494 /*****************************************************************************/
495 // tell the buddy we want to accept the file
496 static void
497 _qq_send_packet_file_accept(GaimConnection *gc, guint32 to_uid)
498 {
499 qq_data *qd;
500 guint8 *cursor, *raw_data;
501 gint packet_len, bytes;
502 ft_info *info;
503
504 g_return_if_fail (gc != NULL && gc->proto_data != NULL);
505 qd = (qq_data *) gc->proto_data;
506 info = (ft_info *) qd->xfer->data;
507
508 gaim_debug(GAIM_DEBUG_INFO, "QQ", "I've accepted the file transfer request from %d\n", to_uid);
509 _qq_xfer_init_socket(qd->xfer);
510 guint16 minor_port;
511 guint32 real_ip;
512
513 packet_len = 79;
514 raw_data = g_newa (guint8, packet_len);
515 cursor = raw_data;
516
517 minor_port = info->local_minor_port;
518 real_ip = info->local_real_ip;
519 info->local_minor_port = 0;
520 info->local_real_ip = 0;
521
522 bytes = _qq_create_packet_file_header(raw_data, &cursor, to_uid, QQ_FILE_TRANS_ACC_UDP, qd, TRUE);
523 bytes += qq_fill_conn_info(raw_data, &cursor, info);
524
525 info->local_minor_port = minor_port;
526 info->local_real_ip = real_ip;
527
528 if (packet_len == bytes)
529 qq_send_cmd (gc, QQ_CMD_SEND_IM, TRUE, 0, TRUE, raw_data,
530 cursor - raw_data);
531 else
532 gaim_debug (GAIM_DEBUG_INFO, "qq_send_packet_file_accept",
533 "%d bytes expected but got %d bytes\n",
534 packet_len, bytes);
535 } //_qq_send_packet_packet_accept
536
537 static void _qq_send_packet_file_notifyip(GaimConnection *gc, guint32 to_uid)
538 {
539 GaimXfer *xfer;
540 ft_info *info;
541 qq_data *qd;
542 guint8 *cursor, *raw_data;
543 gint packet_len, bytes;
544
545 qd = (qq_data *) gc->proto_data;
546 xfer = qd->xfer;
547 info = xfer->data;
548
549 packet_len = 79;
550 raw_data = g_newa (guint8, packet_len);
551 cursor = raw_data;
552
553 gaim_debug(GAIM_DEBUG_INFO, "QQ", "<== sending qq file notify ip packet\n");
554 bytes = _qq_create_packet_file_header(raw_data, &cursor, to_uid, QQ_FILE_TRANS_NOTIFY, qd, TRUE);
555 bytes += qq_fill_conn_info(raw_data, &cursor, info);
556 if (packet_len == bytes)
557 qq_send_cmd (gc, QQ_CMD_SEND_IM, TRUE, 0, TRUE, raw_data,
558 cursor - raw_data);
559 else
560 gaim_debug (GAIM_DEBUG_INFO, "qq_send_packet_file_notify",
561 "%d bytes expected but got %d bytes\n",
562 packet_len, bytes);
563
564 if (xfer->watcher) gaim_input_remove(xfer->watcher);
565 xfer->watcher = gaim_input_add(info->recv_fd, GAIM_INPUT_READ, _qq_xfer_recv_packet, xfer);
566 gaim_input_add(info->major_fd, GAIM_INPUT_READ, _qq_xfer_recv_packet, xfer);
567 // gaim_input_add(info->minor_fd, GAIM_INPUT_READ, _qq_xfer_recv_packet, xfer);
568 }
569
570 /*****************************************************************************/
571 // tell the buddy we don't want the file
572 static void
573 _qq_send_packet_file_reject (GaimConnection *gc, guint32 to_uid)
574 {
575 qq_data *qd;
576 guint8 *cursor, *raw_data;
577 gint packet_len, bytes;
578
579 gaim_debug(GAIM_DEBUG_INFO, "_qq_send_packet_file_reject", "start");
580 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
581 qd = (qq_data *) gc->proto_data;
582
583 packet_len = 64;
584 raw_data = g_newa (guint8, packet_len);
585 cursor = raw_data;
586 bytes = 0;
587
588 bytes = _qq_create_packet_file_header(raw_data, &cursor, to_uid, QQ_FILE_TRANS_DENY_UDP, qd, TRUE);
589
590 if (packet_len == bytes)
591 qq_send_cmd (gc, QQ_CMD_SEND_IM, TRUE, 0, TRUE, raw_data,
592 cursor - raw_data);
593 else
594 gaim_debug (GAIM_DEBUG_INFO, "qq_send_packet_file",
595 "%d bytes expected but got %d bytes\n",
596 packet_len, bytes);
597
598 // gaim_debug (GAIM_DEBUG_INFO, "qq_send_packet_file_reject", "end\n");
599 } // qq_send_packet_file_reject
600
601 /*****************************************************************************/
602 // tell the buddy to cancel transfer
603 static void
604 _qq_send_packet_file_cancel (GaimConnection *gc, guint32 to_uid)
605 {
606 qq_data *qd;
607 guint8 *cursor, *raw_data;
608 gint packet_len, bytes;
609
610 gaim_debug(GAIM_DEBUG_INFO, "_qq_send_packet_file_cancel", "start\n");
611 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
612 qd = (qq_data *) gc->proto_data;
613
614 packet_len = 64;
615 raw_data = g_newa (guint8, packet_len);
616 cursor = raw_data;
617 bytes = 0;
618
619 gaim_debug(GAIM_DEBUG_INFO, "_qq_send_packet_file_cancel", "before create header\n");
620 bytes = _qq_create_packet_file_header(raw_data, &cursor, to_uid, QQ_FILE_TRANS_CANCEL, qd, TRUE);
621 gaim_debug(GAIM_DEBUG_INFO, "_qq_send_packet_file_cancel", "end create header\n");
622
623 if (packet_len == bytes) {
624 gaim_debug(GAIM_DEBUG_INFO, "_qq_send_packet_file_cancel", "before send cmd\n");
625 qq_send_cmd (gc, QQ_CMD_SEND_IM, TRUE, 0, TRUE, raw_data,
626 cursor - raw_data);
627 }
628 else
629 gaim_debug (GAIM_DEBUG_INFO, "qq_send_packet_file",
630 "%d bytes expected but got %d bytes\n",
631 packet_len, bytes);
632
633 gaim_debug (GAIM_DEBUG_INFO, "qq_send_packet_file_cancel", "end\n");
634 } // qq_send_packet_file_cancel
635
636 /*****************************************************************************/
637 // request to send a file
638 static void
639 _qq_xfer_init (GaimXfer * xfer)
640 {
641 GaimConnection *gc;
642 GaimAccount *account;
643 guint32 to_uid;
644 gchar *filename, *filename_without_path;
645
646 g_return_if_fail (xfer != NULL);
647 account = gaim_xfer_get_account(xfer);
648 gc = gaim_account_get_connection(account);
649 g_return_if_fail (gc != NULL && gc->proto_data != NULL);
650
651 to_uid = gaim_name_to_uid (xfer->who);
652 g_return_if_fail (to_uid != 0);
653
654 filename = (gchar *) gaim_xfer_get_local_filename (xfer);
655 g_return_if_fail (filename != NULL);
656
657 filename_without_path = strrchr (filename, '/') + 1;
658
659 _qq_send_packet_file_request (gc, to_uid, filename_without_path,
660 gaim_xfer_get_size(xfer));
661 } // qq_xfer_init
662
663 /*****************************************************************************/
664 // cancel the transfer of receiving files
665 static void
666 _qq_xfer_cancel(GaimXfer *xfer)
667 {
668 GaimConnection *gc;
669 GaimAccount *account;
670 guint16 *seq;
671
672 g_return_if_fail (xfer != NULL);
673 seq = (guint16 *) xfer->data;
674 account = gaim_xfer_get_account(xfer);
675 gc = gaim_account_get_connection(account);
676
677 switch (gaim_xfer_get_status(xfer)) {
678 case GAIM_XFER_STATUS_CANCEL_LOCAL:
679 _qq_send_packet_file_cancel(gc, gaim_name_to_uid(xfer->who));
680 break;
681 case GAIM_XFER_STATUS_CANCEL_REMOTE:
682 _qq_send_packet_file_cancel(gc, gaim_name_to_uid(xfer->who));
683 break;
684 case GAIM_XFER_STATUS_NOT_STARTED:
685 break;
686 case GAIM_XFER_STATUS_UNKNOWN:
687 _qq_send_packet_file_reject(gc, gaim_name_to_uid(xfer->who));
688 break;
689 case GAIM_XFER_STATUS_DONE:
690 break;
691 case GAIM_XFER_STATUS_ACCEPTED:
692 break;
693 case GAIM_XFER_STATUS_STARTED:
694 break;
695 }
696 }
697
698 /*****************************************************************************/
699 // init the transfer of receiving files
700 static void
701 _qq_xfer_recv_init(GaimXfer *xfer)
702 {
703 GaimConnection *gc;
704 GaimAccount *account;
705 ft_info *info;
706
707 g_return_if_fail (xfer != NULL && xfer->data != NULL);
708 info = (ft_info *) xfer->data;
709 account = gaim_xfer_get_account(xfer);
710 gc = gaim_account_get_connection(account);
711
712 _qq_send_packet_file_accept(gc, gaim_name_to_uid(xfer->who));
713 }
714
715 /*****************************************************************************/
716 // process reject im for file transfer request
717 void qq_process_recv_file_reject
718 (guint8 * data, guint8 ** cursor, gint data_len, guint32 sender_uid,
719 GaimConnection * gc)
720 {
721 gchar *msg, *filename;
722 qq_data *qd;
723
724 g_return_if_fail (gc != NULL && data != NULL && data_len != 0);
725 qd = (qq_data *) gc->proto_data;
726 g_return_if_fail (qd != NULL && qd->xfer != NULL);
727
728 if (*cursor >= (data + data_len - 1)) {
729 gaim_debug (GAIM_DEBUG_WARNING, "QQ",
730 "Received file reject message is empty\n");
731 return;
732 }
733 filename = strrchr(gaim_xfer_get_local_filename(qd->xfer), '/') + 1;
734 msg = g_strdup_printf
735 (_
736 ("Your request to send file[%s] has been rejected by buddy[%d]"),
737 filename, sender_uid);
738
739 gaim_notify_warning (gc, _("File Send"), msg, NULL);
740 gaim_xfer_request_denied(qd->xfer);
741 qd->xfer = NULL;
742
743 g_free (msg);
744 } // qq_process_recv_file_reject
745
746 /*****************************************************************************/
747 // process cancel im for file transfer request
748 void qq_process_recv_file_cancel
749 (guint8 * data, guint8 ** cursor, gint data_len, guint32 sender_uid,
750 GaimConnection * gc)
751 {
752 gchar *msg, *filename;
753 qq_data *qd;
754
755 g_return_if_fail (gc != NULL && data != NULL && data_len != 0);
756 qd = (qq_data *) gc->proto_data;
757 g_return_if_fail (qd != NULL && qd->xfer != NULL
758 && gaim_xfer_get_filename(qd->xfer) != NULL);
759
760 if (*cursor >= (data + data_len - 1)) {
761 gaim_debug (GAIM_DEBUG_WARNING, "QQ",
762 "Received file reject message is empty\n");
763 return;
764 }
765 filename = strrchr(gaim_xfer_get_local_filename(qd->xfer), '/') + 1;
766 msg = g_strdup_printf
767 (_("The sending process of file[%s] has been cancaled by buddy[%d]"),
768 filename, sender_uid);
769
770 gaim_notify_warning (gc, _("File Send"), msg, NULL);
771 gaim_xfer_cancel_remote(qd->xfer);
772 qd->xfer = NULL;
773
774 g_free (msg);
775 } // qq_process_recv_file_cancel
776
777 /*****************************************************************************/
778 // process accept im for file transfer request
779 void qq_process_recv_file_accept
780 (guint8 * data, guint8 ** cursor, gint data_len, guint32 sender_uid,
781 GaimConnection * gc)
782 {
783 qq_data *qd;
784 ft_info *info;
785 GaimXfer *xfer;
786
787 g_return_if_fail (gc != NULL && data != NULL && data_len != 0);
788 qd = (qq_data *) gc->proto_data;
789 xfer = qd->xfer;
790
791 if (*cursor >= (data + data_len - 1)) {
792 gaim_debug (GAIM_DEBUG_WARNING, "QQ",
793 "Received file reject message is empty\n");
794 return;
795 }
796
797 info = (ft_info *) qd->xfer->data;
798
799 *cursor = data + 18 + 12;
800 qq_get_conn_info(data, cursor, data_len, info);
801 _qq_xfer_init_socket(qd->xfer);
802
803 _qq_xfer_init_udp_channel(info);
804 _qq_send_packet_file_notifyip(gc, sender_uid);
805 } // qq_process_recv_file_accept
806
807 /*****************************************************************************/
808 // process request from buddy's im for file transfer request
809 void qq_process_recv_file_request
810 (guint8 * data, guint8 ** cursor, gint data_len, guint32 sender_uid,
811 GaimConnection * gc)
812 {
813 qq_data *qd;
814 GaimXfer *xfer;
815 gchar *sender_name;
816 ft_info *info;
817 GaimBuddy *b; //by gfhuang
818 qq_buddy *q_bud;
819
820 g_return_if_fail (gc != NULL && data != NULL && data_len != 0);
821 qd = (qq_data *) gc->proto_data;
822
823 if (*cursor >= (data + data_len - 1)) {
824 gaim_debug (GAIM_DEBUG_WARNING, "QQ",
825 "Received file reject message is empty\n");
826 return;
827 }
828
829 info = g_new0(ft_info, 1);
830 info->local_internet_ip = ntohl(inet_addr(qd->my_ip));
831 info->local_internet_port = qd->my_port;
832 info->local_real_ip = 0x00000000;
833 info->to_uid = sender_uid;
834 read_packet_w(data, cursor, data_len, &(info->send_seq));
835
836 *cursor = data + 18 + 12;
837 qq_get_conn_info(data, cursor, data_len, info);
838
839 gchar **fileinfo;
840 fileinfo = g_strsplit(data + 81 + 12, "\x1f", 2);
841 g_return_if_fail (fileinfo != NULL && fileinfo[0] != NULL && fileinfo[1] != NULL);
842
843 sender_name = uid_to_gaim_name(sender_uid);
844
845 //FACE from IP detector, ignored by gfhuang
846 if(g_ascii_strcasecmp(fileinfo[0], "FACE") == 0) {
847 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
848 "Received a FACE ip detect from qq-%d, so he/she must be online :)\n", sender_uid);
849
850 b = gaim_find_buddy(gc->account, sender_name);
851 q_bud = (b == NULL) ? NULL : (qq_buddy *) b->proto_data;
852 if (q_bud) {
853 if(0 != info->remote_real_ip) { //by gfhuang
854 g_memmove(q_bud->ip, &info->remote_real_ip, 4);
855 q_bud->port = info->remote_minor_port;
856 }
857 else if (0 != info->remote_internet_ip) {
858 g_memmove(q_bud->ip, &info->remote_internet_ip, 4);
859 q_bud->port = info->remote_major_port;
860 }
861
862 if(!is_online(q_bud->status)) {
863 q_bud->status = QQ_BUDDY_ONLINE_INVISIBLE;
864 qq_update_buddy_contact(gc, q_bud);
865 }
866 else
867 gaim_debug(GAIM_DEBUG_INFO, "QQ", "buddy %d is already online\n", sender_uid);
868
869 }
870 else
871 gaim_debug(GAIM_DEBUG_WARNING, "QQ", "buddy %d is not in my friendlist\n", sender_uid);
872
873 g_free(sender_name);
874 g_strfreev(fileinfo);
875 return;
876 }
877
878 xfer = gaim_xfer_new(gaim_connection_get_account(gc),
879 GAIM_XFER_RECEIVE,
880 sender_name);
881 gaim_xfer_set_filename(xfer, fileinfo[0]);
882 gaim_xfer_set_size(xfer, atoi(fileinfo[1]));
883
884 gaim_xfer_set_init_fnc(xfer, _qq_xfer_recv_init);
885 gaim_xfer_set_request_denied_fnc(xfer, _qq_xfer_cancel);
886 gaim_xfer_set_cancel_recv_fnc(xfer, _qq_xfer_cancel);
887 gaim_xfer_set_end_fnc(xfer, _qq_xfer_end);
888 gaim_xfer_set_write_fnc(xfer, _qq_xfer_write);
889
890 xfer->data = info;
891 qd->xfer = xfer;
892
893 gaim_xfer_request(xfer);
894
895 g_free(sender_name);
896 g_strfreev(fileinfo);
897
898 // gaim_debug (GAIM_DEBUG_INFO, "qq_process_recv_file_request", "end\n");
899 } // qq_process_recv_file_request
900
901 static void
902 _qq_xfer_send_notify_ip_ack(gpointer data, gint source, GaimInputCondition cond)
903 {
904 GaimXfer *xfer = (GaimXfer *) data;
905 GaimAccount *account = gaim_xfer_get_account(xfer);
906 GaimConnection *gc = gaim_account_get_connection(account);
907 ft_info *info = (ft_info *) xfer->data;
908
909 gaim_input_remove(xfer->watcher);
910 xfer->watcher = gaim_input_add(info->recv_fd, GAIM_INPUT_READ, _qq_xfer_recv_packet, xfer);
911 qq_send_file_ctl_packet(gc, QQ_FILE_CMD_NOTIFY_IP_ACK, info->to_uid, 0);
912 // info->use_major = TRUE;
913 // qq_send_file_ctl_packet(gc, QQ_FILE_CMD_NOTIFY_IP_ACK, info->to_uid, 0);
914 // info->use_major = FALSE;
915 }
916
917 void qq_process_recv_file_notify
918 (guint8 * data, guint8 ** cursor, gint data_len, guint32 sender_uid,
919 GaimConnection * gc)
920 {
921 qq_data *qd;
922 ft_info *info;
923 GaimXfer *xfer;
924
925 g_return_if_fail (gc != NULL && data != NULL && data_len != 0);
926 qd = (qq_data *) gc->proto_data;
927
928 if (*cursor >= (data + data_len - 1)) {
929 gaim_debug (GAIM_DEBUG_WARNING, "QQ",
930 "Received file notify message is empty\n");
931 return;
932 }
933
934 xfer = qd->xfer;
935 info = (ft_info *) qd->xfer->data;
936 /* FIXME */
937 read_packet_w(data, cursor, data_len, &(info->send_seq));
938
939 *cursor = data + 18 + 12;
940 qq_get_conn_info(data, cursor, data_len, info);
941
942 _qq_xfer_init_udp_channel(info);
943
944 xfer->watcher = gaim_input_add(info->sender_fd, GAIM_INPUT_WRITE, _qq_xfer_send_notify_ip_ack, xfer);
945 }
946
947 //temp placeholder until a working function can be implemented
948 gboolean qq_can_receive_file(GaimConnection *gc, const char *who)
949 {
950 return TRUE;
951 }
952
953 void qq_send_file(GaimConnection *gc, const char *who, const char *file)
954 {
955 qq_data *qd;
956 GaimXfer *xfer;
957
958 g_return_if_fail (gc != NULL && gc->proto_data != NULL);
959 qd = (qq_data *) gc->proto_data;
960
961 xfer = gaim_xfer_new (gc->account, GAIM_XFER_SEND,
962 who);
963 gaim_xfer_set_init_fnc (xfer, _qq_xfer_init);
964 gaim_xfer_set_cancel_send_fnc (xfer, _qq_xfer_cancel);
965 gaim_xfer_set_write_fnc(xfer, _qq_xfer_write);
966
967 qd->xfer = xfer;
968 gaim_xfer_request (xfer);
969 }
970
971 static void qq_send_packet_request_key(GaimConnection *gc, guint8 key)
972 {
973 qq_send_cmd(gc, QQ_CMD_REQUEST_KEY, TRUE, 0, TRUE, &key, 1);
974 }
975
976 static void qq_process_recv_request_key(GaimConnection *gc)
977 {
978 }