Mercurial > pidgin
annotate libgaim/protocols/qq/recv_core.c @ 14678:21bd0369ddd7
[gaim-migrate @ 17427]
fix this error
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Wed, 04 Oct 2006 12:31:26 +0000 |
parents | d1a76ccb93c4 |
children | c039c920e11c |
rev | line source |
---|---|
14192 | 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 #include "debug.h" | |
24 #include "internal.h" | |
25 | |
26 #include "buddy_info.h" | |
27 #include "buddy_list.h" | |
28 #include "buddy_opt.h" | |
29 #include "buddy_status.h" | |
30 #include "char_conv.h" | |
31 #include "crypt.h" | |
32 #include "group_network.h" | |
33 #include "header_info.h" | |
34 #include "keep_alive.h" | |
35 #include "im.h" | |
36 #include "login_logout.h" | |
37 #include "packet_parse.h" | |
38 #include "qq_proxy.h" | |
39 #include "recv_core.h" | |
40 #include "sendqueue.h" | |
41 #include "sys_msg.h" | |
42 #include "utils.h" | |
43 | |
44 typedef struct _packet_before_login packet_before_login; | |
45 typedef struct _qq_recv_msg_header qq_recv_msg_header; | |
46 | |
47 struct _packet_before_login { | |
48 guint8 *buf; | |
49 gint len; | |
50 }; | |
51 | |
52 struct _qq_recv_msg_header { | |
53 guint8 header_tag; | |
54 guint16 source_tag; | |
55 guint16 cmd; | |
56 guint16 seq; /* can be ack_seq or send_seq, depends on cmd */ | |
57 }; | |
58 | |
59 /* check whether one sequence number is duplicated or not | |
60 * return TRUE if it is duplicated, otherwise FALSE */ | |
61 static gboolean _qq_check_packet_set_window(guint16 seq, GaimConnection *gc) | |
62 { | |
63 qq_data *qd; | |
64 guint8 *byte, mask; | |
65 | |
66 qd = (qq_data *) gc->proto_data; | |
67 byte = &(qd->window[seq / 8]); | |
68 mask = (1 << (seq % 8)); | |
69 | |
70 if ((*byte) & mask) | |
71 return TRUE; /* check mask */ | |
72 (*byte) |= mask; | |
73 return FALSE; /* set mask */ | |
14629 | 74 } |
14192 | 75 |
76 /* default process, decrypt and dump */ | |
77 static void _qq_process_packet_default(guint8 *buf, gint buf_len, guint16 cmd, guint16 seq, GaimConnection *gc) | |
78 { | |
79 qq_data *qd; | |
80 guint8 *data; | |
81 gchar *msg_utf8; | |
82 gint len; | |
83 | |
84 g_return_if_fail(buf != NULL && buf_len != 0); | |
85 | |
86 qd = (qq_data *) gc->proto_data; | |
87 len = buf_len; | |
88 data = g_newa(guint8, len); | |
89 msg_utf8 = NULL; | |
90 | |
91 _qq_show_packet("Processing unknown packet", buf, len); | |
92 if (qq_crypt(DECRYPT, buf, buf_len, qd->session_key, data, &len)) { | |
14656 | 93 gchar *hex_dump = hex_dump_to_str(data, len); |
14192 | 94 gaim_debug(GAIM_DEBUG_WARNING, "QQ", |
95 ">>> [%d] %s, %d bytes -> [default] decrypt and dump\n%s", | |
14656 | 96 seq, qq_get_cmd_desc(cmd), buf_len, hex_dump); |
97 g_free(hex_dump); | |
14192 | 98 try_dump_as_gbk(data, len); |
99 } else { | |
100 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Fail decrypt packet with default process\n"); | |
101 } | |
102 } | |
103 | |
104 /* process the incoming packet from qq_pending */ | |
105 static void _qq_packet_process(guint8 *buf, gint buf_len, GaimConnection *gc) | |
106 { | |
107 qq_data *qd; | |
108 gint len, bytes_expected, bytes_read; | |
109 guint16 buf_len_read; /* two bytes in the begining of TCP packet */ | |
110 guint8 *cursor; | |
111 qq_recv_msg_header header; | |
112 packet_before_login *b4_packet; | |
113 | |
114 g_return_if_fail(buf != NULL && buf_len > 0); | |
115 | |
116 qd = (qq_data *) gc->proto_data; | |
117 bytes_expected = qd->use_tcp ? QQ_TCP_HEADER_LENGTH : QQ_UDP_HEADER_LENGTH; | |
118 | |
119 if (buf_len < bytes_expected) { | |
14656 | 120 gchar *hex_dump = hex_dump_to_str(buf, buf_len); |
14192 | 121 gaim_debug(GAIM_DEBUG_ERROR, |
14656 | 122 "QQ", "Received packet is too short, dump and drop\n%s", hex_dump); |
123 g_free(hex_dump); | |
14192 | 124 return; |
125 } | |
126 /* initialize */ | |
127 cursor = buf; | |
128 bytes_read = 0; | |
129 | |
130 /* QQ TCP packet returns first 2 bytes the length of this packet */ | |
131 if (qd->use_tcp) { | |
132 bytes_read += read_packet_w(buf, &cursor, buf_len, &buf_len_read); | |
133 if (buf_len_read != buf_len) { /* wrong */ | |
134 gaim_debug | |
135 (GAIM_DEBUG_ERROR, | |
136 "QQ", | |
137 "TCP read %d bytes, header says %d bytes, use header anyway\n", buf_len, buf_len_read); | |
138 buf_len = buf_len_read; /* we believe header is more accurate */ | |
139 } | |
140 } | |
141 | |
142 /* now goes the normal QQ packet as UDP packet */ | |
143 bytes_read += read_packet_b(buf, &cursor, buf_len, &header.header_tag); | |
144 bytes_read += read_packet_w(buf, &cursor, buf_len, &header.source_tag); | |
145 bytes_read += read_packet_w(buf, &cursor, buf_len, &header.cmd); | |
146 bytes_read += read_packet_w(buf, &cursor, buf_len, &header.seq); | |
147 | |
148 if (bytes_read != bytes_expected) { /* read error */ | |
149 gaim_debug(GAIM_DEBUG_ERROR, "QQ", | |
150 "Fail reading packet header, expect %d bytes, read %d bytes\n", | |
151 bytes_expected, bytes_read); | |
152 return; | |
153 } | |
154 | |
155 if ((buf[buf_len - 1] != QQ_PACKET_TAIL) || (header.header_tag != QQ_PACKET_TAG)) { | |
14656 | 156 gchar *hex_dump = hex_dump_to_str(buf, buf_len); |
14192 | 157 gaim_debug(GAIM_DEBUG_ERROR, |
14656 | 158 "QQ", "Unknown QQ proctocol, dump and drop\n%s", hex_dump); |
159 g_free(hex_dump); | |
14192 | 160 return; |
161 } | |
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) { | |
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 } | |
188 } | |
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 } | |
215 } | |
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: | |
262 qq_process_get_all_list_with_group_reply(cursor, len, gc); | |
263 break; | |
264 case QQ_CMD_REQUEST_LOGIN_TOKEN: | |
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 } | |
277 } | |
278 | |
279 /* clean up the packets before login */ | |
280 void qq_b4_packets_free(qq_data *qd) | |
281 { | |
282 packet_before_login *b4_packet; | |
283 g_return_if_fail(qd != NULL); | |
284 /* now clean up my own data structures */ | |
285 if (qd->before_login_packets != NULL) { | |
286 while (NULL != (b4_packet = g_queue_pop_tail(qd->before_login_packets))) { | |
287 g_free(b4_packet->buf); | |
288 g_free(b4_packet); | |
289 } | |
290 g_queue_free(qd->before_login_packets); | |
291 } | |
292 } | |
293 | |
294 void qq_input_pending(gpointer data, gint source, GaimInputCondition cond) | |
295 { | |
296 GaimConnection *gc; | |
297 qq_data *qd; | |
298 guint8 *buf; | |
299 gint len; | |
300 | |
301 gc = (GaimConnection *) data; | |
14498
26bca01c7076
[gaim-migrate @ 17217]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14195
diff
changeset
|
302 |
26bca01c7076
[gaim-migrate @ 17217]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14195
diff
changeset
|
303 if(cond != GAIM_INPUT_READ) { |
14195 | 304 gaim_connection_error(gc, _("Socket error")); |
305 return; | |
306 } | |
14192 | 307 |
308 qd = (qq_data *) gc->proto_data; | |
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 } | |
319 } |