~ubuntu-branches/ubuntu/quantal/pidgin/quantal

« back to all changes in this revision

Viewing changes to libpurple/protocols/qq/send_file.c

  • Committer: Bazaar Package Importer
  • Author(s): Ari Pollak
  • Date: 2011-06-13 21:17:20 UTC
  • mfrom: (1.3.19 upstream)
  • mto: This revision was merged to the branch mainline in revision 73.
  • Revision ID: james.westby@ubuntu.com-20110613211720-ke87vzmdcuaxams7
Tags: 2.8.0-1
* Imported Upstream version 2.8.0 (Closes: #630124)
* Remove SILC support since the library will be orphaned (Closes: #629222)
* Fix typo in libpurple-bin description (Closes: #625462)

Show diffs side-by-side

added added

removed removed

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