13870
|
1 /**
|
|
2 * The QQ2003C protocol plugin
|
|
3 *
|
|
4 * for gaim
|
|
5 *
|
|
6 * Copyright (C) 2004 Puzzlebird
|
|
7 *
|
|
8 * This program is free software; you can redistribute it and/or modify
|
|
9 * it under the terms of the GNU General Public License as published by
|
|
10 * the Free Software Foundation; either version 2 of the License, or
|
|
11 * (at your option) any later version.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this program; if not, write to the Free Software
|
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
21 */
|
|
22
|
|
23 // START OF FILE
|
|
24 /*****************************************************************************/
|
|
25 #include "debug.h" // gaim_debug
|
|
26 #include "internal.h" // send, socket
|
|
27
|
|
28 #include "packet_parse.h" // MAX_PACKET_SIZE
|
|
29 #include "crypt.h" // qq_crypt
|
|
30 #include "header_info.h" // cmd alias
|
|
31 #include "qq_proxy.h" // qq_proxy_write
|
|
32 #include "send_core.h"
|
|
33 #include "sendqueue.h" // qq_sendpacket
|
|
34 #include "qq.h" // qq_data, QQ_DEBUG
|
|
35
|
|
36 /*****************************************************************************/
|
|
37 // create qq packet header with given sequence
|
|
38 // return the number of bytes in header if succeeds
|
|
39 // return -1 if there is any error
|
13871
|
40 gint _create_packet_head_seq(guint8 * buf, guint8 ** cursor,
|
13870
|
41 GaimConnection * gc, guint16 cmd, gboolean is_auto_seq, guint16 * seq)
|
|
42 {
|
|
43 qq_data *qd;
|
|
44 gint bytes_expected, bytes_written;
|
|
45
|
|
46 g_return_val_if_fail(gc != NULL &&
|
|
47 gc->proto_data != NULL && buf != NULL && cursor != NULL && *cursor != NULL, -1);
|
|
48
|
|
49 qd = (qq_data *) gc->proto_data;
|
|
50 if (is_auto_seq)
|
|
51 *seq = ++(qd->send_seq);
|
|
52
|
|
53 *cursor = buf;
|
|
54 bytes_written = 0;
|
|
55 bytes_expected = (qd->use_tcp) ? QQ_TCP_HEADER_LENGTH : QQ_UDP_HEADER_LENGTH;
|
|
56
|
|
57 // QQ TCP packet has two bytes in the begining defines packet length
|
|
58 // so I leave room here for size
|
|
59 if (qd->use_tcp)
|
|
60 bytes_written += create_packet_w(buf, cursor, 0x0000);
|
|
61
|
|
62 // now comes the normal QQ packet as UDP
|
|
63 bytes_written += create_packet_b(buf, cursor, QQ_PACKET_TAG);
|
|
64 bytes_written += create_packet_w(buf, cursor, QQ_CLIENT);
|
|
65 bytes_written += create_packet_w(buf, cursor, cmd);
|
|
66 bytes_written += create_packet_w(buf, cursor, *seq);
|
|
67
|
|
68 if (bytes_written != bytes_expected) {
|
|
69 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
|
|
70 "Fail create qq header, expect %d bytes, written %d bytes\n", bytes_expected, bytes_written);
|
|
71 bytes_written = -1;
|
|
72 }
|
|
73 return bytes_written;
|
|
74 } // _create_packet_head_seq
|
|
75
|
|
76 /*****************************************************************************/
|
|
77 // for those need ack and resend no ack feed back from server
|
|
78 // return number of bytes written to the socket,
|
|
79 // return -1 if there is any error
|
13871
|
80 gint _qq_send_packet(GaimConnection * gc, guint8 * buf, gint len, guint16 cmd)
|
13870
|
81 {
|
|
82 qq_data *qd;
|
|
83 qq_sendpacket *p;
|
|
84 gint bytes_sent;
|
|
85 guint8 *cursor;
|
|
86
|
|
87 g_return_val_if_fail(gc != NULL && gc->proto_data != NULL, -1);
|
|
88
|
|
89 qd = (qq_data *) gc->proto_data;
|
|
90
|
|
91 if (qd->use_tcp) {
|
|
92 if (len > MAX_PACKET_SIZE) {
|
|
93 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
|
|
94 "xxx [%05d] %s, %d bytes is too large, do not send\n",
|
|
95 qq_get_cmd_desc(cmd), qd->send_seq, len);
|
|
96 return -1;
|
|
97 } else { // I update the len for TCP packet
|
|
98 cursor = buf;
|
|
99 create_packet_w(buf, &cursor, len);
|
|
100 } // if len
|
|
101 } // if use_tcp
|
|
102
|
|
103 bytes_sent = qq_proxy_write(qd, buf, len);
|
|
104
|
|
105 if (bytes_sent >= 0) { //put to queue, for matching server ACK usage
|
|
106 p = g_new0(qq_sendpacket, 1);
|
|
107 p->fd = qd->fd;
|
|
108 p->cmd = cmd;
|
|
109 p->send_seq = qd->send_seq;
|
|
110 p->resend_times = 0;
|
|
111 p->sendtime = time(NULL);
|
|
112 p->buf = g_memdup(buf, len); // don't use g_strdup, may have 0x00
|
|
113 p->len = len;
|
|
114 qd->sendqueue = g_list_append(qd->sendqueue, p);
|
|
115 } // if bytes_sent
|
|
116
|
|
117 return bytes_sent;
|
|
118
|
|
119 } // _qq_send_packet
|
|
120
|
|
121 /*****************************************************************************/
|
|
122 // send the packet generated with the given cmd and data
|
|
123 // return the number of bytes sent to socket if succeeds
|
|
124 // return -1 if there is any error
|
|
125 gint qq_send_cmd(GaimConnection * gc, guint16 cmd,
|
|
126 gboolean is_auto_seq, guint16 seq, gboolean need_ack, guint8 * data, gint len)
|
|
127 {
|
|
128 qq_data *qd;
|
|
129 guint8 *buf, *cursor, *encrypted_data;
|
|
130 guint16 seq_ret;
|
|
131 gint encrypted_len, bytes_written, bytes_expected, bytes_sent;
|
|
132
|
|
133 g_return_val_if_fail(gc != NULL && gc->proto_data != NULL, -1);
|
|
134
|
|
135 qd = (qq_data *) gc->proto_data;
|
|
136 g_return_val_if_fail(qd->session_key != NULL, -1);
|
|
137
|
|
138 // use g_newa, so that the allocated memory will be freed
|
|
139 // when the current stack frame is cleaned up
|
|
140 buf = g_newa(guint8, MAX_PACKET_SIZE);
|
|
141 encrypted_len = len + 16; // at most 16 bytes more
|
|
142 encrypted_data = g_newa(guint8, encrypted_len);
|
|
143 cursor = buf;
|
|
144 bytes_written = 0;
|
|
145
|
|
146 qq_crypt(ENCRYPT, data, len, qd->session_key, encrypted_data, &encrypted_len);
|
|
147
|
|
148 seq_ret = seq;
|
|
149 if (_create_packet_head_seq(buf, &cursor, gc, cmd, is_auto_seq, &seq_ret) >= 0) {
|
|
150 bytes_expected = 4 + encrypted_len + 1;
|
|
151 bytes_written += create_packet_dw(buf, &cursor, (guint32) qd->uid);
|
|
152 bytes_written += create_packet_data(buf, &cursor, encrypted_data, encrypted_len);
|
|
153 bytes_written += create_packet_b(buf, &cursor, QQ_PACKET_TAIL);
|
|
154 if (bytes_written == bytes_expected) { // packet OK
|
|
155 // if it does not need ACK, we send ACK manually several times
|
|
156 if (need_ack) // my request, send it
|
|
157 bytes_sent = _qq_send_packet(gc, buf, cursor - buf, cmd);
|
|
158 else // server's request, send ACK
|
|
159 bytes_sent = qq_proxy_write(qd, buf, cursor - buf);
|
|
160
|
|
161 if (QQ_DEBUG)
|
|
162 gaim_debug(GAIM_DEBUG_INFO, "QQ",
|
|
163 "<== [%05d] %s, %d bytes\n", seq_ret, qq_get_cmd_desc(cmd), bytes_sent);
|
|
164 return bytes_sent;
|
|
165 } else { // bad packet
|
|
166 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
|
|
167 "Fail creating packet, expect %d bytes, written %d bytes\n",
|
|
168 bytes_expected, bytes_written);
|
|
169 return -1;
|
|
170 } // if bytes_written
|
|
171 } // if create_packet_head_seq
|
|
172 return -1;
|
|
173 } // qq_send_cmd
|
|
174
|
|
175 /*****************************************************************************/
|
|
176 // END OF FILE
|