Mercurial > pidgin
comparison src/proxy.c @ 14090:983fbec46eb0
[gaim-migrate @ 16713]
Rename struct PHB to struct _GaimProxyConnectInfo
committer: Tailor Script <tailor@pidgin.im>
| author | Mark Doliner <mark@kingant.net> |
|---|---|
| date | Sat, 12 Aug 2006 10:20:19 +0000 |
| parents | 10e8eb6a4910 |
| children | 3b871b67556d |
comparison
equal
deleted
inserted
replaced
| 14089:10e8eb6a4910 | 14090:983fbec46eb0 |
|---|---|
| 37 #include "prefs.h" | 37 #include "prefs.h" |
| 38 #include "proxy.h" | 38 #include "proxy.h" |
| 39 #include "util.h" | 39 #include "util.h" |
| 40 | 40 |
| 41 /* Does anyone know what PHB stands for? */ | 41 /* Does anyone know what PHB stands for? */ |
| 42 struct PHB { | 42 struct _GaimProxyConnectInfo { |
| 43 GaimProxyConnectFunction connect_cb; | 43 GaimProxyConnectFunction connect_cb; |
| 44 GaimProxyErrorFunction error_cb; | 44 GaimProxyErrorFunction error_cb; |
| 45 gpointer data; | 45 gpointer data; |
| 46 char *host; | 46 char *host; |
| 47 int port; | 47 int port; |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 static GaimProxyInfo *global_proxy_info = NULL; | 72 static GaimProxyInfo *global_proxy_info = NULL; |
| 73 static GSList *phbs = NULL; | 73 static GSList *phbs = NULL; |
| 74 | 74 |
| 75 static void try_connect(struct PHB *); | 75 static void try_connect(struct _GaimProxyConnectInfo *); |
| 76 | 76 |
| 77 /************************************************************************** | 77 /************************************************************************** |
| 78 * Proxy structure API | 78 * Proxy structure API |
| 79 **************************************************************************/ | 79 **************************************************************************/ |
| 80 GaimProxyInfo * | 80 GaimProxyInfo * |
| 256 /************************************************************************** | 256 /************************************************************************** |
| 257 * Proxy API | 257 * Proxy API |
| 258 **************************************************************************/ | 258 **************************************************************************/ |
| 259 | 259 |
| 260 static void | 260 static void |
| 261 gaim_proxy_phb_destroy(struct PHB *phb) | 261 gaim_proxy_phb_destroy(struct _GaimProxyConnectInfo *phb) |
| 262 { | 262 { |
| 263 phbs = g_slist_remove(phbs, phb); | 263 phbs = g_slist_remove(phbs, phb); |
| 264 | 264 |
| 265 if (phb->inpa > 0) | 265 if (phb->inpa > 0) |
| 266 gaim_input_remove(phb->inpa); | 266 gaim_input_remove(phb->inpa); |
| 279 g_free(phb->read_buffer); | 279 g_free(phb->read_buffer); |
| 280 g_free(phb); | 280 g_free(phb); |
| 281 } | 281 } |
| 282 | 282 |
| 283 static void | 283 static void |
| 284 gaim_proxy_phb_connected(struct PHB *phb, int fd) | 284 gaim_proxy_phb_connected(struct _GaimProxyConnectInfo *phb, int fd) |
| 285 { | 285 { |
| 286 phb->connect_cb(phb->data, fd); | 286 phb->connect_cb(phb->data, fd); |
| 287 gaim_proxy_phb_destroy(phb); | 287 gaim_proxy_phb_destroy(phb); |
| 288 } | 288 } |
| 289 | 289 |
| 291 * @param error An error message explaining why the connection | 291 * @param error An error message explaining why the connection |
| 292 * failed. This will be passed to the callback function | 292 * failed. This will be passed to the callback function |
| 293 * specified in the call to gaim_proxy_connect(). | 293 * specified in the call to gaim_proxy_connect(). |
| 294 */ | 294 */ |
| 295 static void | 295 static void |
| 296 gaim_proxy_phb_error(struct PHB *phb, const gchar *error_message) | 296 gaim_proxy_phb_error(struct _GaimProxyConnectInfo *phb, const gchar *error_message) |
| 297 { | 297 { |
| 298 if (phb->error_cb == NULL) | 298 if (phb->error_cb == NULL) |
| 299 { | 299 { |
| 300 /* | 300 /* |
| 301 * TODO | 301 * TODO |
| 989 #endif /* not __unix__ or __APPLE__ or _WIN32 */ | 989 #endif /* not __unix__ or __APPLE__ or _WIN32 */ |
| 990 | 990 |
| 991 static void | 991 static void |
| 992 no_one_calls(gpointer data, gint source, GaimInputCondition cond) | 992 no_one_calls(gpointer data, gint source, GaimInputCondition cond) |
| 993 { | 993 { |
| 994 struct PHB *phb = data; | 994 struct _GaimProxyConnectInfo *phb = data; |
| 995 socklen_t len; | 995 socklen_t len; |
| 996 int error=0, ret; | 996 int error=0, ret; |
| 997 | 997 |
| 998 gaim_debug_info("proxy", "Connected.\n"); | 998 gaim_debug_info("proxy", "Connected.\n"); |
| 999 | 999 |
| 1033 gaim_proxy_phb_connected(phb, source); | 1033 gaim_proxy_phb_connected(phb, source); |
| 1034 } | 1034 } |
| 1035 | 1035 |
| 1036 static gboolean clean_connect(gpointer data) | 1036 static gboolean clean_connect(gpointer data) |
| 1037 { | 1037 { |
| 1038 struct PHB *phb = data; | 1038 struct _GaimProxyConnectInfo *phb = data; |
| 1039 | 1039 |
| 1040 gaim_proxy_phb_connected(phb, phb->port); | 1040 gaim_proxy_phb_connected(phb, phb->port); |
| 1041 | 1041 |
| 1042 return FALSE; | 1042 return FALSE; |
| 1043 } | 1043 } |
| 1044 | 1044 |
| 1045 | 1045 |
| 1046 static int | 1046 static int |
| 1047 proxy_connect_none(struct PHB *phb, struct sockaddr *addr, socklen_t addrlen) | 1047 proxy_connect_none(struct _GaimProxyConnectInfo *phb, struct sockaddr *addr, socklen_t addrlen) |
| 1048 { | 1048 { |
| 1049 int fd = -1; | 1049 int fd = -1; |
| 1050 | 1050 |
| 1051 gaim_debug_info("proxy", | 1051 gaim_debug_info("proxy", |
| 1052 "Connecting to %s:%d with no proxy\n", phb->host, phb->port); | 1052 "Connecting to %s:%d with no proxy\n", phb->host, phb->port); |
| 1096 } | 1096 } |
| 1097 | 1097 |
| 1098 static void | 1098 static void |
| 1099 proxy_do_write(gpointer data, gint source, GaimInputCondition cond) | 1099 proxy_do_write(gpointer data, gint source, GaimInputCondition cond) |
| 1100 { | 1100 { |
| 1101 struct PHB *phb = data; | 1101 struct _GaimProxyConnectInfo *phb = data; |
| 1102 const guchar *request = phb->write_buffer + phb->written_len; | 1102 const guchar *request = phb->write_buffer + phb->written_len; |
| 1103 gsize request_len = phb->write_buf_len - phb->written_len; | 1103 gsize request_len = phb->write_buf_len - phb->written_len; |
| 1104 | 1104 |
| 1105 int ret = write(source, request, request_len); | 1105 int ret = write(source, request, request_len); |
| 1106 | 1106 |
| 1134 static void | 1134 static void |
| 1135 http_canread(gpointer data, gint source, GaimInputCondition cond) | 1135 http_canread(gpointer data, gint source, GaimInputCondition cond) |
| 1136 { | 1136 { |
| 1137 int len, headers_len, status = 0; | 1137 int len, headers_len, status = 0; |
| 1138 gboolean error; | 1138 gboolean error; |
| 1139 struct PHB *phb = data; | 1139 struct _GaimProxyConnectInfo *phb = data; |
| 1140 guchar *p; | 1140 guchar *p; |
| 1141 gsize max_read; | 1141 gsize max_read; |
| 1142 gchar *msg; | 1142 gchar *msg; |
| 1143 | 1143 |
| 1144 if(phb->read_buffer == NULL) { | 1144 if(phb->read_buffer == NULL) { |
| 1366 static void | 1366 static void |
| 1367 http_canwrite(gpointer data, gint source, GaimInputCondition cond) | 1367 http_canwrite(gpointer data, gint source, GaimInputCondition cond) |
| 1368 { | 1368 { |
| 1369 char request[8192]; | 1369 char request[8192]; |
| 1370 int request_len = 0; | 1370 int request_len = 0; |
| 1371 struct PHB *phb = data; | 1371 struct _GaimProxyConnectInfo *phb = data; |
| 1372 socklen_t len; | 1372 socklen_t len; |
| 1373 int error = ETIMEDOUT; | 1373 int error = ETIMEDOUT; |
| 1374 | 1374 |
| 1375 gaim_debug_info("http proxy", "Connected.\n"); | 1375 gaim_debug_info("http proxy", "Connected.\n"); |
| 1376 | 1376 |
| 1429 | 1429 |
| 1430 proxy_do_write(phb, source, cond); | 1430 proxy_do_write(phb, source, cond); |
| 1431 } | 1431 } |
| 1432 | 1432 |
| 1433 static int | 1433 static int |
| 1434 proxy_connect_http(struct PHB *phb, struct sockaddr *addr, socklen_t addrlen) | 1434 proxy_connect_http(struct _GaimProxyConnectInfo *phb, struct sockaddr *addr, socklen_t addrlen) |
| 1435 { | 1435 { |
| 1436 int fd = -1; | 1436 int fd = -1; |
| 1437 | 1437 |
| 1438 gaim_debug_info("http proxy", | 1438 gaim_debug_info("http proxy", |
| 1439 "Connecting to %s:%d via %s:%d using HTTP\n", | 1439 "Connecting to %s:%d via %s:%d using HTTP\n", |
| 1488 | 1488 |
| 1489 | 1489 |
| 1490 static void | 1490 static void |
| 1491 s4_canread(gpointer data, gint source, GaimInputCondition cond) | 1491 s4_canread(gpointer data, gint source, GaimInputCondition cond) |
| 1492 { | 1492 { |
| 1493 struct PHB *phb = data; | 1493 struct _GaimProxyConnectInfo *phb = data; |
| 1494 guchar *buf; | 1494 guchar *buf; |
| 1495 int len, max_read; | 1495 int len, max_read; |
| 1496 | 1496 |
| 1497 /* This is really not going to block under normal circumstances, but to | 1497 /* This is really not going to block under normal circumstances, but to |
| 1498 * be correct, we deal with the unlikely scenario */ | 1498 * be correct, we deal with the unlikely scenario */ |
| 1530 static void | 1530 static void |
| 1531 s4_canwrite(gpointer data, gint source, GaimInputCondition cond) | 1531 s4_canwrite(gpointer data, gint source, GaimInputCondition cond) |
| 1532 { | 1532 { |
| 1533 unsigned char packet[9]; | 1533 unsigned char packet[9]; |
| 1534 struct hostent *hp; | 1534 struct hostent *hp; |
| 1535 struct PHB *phb = data; | 1535 struct _GaimProxyConnectInfo *phb = data; |
| 1536 socklen_t len; | 1536 socklen_t len; |
| 1537 int error = ETIMEDOUT; | 1537 int error = ETIMEDOUT; |
| 1538 | 1538 |
| 1539 gaim_debug_info("socks4 proxy", "Connected.\n"); | 1539 gaim_debug_info("socks4 proxy", "Connected.\n"); |
| 1540 | 1540 |
| 1587 | 1587 |
| 1588 proxy_do_write(phb, source, cond); | 1588 proxy_do_write(phb, source, cond); |
| 1589 } | 1589 } |
| 1590 | 1590 |
| 1591 static int | 1591 static int |
| 1592 proxy_connect_socks4(struct PHB *phb, struct sockaddr *addr, socklen_t addrlen) | 1592 proxy_connect_socks4(struct _GaimProxyConnectInfo *phb, struct sockaddr *addr, socklen_t addrlen) |
| 1593 { | 1593 { |
| 1594 int fd = -1; | 1594 int fd = -1; |
| 1595 | 1595 |
| 1596 gaim_debug_info("socks4 proxy", | 1596 gaim_debug_info("socks4 proxy", |
| 1597 "Connecting to %s:%d via %s:%d using SOCKS4\n", | 1597 "Connecting to %s:%d via %s:%d using SOCKS4\n", |
| 1639 | 1639 |
| 1640 static void | 1640 static void |
| 1641 s5_canread_again(gpointer data, gint source, GaimInputCondition cond) | 1641 s5_canread_again(gpointer data, gint source, GaimInputCondition cond) |
| 1642 { | 1642 { |
| 1643 guchar *dest, *buf; | 1643 guchar *dest, *buf; |
| 1644 struct PHB *phb = data; | 1644 struct _GaimProxyConnectInfo *phb = data; |
| 1645 int len; | 1645 int len; |
| 1646 | 1646 |
| 1647 if (phb->read_buffer == NULL) { | 1647 if (phb->read_buffer == NULL) { |
| 1648 phb->read_buf_len = 512; | 1648 phb->read_buf_len = 512; |
| 1649 phb->read_buffer = g_malloc(phb->read_buf_len); | 1649 phb->read_buffer = g_malloc(phb->read_buf_len); |
| 1721 } | 1721 } |
| 1722 | 1722 |
| 1723 static void | 1723 static void |
| 1724 s5_sendconnect(gpointer data, int source) | 1724 s5_sendconnect(gpointer data, int source) |
| 1725 { | 1725 { |
| 1726 struct PHB *phb = data; | 1726 struct _GaimProxyConnectInfo *phb = data; |
| 1727 int hlen = strlen(phb->host); | 1727 int hlen = strlen(phb->host); |
| 1728 phb->write_buf_len = 5 + hlen + 2; | 1728 phb->write_buf_len = 5 + hlen + 2; |
| 1729 phb->write_buffer = g_malloc(phb->write_buf_len); | 1729 phb->write_buffer = g_malloc(phb->write_buf_len); |
| 1730 phb->written_len = 0; | 1730 phb->written_len = 0; |
| 1731 | 1731 |
| 1745 } | 1745 } |
| 1746 | 1746 |
| 1747 static void | 1747 static void |
| 1748 s5_readauth(gpointer data, gint source, GaimInputCondition cond) | 1748 s5_readauth(gpointer data, gint source, GaimInputCondition cond) |
| 1749 { | 1749 { |
| 1750 struct PHB *phb = data; | 1750 struct _GaimProxyConnectInfo *phb = data; |
| 1751 int len; | 1751 int len; |
| 1752 | 1752 |
| 1753 if (phb->read_buffer == NULL) { | 1753 if (phb->read_buffer == NULL) { |
| 1754 phb->read_buf_len = 2; | 1754 phb->read_buf_len = 2; |
| 1755 phb->read_buffer = g_malloc(phb->read_buf_len); | 1755 phb->read_buffer = g_malloc(phb->read_buf_len); |
| 1838 | 1838 |
| 1839 static void | 1839 static void |
| 1840 s5_readchap(gpointer data, gint source, GaimInputCondition cond) | 1840 s5_readchap(gpointer data, gint source, GaimInputCondition cond) |
| 1841 { | 1841 { |
| 1842 guchar *cmdbuf, *buf; | 1842 guchar *cmdbuf, *buf; |
| 1843 struct PHB *phb = data; | 1843 struct _GaimProxyConnectInfo *phb = data; |
| 1844 int len, navas, currentav; | 1844 int len, navas, currentav; |
| 1845 | 1845 |
| 1846 gaim_debug(GAIM_DEBUG_INFO, "socks5 proxy", "Got CHAP response.\n"); | 1846 gaim_debug(GAIM_DEBUG_INFO, "socks5 proxy", "Got CHAP response.\n"); |
| 1847 | 1847 |
| 1848 if (phb->read_buffer == NULL) { | 1848 if (phb->read_buffer == NULL) { |
| 1971 } | 1971 } |
| 1972 | 1972 |
| 1973 static void | 1973 static void |
| 1974 s5_canread(gpointer data, gint source, GaimInputCondition cond) | 1974 s5_canread(gpointer data, gint source, GaimInputCondition cond) |
| 1975 { | 1975 { |
| 1976 struct PHB *phb = data; | 1976 struct _GaimProxyConnectInfo *phb = data; |
| 1977 int len; | 1977 int len; |
| 1978 | 1978 |
| 1979 if (phb->read_buffer == NULL) { | 1979 if (phb->read_buffer == NULL) { |
| 1980 phb->read_buf_len = 2; | 1980 phb->read_buf_len = 2; |
| 1981 phb->read_buffer = g_malloc(phb->read_buf_len); | 1981 phb->read_buffer = g_malloc(phb->read_buf_len); |
| 2086 static void | 2086 static void |
| 2087 s5_canwrite(gpointer data, gint source, GaimInputCondition cond) | 2087 s5_canwrite(gpointer data, gint source, GaimInputCondition cond) |
| 2088 { | 2088 { |
| 2089 unsigned char buf[5]; | 2089 unsigned char buf[5]; |
| 2090 int i; | 2090 int i; |
| 2091 struct PHB *phb = data; | 2091 struct _GaimProxyConnectInfo *phb = data; |
| 2092 socklen_t len; | 2092 socklen_t len; |
| 2093 int error = ETIMEDOUT; | 2093 int error = ETIMEDOUT; |
| 2094 | 2094 |
| 2095 gaim_debug_info("socks5 proxy", "Connected.\n"); | 2095 gaim_debug_info("socks5 proxy", "Connected.\n"); |
| 2096 | 2096 |
| 2133 phb->inpa = gaim_input_add(source, GAIM_INPUT_WRITE, proxy_do_write, phb); | 2133 phb->inpa = gaim_input_add(source, GAIM_INPUT_WRITE, proxy_do_write, phb); |
| 2134 proxy_do_write(phb, source, GAIM_INPUT_WRITE); | 2134 proxy_do_write(phb, source, GAIM_INPUT_WRITE); |
| 2135 } | 2135 } |
| 2136 | 2136 |
| 2137 static int | 2137 static int |
| 2138 proxy_connect_socks5(struct PHB *phb, struct sockaddr *addr, socklen_t addrlen) | 2138 proxy_connect_socks5(struct _GaimProxyConnectInfo *phb, struct sockaddr *addr, socklen_t addrlen) |
| 2139 { | 2139 { |
| 2140 int fd = -1; | 2140 int fd = -1; |
| 2141 | 2141 |
| 2142 gaim_debug_info("socks5 proxy", | 2142 gaim_debug_info("socks5 proxy", |
| 2143 "Connecting to %s:%d via %s:%d using SOCKS5\n", | 2143 "Connecting to %s:%d via %s:%d using SOCKS5\n", |
| 2183 } | 2183 } |
| 2184 | 2184 |
| 2185 return fd; | 2185 return fd; |
| 2186 } | 2186 } |
| 2187 | 2187 |
| 2188 static void try_connect(struct PHB *phb) | 2188 static void try_connect(struct _GaimProxyConnectInfo *phb) |
| 2189 { | 2189 { |
| 2190 size_t addrlen; | 2190 size_t addrlen; |
| 2191 struct sockaddr *addr; | 2191 struct sockaddr *addr; |
| 2192 int ret = -1; | 2192 int ret = -1; |
| 2193 | 2193 |
| 2235 | 2235 |
| 2236 static void | 2236 static void |
| 2237 connection_host_resolved(GSList *hosts, gpointer data, | 2237 connection_host_resolved(GSList *hosts, gpointer data, |
| 2238 const char *error_message) | 2238 const char *error_message) |
| 2239 { | 2239 { |
| 2240 struct PHB *phb = (struct PHB*)data; | 2240 struct _GaimProxyConnectInfo *phb = (struct _GaimProxyConnectInfo*)data; |
| 2241 | 2241 |
| 2242 phb->hosts = hosts; | 2242 phb->hosts = hosts; |
| 2243 | 2243 |
| 2244 try_connect(phb); | 2244 try_connect(phb); |
| 2245 } | 2245 } |
| 2315 GaimProxyConnectFunction connect_cb, | 2315 GaimProxyConnectFunction connect_cb, |
| 2316 GaimProxyErrorFunction error_cb, gpointer data) | 2316 GaimProxyErrorFunction error_cb, gpointer data) |
| 2317 { | 2317 { |
| 2318 const char *connecthost = host; | 2318 const char *connecthost = host; |
| 2319 int connectport = port; | 2319 int connectport = port; |
| 2320 struct PHB *phb; | 2320 struct _GaimProxyConnectInfo *phb; |
| 2321 | 2321 |
| 2322 g_return_val_if_fail(host != NULL, NULL); | 2322 g_return_val_if_fail(host != NULL, NULL); |
| 2323 g_return_val_if_fail(port > 0, NULL); | 2323 g_return_val_if_fail(port > 0, NULL); |
| 2324 g_return_val_if_fail(connect_cb != NULL, NULL); | 2324 g_return_val_if_fail(connect_cb != NULL, NULL); |
| 2325 /* g_return_val_if_fail(error_cb != NULL, NULL); *//* TODO: Soon! */ | 2325 /* g_return_val_if_fail(error_cb != NULL, NULL); *//* TODO: Soon! */ |
| 2326 | 2326 |
| 2327 phb = g_new0(struct PHB, 1); | 2327 phb = g_new0(struct _GaimProxyConnectInfo, 1); |
| 2328 phb->connect_cb = connect_cb; | 2328 phb->connect_cb = connect_cb; |
| 2329 phb->error_cb = error_cb; | 2329 phb->error_cb = error_cb; |
| 2330 phb->data = data; | 2330 phb->data = data; |
| 2331 phb->host = g_strdup(host); | 2331 phb->host = g_strdup(host); |
| 2332 phb->port = port; | 2332 phb->port = port; |
| 2377 GaimProxyConnectInfo * | 2377 GaimProxyConnectInfo * |
| 2378 gaim_proxy_connect_socks5(GaimProxyInfo *gpi, const char *host, int port, | 2378 gaim_proxy_connect_socks5(GaimProxyInfo *gpi, const char *host, int port, |
| 2379 GaimProxyConnectFunction connect_cb, | 2379 GaimProxyConnectFunction connect_cb, |
| 2380 GaimProxyErrorFunction error_cb, gpointer data) | 2380 GaimProxyErrorFunction error_cb, gpointer data) |
| 2381 { | 2381 { |
| 2382 struct PHB *phb; | 2382 struct _GaimProxyConnectInfo *phb; |
| 2383 | 2383 |
| 2384 g_return_val_if_fail(host != NULL, NULL); | 2384 g_return_val_if_fail(host != NULL, NULL); |
| 2385 g_return_val_if_fail(port > 0, NULL); | 2385 g_return_val_if_fail(port > 0, NULL); |
| 2386 g_return_val_if_fail(connect_cb != NULL, NULL); | 2386 g_return_val_if_fail(connect_cb != NULL, NULL); |
| 2387 /* g_return_val_if_fail(error_cb != NULL, NULL); *//* TODO: Soon! */ | 2387 /* g_return_val_if_fail(error_cb != NULL, NULL); *//* TODO: Soon! */ |
| 2388 | 2388 |
| 2389 phb = g_new0(struct PHB, 1); | 2389 phb = g_new0(struct _GaimProxyConnectInfo, 1); |
| 2390 phb->connect_cb = connect_cb; | 2390 phb->connect_cb = connect_cb; |
| 2391 phb->error_cb = error_cb; | 2391 phb->error_cb = error_cb; |
| 2392 phb->data = data; | 2392 phb->data = data; |
| 2393 phb->host = g_strdup(host); | 2393 phb->host = g_strdup(host); |
| 2394 phb->port = port; | 2394 phb->port = port; |
