Mercurial > pidgin
annotate libgaim/protocols/qq/recv_core.c @ 14629:6b8bc59414f0
[gaim-migrate @ 17375]
Get rid of these useless checks.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Huetsch <markhuetsch> |
---|---|
date | Tue, 26 Sep 2006 22:54:24 +0000 |
parents | 26bca01c7076 |
children | d1a76ccb93c4 |
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)) { | |
93 gaim_debug(GAIM_DEBUG_WARNING, "QQ", | |
94 ">>> [%d] %s, %d bytes -> [default] decrypt and dump\n%s", | |
95 seq, qq_get_cmd_desc(cmd), buf_len, hex_dump_to_str(data, len)); | |
96 try_dump_as_gbk(data, len); | |
97 } else { | |
98 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Fail decrypt packet with default process\n"); | |
99 } | |
100 } | |
101 | |
102 /* process the incoming packet from qq_pending */ | |
103 static void _qq_packet_process(guint8 *buf, gint buf_len, GaimConnection *gc) | |
104 { | |
105 qq_data *qd; | |
106 gint len, bytes_expected, bytes_read; | |
107 guint16 buf_len_read; /* two bytes in the begining of TCP packet */ | |
108 guint8 *cursor; | |
109 qq_recv_msg_header header; | |
110 packet_before_login *b4_packet; | |
111 | |
112 g_return_if_fail(buf != NULL && buf_len > 0); | |
113 | |
114 qd = (qq_data *) gc->proto_data; | |
115 bytes_expected = qd->use_tcp ? QQ_TCP_HEADER_LENGTH : QQ_UDP_HEADER_LENGTH; | |
116 | |
117 if (buf_len < bytes_expected) { | |
118 gaim_debug(GAIM_DEBUG_ERROR, | |
119 "QQ", "Received packet is too short, dump and drop\n%s", hex_dump_to_str(buf, buf_len)); | |
120 return; | |
121 } | |
122 /* initialize */ | |
123 cursor = buf; | |
124 bytes_read = 0; | |
125 | |
126 /* QQ TCP packet returns first 2 bytes the length of this packet */ | |
127 if (qd->use_tcp) { | |
128 bytes_read += read_packet_w(buf, &cursor, buf_len, &buf_len_read); | |
129 if (buf_len_read != buf_len) { /* wrong */ | |
130 gaim_debug | |
131 (GAIM_DEBUG_ERROR, | |
132 "QQ", | |
133 "TCP read %d bytes, header says %d bytes, use header anyway\n", buf_len, buf_len_read); | |
134 buf_len = buf_len_read; /* we believe header is more accurate */ | |
135 } | |
136 } | |
137 | |
138 /* now goes the normal QQ packet as UDP packet */ | |
139 bytes_read += read_packet_b(buf, &cursor, buf_len, &header.header_tag); | |
140 bytes_read += read_packet_w(buf, &cursor, buf_len, &header.source_tag); | |
141 bytes_read += read_packet_w(buf, &cursor, buf_len, &header.cmd); | |
142 bytes_read += read_packet_w(buf, &cursor, buf_len, &header.seq); | |
143 | |
144 if (bytes_read != bytes_expected) { /* read error */ | |
145 gaim_debug(GAIM_DEBUG_ERROR, "QQ", | |
146 "Fail reading packet header, expect %d bytes, read %d bytes\n", | |
147 bytes_expected, bytes_read); | |
148 return; | |
149 } | |
150 | |
151 if ((buf[buf_len - 1] != QQ_PACKET_TAIL) || (header.header_tag != QQ_PACKET_TAG)) { | |
152 gaim_debug(GAIM_DEBUG_ERROR, | |
153 "QQ", "Unknown QQ proctocol, dump and drop\n%s", hex_dump_to_str(buf, buf_len)); | |
154 return; | |
155 } | |
156 | |
157 if (QQ_DEBUG) | |
158 gaim_debug(GAIM_DEBUG_INFO, "QQ", | |
159 "==> [%05d] %s, from (%s)\n", | |
160 header.seq, qq_get_cmd_desc(header.cmd), qq_get_source_str(header.source_tag)); | |
161 | |
162 if (header.cmd != QQ_CMD_LOGIN && header.cmd != QQ_CMD_REQUEST_LOGIN_TOKEN) { | |
163 if (!qd->logged_in) { /* packets before login */ | |
164 b4_packet = g_new0(packet_before_login, 1); | |
165 /* must duplicate, buffer will be freed after exiting this function */ | |
166 b4_packet->buf = g_memdup(buf, buf_len); | |
167 b4_packet->len = buf_len; | |
168 if (qd->before_login_packets == NULL) | |
169 qd->before_login_packets = g_queue_new(); | |
170 g_queue_push_head(qd->before_login_packets, b4_packet); | |
171 return; /* do not process it now */ | |
172 } else if (!g_queue_is_empty(qd->before_login_packets)) { | |
173 /* logged_in, but we have packets before login */ | |
174 b4_packet = (packet_before_login *) | |
175 g_queue_pop_head(qd->before_login_packets); | |
176 _qq_packet_process(b4_packet->buf, b4_packet->len, gc); | |
177 /* in fact this is a recursive call, | |
178 * all packets before login will be processed before goes on */ | |
179 g_free(b4_packet->buf); /* the buf is duplicated, need to be freed */ | |
180 g_free(b4_packet); | |
181 } | |
182 } | |
183 | |
184 /* this is the length of all the encrypted data (also remove tail tag */ | |
185 len = buf_len - (bytes_read) - 1; | |
186 | |
187 /* whether it is an ack */ | |
188 switch (header.cmd) { | |
189 case QQ_CMD_RECV_IM: | |
190 case QQ_CMD_RECV_MSG_SYS: | |
191 case QQ_CMD_RECV_MSG_FRIEND_CHANGE_STATUS: | |
192 /* server intiated packet, we need to send ack and check duplicaion | |
193 * this must be put after processing b4_packet | |
194 * as these packets will be passed in twice */ | |
195 if (_qq_check_packet_set_window(header.seq, gc)) { | |
196 gaim_debug(GAIM_DEBUG_WARNING, | |
197 "QQ", "dup [%05d] %s, discard...\n", header.seq, qq_get_cmd_desc(header.cmd)); | |
198 return; | |
199 } | |
200 break; | |
201 default:{ /* ack packet, we need to update sendqueue */ | |
202 /* we do not check duplication for server ack */ | |
203 qq_sendqueue_remove(qd, header.seq); | |
204 if (QQ_DEBUG) | |
205 gaim_debug(GAIM_DEBUG_INFO, "QQ", | |
206 "ack [%05d] %s, remove from sendqueue\n", | |
207 header.seq, qq_get_cmd_desc(header.cmd)); | |
208 } | |
209 } | |
210 | |
211 /* now process the packet */ | |
212 switch (header.cmd) { | |
213 case QQ_CMD_KEEP_ALIVE: | |
214 qq_process_keep_alive_reply(cursor, len, gc); | |
215 break; | |
216 case QQ_CMD_UPDATE_INFO: | |
217 qq_process_modify_info_reply(cursor, len, gc); | |
218 break; | |
219 case QQ_CMD_ADD_FRIEND_WO_AUTH: | |
220 qq_process_add_buddy_reply(cursor, len, header.seq, gc); | |
221 break; | |
222 case QQ_CMD_DEL_FRIEND: | |
223 qq_process_remove_buddy_reply(cursor, len, gc); | |
224 break; | |
225 case QQ_CMD_REMOVE_SELF: | |
226 qq_process_remove_self_reply(cursor, len, gc); | |
227 break; | |
228 case QQ_CMD_BUDDY_AUTH: | |
229 qq_process_add_buddy_auth_reply(cursor, len, gc); | |
230 break; | |
231 case QQ_CMD_GET_USER_INFO: | |
232 qq_process_get_info_reply(cursor, len, gc); | |
233 break; | |
234 case QQ_CMD_CHANGE_ONLINE_STATUS: | |
235 qq_process_change_status_reply(cursor, len, gc); | |
236 break; | |
237 case QQ_CMD_SEND_IM: | |
238 qq_process_send_im_reply(cursor, len, gc); | |
239 break; | |
240 case QQ_CMD_RECV_IM: | |
241 qq_process_recv_im(cursor, len, header.seq, gc); | |
242 break; | |
243 case QQ_CMD_LOGIN: | |
244 qq_process_login_reply(cursor, len, gc); | |
245 break; | |
246 case QQ_CMD_GET_FRIENDS_LIST: | |
247 qq_process_get_buddies_list_reply(cursor, len, gc); | |
248 break; | |
249 case QQ_CMD_GET_FRIENDS_ONLINE: | |
250 qq_process_get_buddies_online_reply(cursor, len, gc); | |
251 break; | |
252 case QQ_CMD_GROUP_CMD: | |
253 qq_process_group_cmd_reply(cursor, len, header.seq, gc); | |
254 break; | |
255 case QQ_CMD_GET_ALL_LIST_WITH_GROUP: | |
256 qq_process_get_all_list_with_group_reply(cursor, len, gc); | |
257 break; | |
258 case QQ_CMD_REQUEST_LOGIN_TOKEN: | |
259 qq_process_request_login_token_reply(cursor, len, gc); | |
260 break; | |
261 case QQ_CMD_RECV_MSG_SYS: | |
262 qq_process_msg_sys(cursor, len, header.seq, gc); | |
263 break; | |
264 case QQ_CMD_RECV_MSG_FRIEND_CHANGE_STATUS: | |
265 qq_process_friend_change_status(cursor, len, gc); | |
266 break; | |
267 default: | |
268 _qq_process_packet_default(cursor, len, header.cmd, header.seq, gc); | |
269 break; | |
270 } | |
271 } | |
272 | |
273 /* clean up the packets before login */ | |
274 void qq_b4_packets_free(qq_data *qd) | |
275 { | |
276 packet_before_login *b4_packet; | |
277 g_return_if_fail(qd != NULL); | |
278 /* now clean up my own data structures */ | |
279 if (qd->before_login_packets != NULL) { | |
280 while (NULL != (b4_packet = g_queue_pop_tail(qd->before_login_packets))) { | |
281 g_free(b4_packet->buf); | |
282 g_free(b4_packet); | |
283 } | |
284 g_queue_free(qd->before_login_packets); | |
285 } | |
286 } | |
287 | |
288 void qq_input_pending(gpointer data, gint source, GaimInputCondition cond) | |
289 { | |
290 GaimConnection *gc; | |
291 qq_data *qd; | |
292 guint8 *buf; | |
293 gint len; | |
294 | |
295 gc = (GaimConnection *) data; | |
14498
26bca01c7076
[gaim-migrate @ 17217]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14195
diff
changeset
|
296 |
26bca01c7076
[gaim-migrate @ 17217]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14195
diff
changeset
|
297 if(cond != GAIM_INPUT_READ) { |
14195 | 298 gaim_connection_error(gc, _("Socket error")); |
299 return; | |
300 } | |
14192 | 301 |
302 qd = (qq_data *) gc->proto_data; | |
303 buf = g_newa(guint8, MAX_PACKET_SIZE); | |
304 | |
305 /* here we have UDP proxy suppport */ | |
306 len = qq_proxy_read(qd, buf, MAX_PACKET_SIZE); | |
307 if (len <= 0) { | |
308 gaim_connection_error(gc, _("Unable to read from socket")); | |
309 return; | |
310 } else { | |
311 _qq_packet_process(buf, len, gc); | |
312 } | |
313 } |