comparison libpurple/protocols/qq/qq_proxy.c @ 15373:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children 32c366eeeb99
comparison
equal deleted inserted replaced
15372:f79e0f4df793 15373:5fe8042783c1
1 /**
2 * @file qq_proxy.c
3 *
4 * gaim
5 *
6 * Gaim is the legal property of its developers, whose names are too numerous
7 * to list here. Please refer to the COPYRIGHT file distributed with this
8 * source distribution.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25 #include "cipher.h"
26 #include "debug.h"
27 #include "internal.h"
28
29 #ifdef _WIN32
30 #define random rand
31 #define srandom srand
32 #endif
33
34 #include "packet_parse.h"
35 #include "buddy_info.h"
36 #include "buddy_opt.h"
37 #include "char_conv.h"
38 #include "group_free.h"
39 #include "login_logout.h"
40 #include "qq_proxy.h"
41 #include "recv_core.h"
42 #include "send_core.h"
43 #include "sendqueue.h"
44 #include "udp_proxy_s5.h"
45 #include "utils.h"
46
47 /* These functions are used only in development phase */
48 /*
49 static void _qq_show_socket(gchar *desc, gint fd) {
50 struct sockaddr_in sin;
51 socklen_t len = sizeof(sin);
52 getsockname(fd, (struct sockaddr *)&sin, &len);
53 gaim_debug(GAIM_DEBUG_INFO, desc, "%s:%d\n",
54 inet_ntoa(sin.sin_addr), g_ntohs(sin.sin_port));
55 }
56 */
57
58 void _qq_show_packet(const gchar *desc, const guint8 *buf, gint len)
59 {
60 char buf1[8*len+2], buf2[10];
61 int i;
62 buf1[0] = 0;
63 for (i = 0; i < len; i++) {
64 sprintf(buf2, " %02x(%d)", buf[i] & 0xff, buf[i] & 0xff);
65 strcat(buf1, buf2);
66 }
67 strcat(buf1, "\n");
68 gaim_debug(GAIM_DEBUG_INFO, desc, "%s", buf1);
69 }
70
71 /* QQ 2003iii uses double MD5 for the pwkey to get the session key */
72 static guint8 *_gen_pwkey(const gchar *pwd)
73 {
74 GaimCipher *cipher;
75 GaimCipherContext *context;
76
77 guchar pwkey_tmp[QQ_KEY_LENGTH];
78
79 cipher = gaim_ciphers_find_cipher("md5");
80 context = gaim_cipher_context_new(cipher, NULL);
81 gaim_cipher_context_append(context, (guchar *) pwd, strlen(pwd));
82 gaim_cipher_context_digest(context, sizeof(pwkey_tmp), pwkey_tmp, NULL);
83 gaim_cipher_context_destroy(context);
84 context = gaim_cipher_context_new(cipher, NULL);
85 gaim_cipher_context_append(context, pwkey_tmp, QQ_KEY_LENGTH);
86 gaim_cipher_context_digest(context, sizeof(pwkey_tmp), pwkey_tmp, NULL);
87 gaim_cipher_context_destroy(context);
88
89 return g_memdup(pwkey_tmp, QQ_KEY_LENGTH);
90 }
91
92 static gboolean _qq_fill_host(GSList *hosts, struct sockaddr_in *addr, gint *addr_size)
93 {
94 if (!hosts || !hosts->data)
95 return FALSE;
96
97 *addr_size = GPOINTER_TO_INT(hosts->data);
98
99 hosts = g_slist_remove(hosts, hosts->data);
100 memcpy(addr, hosts->data, *addr_size);
101 g_free(hosts->data);
102 hosts = g_slist_remove(hosts, hosts->data);
103 while(hosts) {
104 hosts = g_slist_remove(hosts, hosts->data);
105 g_free(hosts->data);
106 hosts = g_slist_remove(hosts, hosts->data);
107 }
108
109 return TRUE;
110 }
111
112 /* set up any finalizing start-up stuff */
113 static void _qq_start_services(GaimConnection *gc)
114 {
115 /* start watching for IMs about to be sent */
116 /*
117 gaim_signal_connect(gaim_conversations_get_handle(),
118 "sending-im-msg", gc,
119 GAIM_CALLBACK(qq_sending_im_msg_cb), NULL);
120 */
121 }
122
123 /* the callback function after socket is built
124 * we setup the qq protocol related configuration here */
125 static void _qq_got_login(gpointer data, gint source, const gchar *error_message)
126 {
127 qq_data *qd;
128 GaimConnection *gc;
129 gchar *buf;
130 const gchar *passwd;
131
132 gc = (GaimConnection *) data;
133
134 if (!GAIM_CONNECTION_IS_VALID(gc)) {
135 close(source);
136 return;
137 }
138
139 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
140
141 if (source < 0) { /* socket returns -1 */
142 gaim_connection_error(gc, error_message);
143 return;
144 }
145
146 qd = (qq_data *) gc->proto_data;
147
148 /*
149 _qq_show_socket("Got login socket", source);
150 */
151
152 /* QQ use random seq, to minimize duplicated packets */
153 srandom(time(NULL));
154 qd->send_seq = random() & 0x0000ffff;
155 qd->fd = source;
156 qd->logged_in = FALSE;
157 qd->channel = 1;
158 qd->uid = strtol(gaim_account_get_username(gaim_connection_get_account(gc)), NULL, 10);
159
160 /* now generate md5 processed passwd */
161 passwd = gaim_account_get_password(gaim_connection_get_account(gc));
162 qd->pwkey = _gen_pwkey(passwd);
163
164 qd->sendqueue_timeout = gaim_timeout_add(QQ_SENDQUEUE_TIMEOUT, qq_sendqueue_timeout_callback, gc);
165 gc->inpa = gaim_input_add(qd->fd, GAIM_INPUT_READ, qq_input_pending, gc);
166
167 /* Update the login progress status display */
168 buf = g_strdup_printf("Login as %d", qd->uid);
169 gaim_connection_update_progress(gc, buf, 1, QQ_CONNECT_STEPS);
170 g_free(buf);
171
172 _qq_start_services(gc);
173
174 qq_send_packet_request_login_token(gc);
175 }
176
177 /* clean up qq_data structure and all its components
178 * always used before a redirectly connection */
179 static void _qq_common_clean(GaimConnection *gc)
180 {
181 qq_data *qd;
182
183 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
184 qd = (qq_data *) gc->proto_data;
185
186 /* finish all I/O */
187 if (qd->fd >= 0 && qd->logged_in)
188 qq_send_packet_logout(gc);
189 close(qd->fd);
190
191 if (qd->sendqueue_timeout > 0) {
192 gaim_timeout_remove(qd->sendqueue_timeout);
193 qd->sendqueue_timeout = 0;
194 }
195
196 if (gc->inpa > 0) {
197 gaim_input_remove(gc->inpa);
198 gc->inpa = 0;
199 }
200
201 qq_b4_packets_free(qd);
202 qq_sendqueue_free(qd);
203 qq_group_packets_free(qd);
204 qq_group_free_all(qd);
205 qq_add_buddy_request_free(qd);
206 qq_info_query_free(qd);
207 qq_buddies_list_free(gc->account, qd);
208 }
209
210 static void no_one_calls(gpointer data, gint source, GaimInputCondition cond)
211 {
212 struct PHB *phb = data;
213 socklen_t len;
214 int error=0, ret;
215
216 gaim_debug_info("proxy", "Connected.\n");
217
218 len = sizeof(error);
219
220 /*
221 * getsockopt after a non-blocking connect returns -1 if something is
222 * really messed up (bad descriptor, usually). Otherwise, it returns 0 and
223 * error holds what connect would have returned if it blocked until now.
224 * Thus, error == 0 is success, error == EINPROGRESS means "try again",
225 * and anything else is a real error.
226 *
227 * (error == EINPROGRESS can happen after a select because the kernel can
228 * be overly optimistic sometimes. select is just a hint that you might be
229 * able to do something.)
230 */
231 ret = getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len);
232 if (ret == 0 && error == EINPROGRESS)
233 return; /* we'll be called again later */
234 if (ret < 0 || error != 0) {
235 if(ret!=0)
236 error = errno;
237 close(source);
238 gaim_input_remove(phb->inpa);
239
240 gaim_debug_error("proxy", "getsockopt SO_ERROR check: %s\n", strerror(error));
241
242 phb->func(phb->data, -1, _("Unable to connect"));
243 return;
244 }
245
246 gaim_input_remove(phb->inpa);
247
248 if (phb->account == NULL || gaim_account_get_connection(phb->account) != NULL) {
249
250 phb->func(phb->data, source, NULL);
251 }
252
253 g_free(phb->host);
254 g_free(phb);
255 }
256
257 /* returns -1 if fails, otherwise returns the file handle */
258 static gint _qq_proxy_none(struct PHB *phb, struct sockaddr *addr, socklen_t addrlen)
259 {
260 gint fd = -1;
261
262 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Using UDP without proxy\n");
263 fd = socket(PF_INET, SOCK_DGRAM, 0);
264
265 if (fd < 0) {
266 gaim_debug(GAIM_DEBUG_ERROR, "QQ Redirect",
267 "Unable to create socket: %s\n", strerror(errno));
268 return -1;
269 }
270
271 /* we use non-blocking mode to speed up connection */
272 fcntl(fd, F_SETFL, O_NONBLOCK);
273
274 /* From Unix-socket-FAQ: http://www.faqs.org/faqs/unix-faq/socket/
275 *
276 * If a UDP socket is unconnected, which is the normal state after a
277 * bind() call, then send() or write() are not allowed, since no
278 * destination is available; only sendto() can be used to send data.
279 *
280 * Calling connect() on the socket simply records the specified address
281 * and port number as being the desired communications partner. That
282 * means that send() or write() are now allowed; they use the destination
283 * address and port given on the connect call as the destination of packets.
284 */
285 if (connect(fd, addr, addrlen) < 0) {
286 /* [EINPROGRESS]
287 * The socket is marked as non-blocking and the connection cannot be
288 * completed immediately. It is possible to select for completion by
289 * selecting the socket for writing.
290 * [EINTR]
291 * A signal interrupted the call.
292 * The connection is established asynchronously.
293 */
294 if ((errno == EINPROGRESS) || (errno == EINTR)) {
295 gaim_debug(GAIM_DEBUG_WARNING, "QQ", "Connect in asynchronous mode.\n");
296 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, no_one_calls, phb);
297 } else {
298 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Connection failed: %d\n", strerror(errno));
299 close(fd);
300 return -1;
301 } /* if errno */
302 } else { /* connect returns 0 */
303 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Connected.\n");
304 fcntl(fd, F_SETFL, 0);
305 phb->func(phb->data, fd, NULL);
306 }
307
308 return fd;
309 }
310
311 static void _qq_proxy_resolved(GSList *hosts, gpointer data, const char *error_message)
312 {
313 struct PHB *phb = (struct PHB *) data;
314 struct sockaddr_in addr;
315 gint addr_size, ret = -1;
316
317 if(_qq_fill_host(hosts, &addr, &addr_size))
318 ret = qq_proxy_socks5(phb, (struct sockaddr *) &addr, addr_size);
319
320 if (ret < 0) {
321 phb->func(phb->data, -1, _("Unable to connect"));
322 g_free(phb->host);
323 g_free(phb);
324 }
325 }
326
327 static void _qq_server_resolved(GSList *hosts, gpointer data, const char *error_message)
328 {
329 struct PHB *phb = (struct PHB *) data;
330 GaimConnection *gc = (GaimConnection *) phb->data;
331 qq_data *qd = (qq_data *) gc->proto_data;
332 struct sockaddr_in addr;
333 gint addr_size, ret = -1;
334
335 if(_qq_fill_host(hosts, &addr, &addr_size)) {
336 switch (gaim_proxy_info_get_type(phb->gpi)) {
337 case GAIM_PROXY_NONE:
338 ret = _qq_proxy_none(phb, (struct sockaddr *) &addr, addr_size);
339 break;
340 case GAIM_PROXY_SOCKS5:
341 ret = 0;
342 if (gaim_proxy_info_get_host(phb->gpi) == NULL ||
343 gaim_proxy_info_get_port(phb->gpi) == 0) {
344 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
345 "Use of socks5 proxy selected but host or port info doesn't exist.\n");
346 ret = -1;
347 } else {
348 /* as the destination is always QQ server during the session,
349 * we can set dest_sin here, instead of _qq_s5_canread_again */
350 memcpy(&qd->dest_sin, &addr, addr_size);
351 if (gaim_dnsquery_a(gaim_proxy_info_get_host(phb->gpi),
352 gaim_proxy_info_get_port(phb->gpi),
353 _qq_proxy_resolved, phb) == NULL)
354 ret = -1;
355 }
356 break;
357 default:
358 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
359 "Proxy type %i is unsupported, not using a proxy.\n",
360 gaim_proxy_info_get_type(phb->gpi));
361 ret = _qq_proxy_none(phb, (struct sockaddr *) &addr, addr_size);
362 }
363 }
364
365 if (ret < 0) {
366 phb->func(gc, -1, _("Unable to connect"));
367 g_free(phb->host);
368 g_free(phb);
369 }
370 }
371
372 /* returns -1 if dns lookup fails, otherwise returns 0 */
373 static gint _qq_udp_proxy_connect(GaimAccount *account,
374 const gchar *server, guint16 port,
375 void callback(gpointer, gint, const gchar *error_message),
376 GaimConnection *gc)
377 {
378 GaimProxyInfo *info;
379 struct PHB *phb;
380 qq_data *qd = (qq_data *) gc->proto_data;
381
382 g_return_val_if_fail(gc != NULL && qd != NULL, -1);
383
384 info = gaim_proxy_get_setup(account);
385
386 phb = g_new0(struct PHB, 1);
387 phb->host = g_strdup(server);
388 phb->port = port;
389 phb->account = account;
390 phb->gpi = info;
391 phb->func = callback;
392 phb->data = gc;
393 qd->proxy_type = gaim_proxy_info_get_type(phb->gpi);
394
395 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Choosing proxy type %d\n",
396 gaim_proxy_info_get_type(phb->gpi));
397
398 if (gaim_dnsquery_a(server, port, _qq_server_resolved, phb) == NULL) {
399 phb->func(gc, -1, _("Unable to connect"));
400 g_free(phb->host);
401 g_free(phb);
402 return -1;
403 } else {
404 return 0;
405 }
406 }
407
408 /* QQ connection via UDP/TCP.
409 * I use Gaim proxy function to provide TCP proxy support,
410 * and qq_udp_proxy.c to add UDP proxy support (thanks henry) */
411 static gint _proxy_connect_full (GaimAccount *account, const gchar *host, guint16 port,
412 GaimProxyConnectFunction func, gpointer data, gboolean use_tcp)
413 {
414 GaimConnection *gc;
415 qq_data *qd;
416
417 gc = gaim_account_get_connection(account);
418 qd = (qq_data *) gc->proto_data;
419 qd->server_ip = g_strdup(host);
420 qd->server_port = port;
421
422 if(use_tcp)
423 return (gaim_proxy_connect(NULL, account, host, port, func, data) == NULL);
424 else
425 return _qq_udp_proxy_connect(account, host, port, func, data);
426 }
427
428 /* establish a generic QQ connection
429 * TCP/UDP, and direct/redirected */
430 gint qq_connect(GaimAccount *account, const gchar *host, guint16 port,
431 gboolean use_tcp, gboolean is_redirect)
432 {
433 GaimConnection *gc;
434 qq_data *qd;
435
436 g_return_val_if_fail(host != NULL, -1);
437 g_return_val_if_fail(port > 0, -1);
438
439 gc = gaim_account_get_connection(account);
440 g_return_val_if_fail(gc != NULL && gc->proto_data != NULL, -1);
441
442 if (is_redirect)
443 _qq_common_clean(gc);
444
445 qd = (qq_data *) gc->proto_data;
446 qd->before_login_packets = g_queue_new();
447
448 return _proxy_connect_full(account, host, port, _qq_got_login, gc, use_tcp);
449 }
450
451 /* clean up the given QQ connection and free all resources */
452 void qq_disconnect(GaimConnection *gc)
453 {
454 qq_data *qd;
455
456 g_return_if_fail(gc != NULL);
457
458 _qq_common_clean(gc);
459
460 qd = gc->proto_data;
461 g_free(qd->inikey);
462 g_free(qd->pwkey);
463 g_free(qd->session_key);
464 g_free(qd->session_md5);
465 g_free(qd->my_ip);
466 g_free(qd);
467
468 gc->proto_data = NULL;
469 }
470
471 /* send packet with proxy support */
472 gint qq_proxy_write(qq_data *qd, guint8 *data, gint len)
473 {
474 guint8 *buf;
475 gint ret;
476
477 g_return_val_if_fail(qd != NULL && qd->fd >= 0 && data != NULL && len > 0, -1);
478
479 /* TCP sock5 may be processed twice
480 * so we need to check qd->use_tcp as well */
481 if ((!qd->use_tcp) && qd->proxy_type == GAIM_PROXY_SOCKS5) { /* UDP sock5 */
482 buf = g_newa(guint8, len + 10);
483 buf[0] = 0x00;
484 buf[1] = 0x00; /* reserved */
485 buf[2] = 0x00; /* frag */
486 buf[3] = 0x01; /* type */
487 g_memmove(buf + 4, &(qd->dest_sin.sin_addr.s_addr), 4);
488 g_memmove(buf + 8, &(qd->dest_sin.sin_port), 2);
489 g_memmove(buf + 10, data, len);
490 errno = 0;
491 ret = send(qd->fd, buf, len + 10, 0);
492 } else {
493 errno = 0;
494 ret = send(qd->fd, data, len, 0);
495 }
496 if (ret == -1) {
497 gaim_connection_error(qd->gc, _("Socket send error"));
498 return ret;
499 } else if (errno == ECONNREFUSED) {
500 gaim_connection_error(qd->gc, _("Connection refused"));
501 return ret;
502 }
503
504 return ret;
505 }
506
507 /* read packet input with proxy support */
508 gint qq_proxy_read(qq_data *qd, guint8 *data, gint len)
509 {
510 guint8 *buf;
511 gint bytes;
512 buf = g_newa(guint8, MAX_PACKET_SIZE + 10);
513
514 g_return_val_if_fail(qd != NULL && data != NULL && len > 0, -1);
515 g_return_val_if_fail(qd->fd > 0, -1);
516
517 bytes = read(qd->fd, buf, len + 10);
518 if (bytes < 0)
519 return -1;
520
521 if ((!qd->use_tcp) && qd->proxy_type == GAIM_PROXY_SOCKS5) { /* UDP sock5 */
522 if (bytes < 10)
523 return -1;
524 bytes -= 10;
525 g_memmove(data, buf + 10, bytes); /* cut off the header */
526 } else {
527 g_memmove(data, buf, bytes);
528 }
529
530 return bytes;
531 }