comparison libpurple/proxy.c @ 32094:380f530c3f86

merged from im.pidgin.pidgin
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Tue, 07 Jun 2011 13:42:47 +0900
parents 3e11fa72c5d8
children 5498ee1aa30b
comparison
equal deleted inserted replaced
31933:6b3bc0947068 32094:380f530c3f86
27 27
28 /* this is a little piece of code to handle proxy connection */ 28 /* this is a little piece of code to handle proxy connection */
29 /* it is intended to : 1st handle http proxy, using the CONNECT command 29 /* it is intended to : 1st handle http proxy, using the CONNECT command
30 , 2nd provide an easy way to add socks support 30 , 2nd provide an easy way to add socks support
31 , 3rd draw women to it like flies to honey */ 31 , 3rd draw women to it like flies to honey */
32 #define _PURPLE_PROXY_C_
32 33
33 #include "internal.h" 34 #include "internal.h"
34 #include "cipher.h" 35 #include "cipher.h"
35 #include "debug.h" 36 #include "debug.h"
36 #include "dnsquery.h" 37 #include "dnsquery.h"
2395 handles = g_slist_prepend(handles, connect_data); 2396 handles = g_slist_prepend(handles, connect_data);
2396 2397
2397 return connect_data; 2398 return connect_data;
2398 } 2399 }
2399 2400
2400 /*
2401 * Combine some of this code with purple_proxy_connect()
2402 */
2403 PurpleProxyConnectData * 2401 PurpleProxyConnectData *
2404 purple_proxy_connect_socks5(void *handle, PurpleProxyInfo *gpi, 2402 purple_proxy_connect_socks5(void *handle, PurpleProxyInfo *gpi,
2405 const char *host, int port, 2403 const char *host, int port,
2406 PurpleProxyConnectFunction connect_cb, 2404 PurpleProxyConnectFunction connect_cb,
2407 gpointer data) 2405 gpointer data)
2408 { 2406 {
2409 return purple_proxy_connect_socks5_account(NULL, handle, gpi, 2407 return purple_proxy_connect_socks5_account(NULL, handle, gpi,
2410 host, port, connect_cb, data); 2408 host, port, connect_cb, data);
2411 } 2409 }
2410
2411
2412 /* This is called when we connect to the SOCKS5 proxy server (through any
2413 * relevant account proxy)
2414 */
2415 static void socks5_connected_to_proxy(gpointer data, gint source,
2416 const gchar *error_message) {
2417 /* This is the PurpleProxyConnectData for the overall SOCKS5 connection */
2418 PurpleProxyConnectData *connect_data = data;
2419
2420 /* Check that the overall SOCKS5 connection wasn't cancelled while we were
2421 * connecting to it (we don't have a way of associating the process of
2422 * connecting to the SOCKS5 server to the overall PurpleProxyConnectData)
2423 */
2424 if (!PURPLE_PROXY_CONNECT_DATA_IS_VALID(connect_data))
2425 return;
2426
2427 if (error_message != NULL) {
2428 purple_debug_error("proxy", "Unable to connect to SOCKS5 host.\n");
2429 connect_data->connect_cb(connect_data->data, source, error_message);
2430 return;
2431 }
2432
2433 purple_debug_info("proxy", "Initiating SOCKS5 negotiation.\n");
2434
2435 purple_debug_info("proxy",
2436 "Connecting to %s:%d via %s:%d using SOCKS5\n",
2437 connect_data->host, connect_data->port,
2438 purple_proxy_info_get_host(connect_data->gpi),
2439 purple_proxy_info_get_port(connect_data->gpi));
2440
2441 connect_data->fd = source;
2442
2443 s5_canwrite(connect_data, connect_data->fd, PURPLE_INPUT_WRITE);
2444 }
2445
2412 /* 2446 /*
2413 * Combine some of this code with purple_proxy_connect() 2447 * Combine some of this code with purple_proxy_connect()
2414 */ 2448 */
2415 PurpleProxyConnectData * 2449 PurpleProxyConnectData *
2416 purple_proxy_connect_socks5_account(void *handle, PurpleAccount *account, 2450 purple_proxy_connect_socks5_account(void *handle, PurpleAccount *account,
2418 const char *host, int port, 2452 const char *host, int port,
2419 PurpleProxyConnectFunction connect_cb, 2453 PurpleProxyConnectFunction connect_cb,
2420 gpointer data) 2454 gpointer data)
2421 { 2455 {
2422 PurpleProxyConnectData *connect_data; 2456 PurpleProxyConnectData *connect_data;
2457 PurpleProxyConnectData *account_proxy_conn_data;
2423 2458
2424 g_return_val_if_fail(host != NULL, NULL); 2459 g_return_val_if_fail(host != NULL, NULL);
2425 g_return_val_if_fail(port >= 0, NULL); 2460 g_return_val_if_fail(port >= 0, NULL);
2426 g_return_val_if_fail(connect_cb != NULL, NULL); 2461 g_return_val_if_fail(connect_cb != NULL, NULL);
2427 2462
2434 connect_data->host = g_strdup(host); 2469 connect_data->host = g_strdup(host);
2435 connect_data->port = port; 2470 connect_data->port = port;
2436 connect_data->gpi = gpi; 2471 connect_data->gpi = gpi;
2437 connect_data->account = account; 2472 connect_data->account = account;
2438 2473
2439 connect_data->query_data = 2474 /* If there is an account proxy, use it to connect to the desired SOCKS5
2440 purple_dnsquery_a_account(account, 2475 * proxy.
2441 purple_proxy_info_get_host(gpi), 2476 */
2442 purple_proxy_info_get_port(gpi), 2477 account_proxy_conn_data = purple_proxy_connect(connect_data->handle,
2443 connection_host_resolved, connect_data); 2478 connect_data->account,
2444 if (connect_data->query_data == NULL) 2479 purple_proxy_info_get_host(connect_data->gpi),
2445 { 2480 purple_proxy_info_get_port(connect_data->gpi),
2481 socks5_connected_to_proxy, connect_data);
2482
2483 if (account_proxy_conn_data == NULL) {
2484 purple_debug_error("proxy", "Unable to initiate connection to account proxy.\n");
2446 purple_proxy_connect_data_destroy(connect_data); 2485 purple_proxy_connect_data_destroy(connect_data);
2447 return NULL; 2486 return NULL;
2448 } 2487 }
2488
2489 /* The API doesn't really provide us with a way to cancel the specific
2490 * proxy connection attempt (account_proxy_conn_data) when the overall
2491 * SOCKS5 connection (connect_data) attempt is cancelled :(
2492 */
2449 2493
2450 handles = g_slist_prepend(handles, connect_data); 2494 handles = g_slist_prepend(handles, connect_data);
2451 2495
2452 return connect_data; 2496 return connect_data;
2453 } 2497 }