13870
|
1 /**
|
|
2 * The QQ2003C protocol plugin
|
|
3 *
|
|
4 * for gaim
|
|
5 *
|
|
6 * Copyright (C) 2004 Puzzlebird
|
|
7 * Henry Ou <henry@linux.net>
|
|
8 *
|
|
9 * This program is free software; you can redistribute it and/or modify
|
|
10 * it under the terms of the GNU General Public License as published by
|
|
11 * the Free Software Foundation; either version 2 of the License, or
|
|
12 * (at your option) any later version.
|
|
13 *
|
|
14 * This program is distributed in the hope that it will be useful,
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 * GNU General Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU General Public License
|
|
20 * along with this program; if not, write to the Free Software
|
|
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
22 */
|
|
23
|
14021
|
24 #include "cipher.h"
|
|
25 #include "debug.h"
|
|
26 #include "internal.h"
|
13870
|
27
|
|
28 #ifdef _WIN32
|
|
29 #define random rand
|
|
30 #define srandom srand
|
|
31 #endif
|
|
32
|
14021
|
33 #include "packet_parse.h"
|
|
34 #include "buddy_info.h"
|
|
35 #include "buddy_opt.h"
|
|
36 #include "char_conv.h"
|
|
37 #include "group_free.h"
|
|
38 #include "login_logout.h"
|
|
39 #include "qq_proxy.h"
|
|
40 #include "recv_core.h"
|
|
41 #include "send_core.h"
|
|
42 #include "sendqueue.h"
|
|
43 #include "udp_proxy_s5.h"
|
|
44 #include "utils.h"
|
13870
|
45
|
14021
|
46 /* These functions are used only in development phase
|
13870
|
47 *
|
|
48 static void _qq_show_socket(gchar *desc, gint fd) {
|
|
49 struct sockaddr_in sin;
|
|
50 gint len = sizeof(sin);
|
|
51 getsockname(fd, (struct sockaddr *)&sin, &len);
|
|
52 gaim_debug(GAIM_DEBUG_INFO, desc, "%s:%d\n",
|
|
53 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
|
|
54 }
|
|
55 */
|
|
56
|
14045
|
57 void _qq_show_packet(const gchar *desc, const guint8 *buf, gint len)
|
13870
|
58 {
|
14044
|
59 char buf1[8*len+2], buf2[10];
|
13870
|
60 int i;
|
|
61 buf1[0] = 0;
|
|
62 for (i = 0; i < len; i++) {
|
|
63 sprintf(buf2, " %02x(%d)", buf[i] & 0xff, buf[i] & 0xff);
|
|
64 strcat(buf1, buf2);
|
|
65 }
|
|
66 strcat(buf1, "\n");
|
|
67 gaim_debug(GAIM_DEBUG_INFO, desc, buf1);
|
|
68 }
|
|
69
|
14021
|
70 /* QQ 2003iii uses double MD5 for the pwkey to get the session key */
|
|
71 static guint8 *_gen_pwkey(const gchar *pwd)
|
13870
|
72 {
|
|
73 GaimCipher *cipher;
|
|
74 GaimCipherContext *context;
|
|
75
|
14045
|
76 guchar pwkey_tmp[QQ_KEY_LENGTH];
|
13870
|
77
|
|
78 cipher = gaim_ciphers_find_cipher("md5");
|
|
79 context = gaim_cipher_context_new(cipher, NULL);
|
14045
|
80 gaim_cipher_context_append(context, (guchar *) pwd, strlen(pwd));
|
13870
|
81 gaim_cipher_context_digest(context, sizeof(pwkey_tmp), pwkey_tmp, NULL);
|
|
82 gaim_cipher_context_destroy(context);
|
|
83 context = gaim_cipher_context_new(cipher, NULL);
|
|
84 gaim_cipher_context_append(context, pwkey_tmp, QQ_KEY_LENGTH);
|
|
85 gaim_cipher_context_digest(context, sizeof(pwkey_tmp), pwkey_tmp, NULL);
|
|
86 gaim_cipher_context_destroy(context);
|
|
87
|
|
88 return g_memdup(pwkey_tmp, QQ_KEY_LENGTH);
|
14021
|
89 }
|
13870
|
90
|
14021
|
91 gint _qq_fill_host(struct sockaddr_in *addr, const gchar *host, guint16 port)
|
13870
|
92 {
|
|
93 if (!inet_aton(host, &(addr->sin_addr))) {
|
|
94 struct hostent *hp;
|
|
95 if (!(hp = gethostbyname(host))) {
|
|
96 return -1;
|
|
97 }
|
|
98 memset(addr, 0, sizeof(struct sockaddr_in));
|
|
99 memcpy(&(addr->sin_addr.s_addr), hp->h_addr, hp->h_length);
|
|
100 addr->sin_family = hp->h_addrtype;
|
14021
|
101 } else {
|
13870
|
102 addr->sin_family = AF_INET;
|
14021
|
103 }
|
13870
|
104
|
|
105 addr->sin_port = htons(port);
|
|
106 return 0;
|
14021
|
107 }
|
13870
|
108
|
14021
|
109 /* set up any finalizing start-up stuff */
|
13989
|
110 static void _qq_start_services(GaimConnection *gc)
|
|
111 {
|
|
112 /* start watching for IMs about to be sent */
|
|
113 /*
|
|
114 gaim_signal_connect(gaim_conversations_get_handle(),
|
|
115 "sending-im-msg", gc,
|
|
116 GAIM_CALLBACK(qq_sending_im_msg_cb), NULL);
|
|
117 */
|
|
118 }
|
|
119
|
14021
|
120 /* the callback function after socket is built
|
|
121 * we setup the qq protocol related configuration here */
|
14114
|
122 static void _qq_got_login(gpointer data, gint source, const gchar *error_message)
|
13870
|
123 {
|
|
124 qq_data *qd;
|
|
125 GaimConnection *gc;
|
|
126 gchar *buf;
|
|
127 const gchar *passwd;
|
|
128
|
|
129 gc = (GaimConnection *) data;
|
|
130
|
14114
|
131 if (!GAIM_CONNECTION_IS_VALID(gc)) {
|
13870
|
132 close(source);
|
|
133 return;
|
|
134 }
|
|
135
|
14114
|
136 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
|
|
137
|
14021
|
138 if (source < 0) { /* socket returns -1 */
|
13870
|
139 gaim_connection_error(gc, _("Unable to connect."));
|
|
140 return;
|
|
141 }
|
|
142
|
|
143 qd = (qq_data *) gc->proto_data;
|
|
144
|
14021
|
145 /* QQ use random seq, to minimize duplicated packets */
|
13870
|
146 srandom(time(NULL));
|
|
147 qd->send_seq = random() & 0x0000ffff;
|
|
148 qd->fd = source;
|
|
149 qd->logged_in = FALSE;
|
|
150 qd->channel = 1;
|
|
151 qd->uid = strtol(gaim_account_get_username(gaim_connection_get_account(gc)), NULL, 10);
|
|
152 qd->before_login_packets = g_queue_new();
|
|
153
|
14021
|
154 /* now generate md5 processed passwd */
|
13870
|
155 passwd = gaim_account_get_password(gaim_connection_get_account(gc));
|
|
156 qd->pwkey = _gen_pwkey(passwd);
|
|
157
|
|
158 qd->sendqueue_timeout = gaim_timeout_add(QQ_SENDQUEUE_TIMEOUT, qq_sendqueue_timeout_callback, gc);
|
|
159 gc->inpa = gaim_input_add(qd->fd, GAIM_INPUT_READ, qq_input_pending, gc);
|
|
160
|
14021
|
161 /* Update the login progress status display */
|
13870
|
162 buf = g_strdup_printf("Login as %d", qd->uid);
|
|
163 gaim_connection_update_progress(gc, buf, 1, QQ_CONNECT_STEPS);
|
|
164 g_free(buf);
|
|
165
|
13989
|
166 _qq_start_services(gc);
|
|
167
|
13870
|
168 qq_send_packet_request_login_token(gc);
|
14021
|
169 }
|
13870
|
170
|
14021
|
171 /* clean up qq_data structure and all its components
|
|
172 * always used before a redirectly connection */
|
|
173 static void _qq_common_clean(GaimConnection *gc)
|
13870
|
174 {
|
|
175 qq_data *qd;
|
|
176
|
|
177 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
|
|
178 qd = (qq_data *) gc->proto_data;
|
|
179
|
14021
|
180 /* finish all I/O */
|
13870
|
181 if (qd->fd >= 0 && qd->logged_in)
|
|
182 qq_send_packet_logout(gc);
|
|
183 close(qd->fd);
|
|
184
|
|
185 if (qd->sendqueue_timeout > 0) {
|
|
186 gaim_timeout_remove(qd->sendqueue_timeout);
|
|
187 qd->sendqueue_timeout = 0;
|
14021
|
188 }
|
13870
|
189
|
|
190 if (gc->inpa > 0) {
|
|
191 gaim_input_remove(gc->inpa);
|
|
192 gc->inpa = 0;
|
14021
|
193 }
|
13870
|
194
|
|
195 qq_b4_packets_free(qd);
|
|
196 qq_sendqueue_free(qd);
|
|
197 qq_group_packets_free(qd);
|
|
198 qq_group_free_all(qd);
|
|
199 qq_add_buddy_request_free(qd);
|
|
200 qq_info_query_free(qd);
|
14021
|
201 qq_buddies_list_free(gc->account, qd);
|
|
202 }
|
13870
|
203
|
|
204 static gint _qq_proxy_none(struct PHB *phb, struct sockaddr *addr, socklen_t addrlen)
|
|
205 {
|
|
206 gint fd = -1;
|
|
207
|
|
208 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Using UDP without proxy\n");
|
|
209 fd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
210
|
|
211 if (fd < 0) {
|
|
212 gaim_debug(GAIM_DEBUG_ERROR, "QQ Redirect", "Unable to create socket: %s\n", strerror(errno));
|
|
213 return -1;
|
14021
|
214 }
|
13870
|
215
|
14021
|
216 /* we use non-blocking mode to speed up connection */
|
13870
|
217 fcntl(fd, F_SETFL, O_NONBLOCK);
|
|
218
|
|
219 /* From Unix-socket-FAQ: http://www.faqs.org/faqs/unix-faq/socket/
|
|
220 *
|
|
221 * If a UDP socket is unconnected, which is the normal state after a
|
|
222 * bind() call, then send() or write() are not allowed, since no
|
|
223 * destination is available; only sendto() can be used to send data.
|
|
224 *
|
|
225 * Calling connect() on the socket simply records the specified address
|
|
226 * and port number as being the desired communications partner. That
|
|
227 * means that send() or write() are now allowed; they use the destination
|
|
228 * address and port given on the connect call as the destination of packets.
|
|
229 */
|
|
230 if (connect(fd, addr, addrlen) < 0) {
|
|
231 /* [EINPROGRESS]
|
|
232 * The socket is marked as non-blocking and the connection cannot be
|
|
233 * completed immediately. It is possible to select for completion by
|
|
234 * selecting the socket for writing.
|
|
235 * [EINTR]
|
|
236 * A signal interrupted the call.
|
|
237 * The connection is established asynchronously.
|
|
238 */
|
14021
|
239 if ((errno == EINPROGRESS) || (errno == EINTR)) {
|
13870
|
240 gaim_debug(GAIM_DEBUG_WARNING, "QQ", "Connect in asynchronous mode.\n");
|
14021
|
241 } else {
|
13870
|
242 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Faiil connection: %d\n", strerror(errno));
|
|
243 close(fd);
|
|
244 return -1;
|
14021
|
245 } /* if errno */
|
|
246 } else { /* connect returns 0 */
|
13870
|
247 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Connected.\n");
|
|
248 fcntl(fd, F_SETFL, 0);
|
|
249 phb->func(phb->data, fd, GAIM_INPUT_READ);
|
14021
|
250 }
|
13870
|
251
|
|
252 return fd;
|
14021
|
253 }
|
13870
|
254
|
14021
|
255 /* returns the socket handler, or -1 if there is any error */
|
|
256 static gint _qq_udp_proxy_connect(GaimAccount *account,
|
|
257 const gchar *server,
|
|
258 guint16 port, void callback(gpointer, gint, GaimInputCondition), GaimConnection *gc)
|
13870
|
259 {
|
|
260 struct sockaddr_in sin;
|
|
261 struct PHB *phb;
|
|
262 GaimProxyInfo *info;
|
|
263 qq_data *qd;
|
|
264
|
|
265 g_return_val_if_fail(gc != NULL && gc->proto_data != NULL, -1);
|
|
266 qd = (qq_data *) gc->proto_data;
|
|
267
|
|
268 info = gaim_account_get_proxy_info(account);
|
|
269
|
|
270 phb = g_new0(struct PHB, 1);
|
|
271 phb->host = g_strdup(server);
|
|
272 phb->port = port;
|
|
273 phb->account = account;
|
|
274 phb->gpi = info;
|
|
275 phb->func = callback;
|
|
276 phb->data = gc;
|
|
277
|
|
278 if (_qq_fill_host(&sin, server, port) < 0) {
|
|
279 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
|
|
280 "gethostbyname(\"%s\", %d) failed: %s\n", server, port, hstrerror(h_errno));
|
|
281 return -1;
|
14021
|
282 }
|
13870
|
283
|
|
284 if (info == NULL) {
|
|
285 qd->proxy_type = GAIM_PROXY_NONE;
|
|
286 return _qq_proxy_none(phb, (struct sockaddr *) &sin, sizeof(sin));
|
14021
|
287 }
|
13870
|
288
|
|
289 qd->proxy_type = info->type;
|
|
290 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Choosing proxy type %d\n", info->type);
|
|
291
|
|
292 switch (info->type) {
|
|
293 case GAIM_PROXY_NONE:
|
|
294 return _qq_proxy_none(phb, (struct sockaddr *) &sin, sizeof(sin));
|
|
295 case GAIM_PROXY_SOCKS5:
|
14021
|
296 /* as the destination is always QQ server during the session,
|
|
297 * we can set dest_sin here, instead of _qq_s5_canread_again */
|
13870
|
298 _qq_fill_host(&qd->dest_sin, phb->host, phb->port);
|
|
299 _qq_fill_host(&sin, phb->gpi->host, phb->gpi->port);
|
|
300 return qq_proxy_socks5(phb, (struct sockaddr *) &sin, sizeof(sin));
|
|
301 default:
|
|
302 return _qq_proxy_none(phb, (struct sockaddr *) &sin, sizeof(sin));
|
14021
|
303 }
|
13870
|
304
|
|
305 return -1;
|
|
306 }
|
|
307
|
14021
|
308 /* QQ connection via UDP/TCP.
|
|
309 * I use GAIM proxy function to provide TCP proxy support,
|
|
310 * and qq_udp_proxy.c to add UDP proxy support (thanks henry)
|
|
311 * return the socket handle, -1 means fail */
|
|
312 static gint _proxy_connect_full (GaimAccount *account, const gchar *host, guint16 port,
|
14089
|
313 GaimProxyConnectFunction func, gpointer data, gboolean use_tcp)
|
14021
|
314 {
|
13870
|
315 GaimConnection *gc;
|
|
316 qq_data *qd;
|
|
317
|
|
318 gc = gaim_account_get_connection(account);
|
|
319 qd = (qq_data *) gc->proto_data;
|
|
320 qd->server_ip = g_strdup(host);
|
|
321 qd->server_port = port;
|
|
322
|
14089
|
323 if (use_tcp)
|
|
324 /* TCP mode */
|
14108
|
325 return (gaim_proxy_connect(account, host, port, func, data) == NULL);
|
14089
|
326 else
|
|
327 /* UDP mode */
|
|
328 return _qq_udp_proxy_connect(account, host, port, func, data);
|
14021
|
329 }
|
13870
|
330
|
14021
|
331 /* establish a generic QQ connection
|
|
332 * TCP/UDP, and direct/redirected
|
|
333 * return the socket handler, or -1 if there is any error */
|
|
334 gint qq_connect(GaimAccount *account, const gchar *host, guint16 port, gboolean use_tcp, gboolean is_redirect)
|
|
335 {
|
13870
|
336 GaimConnection *gc;
|
|
337
|
|
338 g_return_val_if_fail(host != NULL, -1);
|
|
339 g_return_val_if_fail(port > 0, -1);
|
|
340
|
|
341 gc = gaim_account_get_connection(account);
|
|
342 g_return_val_if_fail(gc != NULL && gc->proto_data != NULL, -1);
|
|
343
|
|
344 if (is_redirect)
|
|
345 _qq_common_clean(gc);
|
|
346
|
|
347 return _proxy_connect_full(account, host, port, _qq_got_login, gc, use_tcp);
|
14021
|
348 }
|
13870
|
349
|
14021
|
350 /* clean up the given QQ connection and free all resources */
|
|
351 void qq_disconnect(GaimConnection *gc)
|
13870
|
352 {
|
|
353 qq_data *qd;
|
|
354
|
|
355 g_return_if_fail(gc != NULL);
|
|
356
|
|
357 _qq_common_clean(gc);
|
|
358
|
|
359 qd = gc->proto_data;
|
|
360 g_free(qd->inikey);
|
|
361 g_free(qd->pwkey);
|
|
362 g_free(qd->session_key);
|
|
363 g_free(qd->my_ip);
|
|
364 g_free(qd);
|
|
365
|
|
366 gc->proto_data = NULL;
|
14021
|
367 }
|
13870
|
368
|
14021
|
369 /* send packet with proxy support */
|
|
370 gint qq_proxy_write(qq_data *qd, guint8 *data, gint len)
|
13870
|
371 {
|
|
372 guint8 *buf;
|
|
373 gint ret;
|
|
374
|
|
375 g_return_val_if_fail(qd != NULL && qd->fd >= 0 && data != NULL && len > 0, -1);
|
|
376
|
14021
|
377 /* TCP sock5 may be processed twice
|
|
378 * so we need to check qd->use_tcp as well */
|
|
379 if ((!qd->use_tcp) && qd->proxy_type == GAIM_PROXY_SOCKS5) { /* UDP sock5 */
|
13870
|
380 buf = g_newa(guint8, len + 10);
|
|
381 buf[0] = 0x00;
|
14021
|
382 buf[1] = 0x00; /* reserved */
|
|
383 buf[2] = 0x00; /* frag */
|
|
384 buf[3] = 0x01; /* type */
|
13870
|
385 g_memmove(buf + 4, &(qd->dest_sin.sin_addr.s_addr), 4);
|
|
386 g_memmove(buf + 8, &(qd->dest_sin.sin_port), 2);
|
|
387 g_memmove(buf + 10, data, len);
|
|
388 ret = send(qd->fd, buf, len + 10, 0);
|
14021
|
389 } else {
|
13870
|
390 ret = send(qd->fd, data, len, 0);
|
14021
|
391 }
|
13870
|
392
|
|
393 return ret;
|
|
394 }
|
|
395
|
14021
|
396 /* read packet input with proxy support */
|
|
397 gint qq_proxy_read(qq_data *qd, guint8 *data, gint len)
|
13870
|
398 {
|
|
399 guint8 *buf;
|
|
400 gint bytes;
|
|
401 buf = g_newa(guint8, MAX_PACKET_SIZE + 10);
|
|
402
|
|
403 g_return_val_if_fail(qd != NULL && data != NULL && len > 0, -1);
|
|
404 g_return_val_if_fail(qd->fd > 0, -1);
|
|
405
|
|
406 bytes = read(qd->fd, buf, len + 10);
|
|
407 if (bytes < 0)
|
|
408 return -1;
|
|
409
|
14021
|
410 if ((!qd->use_tcp) && qd->proxy_type == GAIM_PROXY_SOCKS5) { /* UDP sock5 */
|
13870
|
411 if (bytes < 10)
|
|
412 return -1;
|
|
413 bytes -= 10;
|
14021
|
414 g_memmove(data, buf + 10, bytes); /* cut off the header */
|
|
415 } else {
|
13870
|
416 g_memmove(data, buf, bytes);
|
14021
|
417 }
|
13870
|
418
|
|
419 return bytes;
|
14021
|
420 }
|