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" // _("get_text")
|
|
27
|
|
28 #include "utils.h" // hex_dump_to_str
|
|
29 #include "packet_parse.h" // MAX_PACKET_SIZE
|
|
30 #include "buddy_info.h" // qq_process_modify_info_reply
|
|
31 #include "buddy_list.h" // qq_process_get_buddies_list_reply
|
|
32 #include "buddy_opt.h" // qq_process_add_buddy_reply
|
|
33 #include "buddy_status.h" // qq_process_friend_change_status
|
|
34 #include "char_conv.h" // qq_to_utf8
|
|
35 #include "crypt.h" // qq_crypt
|
|
36 #include "group_network.h" // qq_process_group_cmd_reply
|
|
37 #include "header_info.h" // cmd alias
|
|
38 #include "keep_alive.h" // qq_process_keep_alive_reply
|
|
39 #include "im.h" // qq_process_send_im_reply
|
|
40 #include "login_logout.h" // qq_process_login_reply
|
|
41 #include "qq_proxy.h" // qq_proxy_read
|
|
42 #include "recv_core.h"
|
|
43 #include "sendqueue.h" // qq_sendqueue_remove
|
|
44 #include "sys_msg.h" // qq_process_msg_sys
|
|
45
|
|
46 typedef struct _packet_before_login packet_before_login;
|
|
47 typedef struct _qq_recv_msg_header qq_recv_msg_header;
|
|
48
|
|
49 struct _packet_before_login {
|
|
50 guint8 *buf;
|
|
51 gint len;
|
|
52 };
|
|
53
|
|
54 struct _qq_recv_msg_header {
|
|
55 guint8 header_tag;
|
|
56 guint16 source_tag;
|
|
57 guint16 cmd;
|
|
58 guint16 seq; // can be ack_seq or send_seq, depends on cmd
|
|
59 };
|
|
60
|
|
61 /*****************************************************************************/
|
|
62 // check whether one sequence number is duplicated or not
|
|
63 // return TRUE if it is duplicated, otherwise FALSE
|
|
64 static gboolean _qq_check_packet_set_window(guint16 seq, GaimConnection * gc)
|
|
65 {
|
|
66 qq_data *qd;
|
|
67 gchar *byte, mask;
|
|
68
|
|
69 g_return_val_if_fail(gc != NULL && gc->proto_data != NULL, FALSE);
|
|
70 qd = (qq_data *) gc->proto_data;
|
|
71 byte = &(qd->window[seq / 8]);
|
|
72 mask = (1 << (seq % 8));
|
|
73
|
|
74 if ((*byte) & mask)
|
|
75 return TRUE; // check mask
|
|
76 (*byte) |= mask;
|
|
77 return FALSE; // set mask
|
|
78 } // _qq_check_packet_set_window
|
|
79
|
|
80 /*****************************************************************************/
|
|
81 // default process, decrypt and dump
|
|
82 static void _qq_process_packet_default(guint8 * buf, gint buf_len, guint16 cmd, guint16 seq, GaimConnection * gc) {
|
|
83
|
|
84 qq_data *qd;
|
|
85 guint8 *data;
|
|
86 gchar *msg_utf8;
|
|
87 gint len;
|
|
88
|
|
89 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
|
|
90 g_return_if_fail(buf != NULL && buf_len != 0);
|
|
91
|
|
92 qd = (qq_data *) gc->proto_data;
|
|
93 len = buf_len;
|
|
94 data = g_newa(guint8, len);
|
|
95 msg_utf8 = NULL;
|
|
96
|
|
97 if (qq_crypt(DECRYPT, buf, buf_len, qd->session_key, data, &len)) {
|
|
98 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
|
|
99 ">>> [%d] %s, %d bytes -> [default] decrypt and dump\n%s",
|
|
100 seq, qq_get_cmd_desc(cmd), buf_len, hex_dump_to_str(data, len));
|
|
101 try_dump_as_gbk(data, len);
|
|
102 } else
|
|
103 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Fail decrypt packet with default process\n");
|
|
104
|
|
105 } // _qq_process_packet_default
|
|
106
|
|
107 /*****************************************************************************/
|
|
108 // process the incoming packet from qq_pending
|
|
109 static void _qq_packet_process(guint8 * buf, gint buf_len, GaimConnection * gc)
|
|
110 {
|
|
111 qq_data *qd;
|
|
112 gint len, bytes_expected, bytes_read;
|
|
113 guint16 buf_len_read; // two bytes in the begining of TCP packet
|
|
114 guint8 *cursor;
|
|
115 qq_recv_msg_header header;
|
|
116 packet_before_login *b4_packet;
|
|
117
|
|
118 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
|
|
119 g_return_if_fail(buf != NULL && buf_len > 0);
|
|
120
|
|
121 qd = (qq_data *) gc->proto_data;
|
|
122 bytes_expected = qd->use_tcp ? QQ_TCP_HEADER_LENGTH : QQ_UDP_HEADER_LENGTH;
|
|
123
|
|
124 if (buf_len < bytes_expected) {
|
|
125 gaim_debug(GAIM_DEBUG_ERROR,
|
|
126 "QQ", "Received packet is too short, dump and drop\n%s", hex_dump_to_str(buf, buf_len));
|
|
127 return;
|
|
128 }
|
|
129 // initialize
|
|
130 cursor = buf;
|
|
131 bytes_read = 0;
|
|
132
|
|
133 // QQ TCP packet returns first 2 bytes the length of this packet
|
|
134 if (qd->use_tcp) {
|
|
135 bytes_read += read_packet_w(buf, &cursor, buf_len, &buf_len_read);
|
|
136 if (buf_len_read != buf_len) { // wrong
|
|
137 gaim_debug
|
|
138 (GAIM_DEBUG_ERROR,
|
|
139 "QQ",
|
|
140 "TCP read %d bytes, header says %d bytes, use header anyway\n", buf_len, buf_len_read);
|
|
141 buf_len = buf_len_read; // we believe header is more accurate
|
|
142 } // if buf_len_read
|
|
143 } // if use_tcp
|
|
144
|
|
145 // now goes the normal QQ packet as UDP packet
|
|
146 bytes_read += read_packet_b(buf, &cursor, buf_len, &header.header_tag);
|
|
147 bytes_read += read_packet_w(buf, &cursor, buf_len, &header.source_tag);
|
|
148 bytes_read += read_packet_w(buf, &cursor, buf_len, &header.cmd);
|
|
149 bytes_read += read_packet_w(buf, &cursor, buf_len, &header.seq);
|
|
150
|
|
151 if (bytes_read != bytes_expected) { // read error
|
|
152 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
|
|
153 "Fail reading packet header, expect %d bytes, read %d bytes\n", bytes_expected, bytes_read);
|
|
154 return;
|
|
155 } // if bytes_read
|
|
156
|
|
157 if ((buf[buf_len - 1] != QQ_PACKET_TAIL) || (header.header_tag != QQ_PACKET_TAG)) {
|
|
158 gaim_debug(GAIM_DEBUG_ERROR,
|
|
159 "QQ", "Unknown QQ proctocol, dump and drop\n%s", hex_dump_to_str(buf, buf_len));
|
|
160 return;
|
|
161 } // if header_tag
|
|
162
|
|
163 if (QQ_DEBUG)
|
|
164 gaim_debug(GAIM_DEBUG_INFO, "QQ",
|
|
165 "==> [%05d] %s, from (%s)\n",
|
|
166 header.seq, qq_get_cmd_desc(header.cmd), qq_get_source_str(header.source_tag));
|
|
167
|
|
168 if (header.cmd != QQ_CMD_LOGIN && header.cmd != QQ_CMD_REQUEST_LOGIN_TOKEN /* gfhuang */) {
|
|
169 if (!qd->logged_in) { // packets before login
|
|
170 b4_packet = g_new0(packet_before_login, 1);
|
|
171 // must duplicate, buffer will be freed after exiting this function
|
|
172 b4_packet->buf = g_memdup(buf, buf_len);
|
|
173 b4_packet->len = buf_len;
|
|
174 if (qd->before_login_packets == NULL)
|
|
175 qd->before_login_packets = g_queue_new();
|
|
176 g_queue_push_head(qd->before_login_packets, b4_packet);
|
|
177 return; // do not process it now
|
|
178 } else if (!g_queue_is_empty(qd->before_login_packets)) {
|
|
179 // logged_in, but we have packets before login
|
|
180 b4_packet = (packet_before_login *)
|
|
181 g_queue_pop_head(qd->before_login_packets);
|
|
182 _qq_packet_process(b4_packet->buf, b4_packet->len, gc);
|
|
183 // in fact this is a recursive call,
|
|
184 // all packets before login will be processed before goes on
|
|
185 g_free(b4_packet->buf); // the buf is duplicated, need to be freed
|
|
186 g_free(b4_packet);
|
|
187 } // if logged_in
|
|
188 } //if header.cmd != QQ_CMD_LOGIN
|
|
189
|
|
190 // this is the length of all the encrypted data (also remove tail tag
|
|
191 len = buf_len - (bytes_read) - 1;
|
|
192
|
|
193 // whether it is an ack
|
|
194 switch (header.cmd) {
|
|
195 case QQ_CMD_RECV_IM:
|
|
196 case QQ_CMD_RECV_MSG_SYS:
|
|
197 case QQ_CMD_RECV_MSG_FRIEND_CHANGE_STATUS:
|
|
198 // server intiated packet, we need to send ack and check duplicaion
|
|
199 // this must be put after processing b4_packet
|
|
200 // as these packets will be passed in twice
|
|
201 if (_qq_check_packet_set_window(header.seq, gc)) {
|
|
202 gaim_debug(GAIM_DEBUG_WARNING,
|
|
203 "QQ", "dup [%05d] %s, discard...\n", header.seq, qq_get_cmd_desc(header.cmd));
|
|
204 return;
|
|
205 }
|
|
206 break;
|
|
207 default:{ // ack packet, we need to update sendqueue
|
|
208 // we do not check duplication for server ack
|
|
209 qq_sendqueue_remove(qd, header.seq);
|
|
210 if (QQ_DEBUG)
|
|
211 gaim_debug(GAIM_DEBUG_INFO, "QQ",
|
|
212 "ack [%05d] %s, remove from sendqueue\n",
|
|
213 header.seq, qq_get_cmd_desc(header.cmd));
|
|
214 } // default
|
|
215 } // switch header.cmd
|
|
216
|
|
217 // now process the packet
|
|
218 switch (header.cmd) {
|
|
219 case QQ_CMD_KEEP_ALIVE:
|
|
220 qq_process_keep_alive_reply(cursor, len, gc);
|
|
221 break;
|
|
222 case QQ_CMD_UPDATE_INFO:
|
|
223 qq_process_modify_info_reply(cursor, len, gc);
|
|
224 break;
|
|
225 case QQ_CMD_ADD_FRIEND_WO_AUTH:
|
|
226 qq_process_add_buddy_reply(cursor, len, header.seq, gc);
|
|
227 break;
|
|
228 case QQ_CMD_DEL_FRIEND:
|
|
229 qq_process_remove_buddy_reply(cursor, len, gc);
|
|
230 break;
|
|
231 case QQ_CMD_REMOVE_SELF:
|
|
232 qq_process_remove_self_reply(cursor, len, gc);
|
|
233 break;
|
|
234 case QQ_CMD_BUDDY_AUTH:
|
|
235 qq_process_add_buddy_auth_reply(cursor, len, gc);
|
|
236 break;
|
|
237 case QQ_CMD_GET_USER_INFO:
|
|
238 qq_process_get_info_reply(cursor, len, gc);
|
|
239 break;
|
|
240 case QQ_CMD_CHANGE_ONLINE_STATUS:
|
|
241 qq_process_change_status_reply(cursor, len, gc);
|
|
242 break;
|
|
243 case QQ_CMD_SEND_IM:
|
|
244 qq_process_send_im_reply(cursor, len, gc);
|
|
245 break;
|
|
246 case QQ_CMD_RECV_IM:
|
|
247 qq_process_recv_im(cursor, len, header.seq, gc);
|
|
248 break;
|
|
249 case QQ_CMD_LOGIN:
|
|
250 qq_process_login_reply(cursor, len, gc);
|
|
251 break;
|
|
252 case QQ_CMD_GET_FRIENDS_LIST:
|
|
253 qq_process_get_buddies_list_reply(cursor, len, gc);
|
|
254 break;
|
|
255 case QQ_CMD_GET_FRIENDS_ONLINE:
|
|
256 qq_process_get_buddies_online_reply(cursor, len, gc);
|
|
257 break;
|
|
258 case QQ_CMD_GROUP_CMD:
|
|
259 qq_process_group_cmd_reply(cursor, len, header.seq, gc);
|
|
260 break;
|
|
261 case QQ_CMD_GET_ALL_LIST_WITH_GROUP: //added by gfhuang
|
|
262 qq_process_get_all_list_with_group_reply(cursor, len, gc);
|
|
263 break;
|
|
264 case QQ_CMD_REQUEST_LOGIN_TOKEN: //added by gfhuang
|
|
265 qq_process_request_login_token_reply(cursor, len, gc);
|
|
266 break;
|
|
267 case QQ_CMD_RECV_MSG_SYS:
|
|
268 qq_process_msg_sys(cursor, len, header.seq, gc);
|
|
269 break;
|
|
270 case QQ_CMD_RECV_MSG_FRIEND_CHANGE_STATUS:
|
|
271 qq_process_friend_change_status(cursor, len, gc);
|
|
272 break;
|
|
273 default:
|
|
274 _qq_process_packet_default(cursor, len, header.cmd, header.seq, gc);
|
|
275 break;
|
|
276 } // switch header.cmd
|
|
277 } // _qq_packet_process
|
|
278
|
|
279 /*****************************************************************************/
|
|
280 // clean up the packets before login
|
|
281 void qq_b4_packets_free(qq_data * qd)
|
|
282 {
|
|
283 packet_before_login *b4_packet;
|
|
284 g_return_if_fail(qd != NULL);
|
|
285 // now clean up my own data structures
|
|
286 if (qd->before_login_packets != NULL) {
|
|
287 while (NULL != (b4_packet = g_queue_pop_tail(qd->before_login_packets))) {
|
|
288 g_free(b4_packet->buf);
|
|
289 g_free(b4_packet);
|
|
290 }
|
|
291 g_queue_free(qd->before_login_packets);
|
|
292 } // if
|
|
293 } // qq_b4_packets_free
|
|
294
|
|
295 /*****************************************************************************/
|
|
296 void qq_input_pending(gpointer data, gint source, GaimInputCondition cond)
|
|
297 {
|
|
298 GaimConnection *gc;
|
|
299 qq_data *qd;;
|
|
300 guint8 *buf;
|
|
301 gint len;
|
|
302
|
|
303 gc = (GaimConnection *) data;
|
|
304 g_return_if_fail(gc != NULL && gc->proto_data != NULL && cond == GAIM_INPUT_READ);
|
|
305
|
|
306 qd = (qq_data *) gc->proto_data;
|
|
307 // according to glib manual memory allocated by g_newa could be
|
|
308 // automatically freed when the current stack frame is cleaned up
|
|
309 buf = g_newa(guint8, MAX_PACKET_SIZE);
|
|
310
|
|
311 // here we have UDP proxy suppport
|
|
312 len = qq_proxy_read(qd, buf, MAX_PACKET_SIZE);
|
|
313 if (len <= 0) {
|
|
314 gaim_connection_error(gc, _("Unable to read from socket"));
|
|
315 return;
|
|
316 } else
|
|
317 _qq_packet_process(buf, len, gc);
|
|
318 } // qq_input_pending
|
|
319
|
|
320 /*****************************************************************************/
|
|
321 // END OF FILE
|