1 | /* |
---|
2 | * gaim |
---|
3 | * |
---|
4 | * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> |
---|
5 | * Copyright (C) 2002-2004, Wilmer van der Gaast, Jelmer Vernooij |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or modify |
---|
8 | * it under the terms of the GNU General Public License as published by |
---|
9 | * the Free Software Foundation; either version 2 of the License, or |
---|
10 | * (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | /* this is a little piece of code to handle proxy connection */ |
---|
24 | /* it is intended to : 1st handle http proxy, using the CONNECT command |
---|
25 | , 2nd provide an easy way to add socks support */ |
---|
26 | |
---|
27 | #define BITLBEE_CORE |
---|
28 | #include <stdio.h> |
---|
29 | #include <stdlib.h> |
---|
30 | #include <string.h> |
---|
31 | #include <sys/types.h> |
---|
32 | #ifndef _WIN32 |
---|
33 | #include <sys/socket.h> |
---|
34 | #include <netdb.h> |
---|
35 | #include <netinet/in.h> |
---|
36 | #include <arpa/inet.h> |
---|
37 | #include <unistd.h> |
---|
38 | #else |
---|
39 | #include "sock.h" |
---|
40 | #define ETIMEDOUT WSAETIMEDOUT |
---|
41 | #define EINPROGRESS WSAEINPROGRESS |
---|
42 | #endif |
---|
43 | #include <fcntl.h> |
---|
44 | #include <errno.h> |
---|
45 | #include "nogaim.h" |
---|
46 | #include "proxy.h" |
---|
47 | |
---|
48 | #define GAIM_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR) |
---|
49 | #define GAIM_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL) |
---|
50 | #define GAIM_ERR_COND (G_IO_HUP | G_IO_ERR | G_IO_NVAL) |
---|
51 | |
---|
52 | char proxyhost[128] = ""; |
---|
53 | int proxyport = 0; |
---|
54 | int proxytype = PROXY_NONE; |
---|
55 | char proxyuser[128] = ""; |
---|
56 | char proxypass[128] = ""; |
---|
57 | |
---|
58 | struct PHB { |
---|
59 | GaimInputFunction func, proxy_func; |
---|
60 | gpointer data, proxy_data; |
---|
61 | char *host; |
---|
62 | int port; |
---|
63 | int fd; |
---|
64 | gint inpa; |
---|
65 | }; |
---|
66 | |
---|
67 | typedef struct _GaimIOClosure { |
---|
68 | GaimInputFunction function; |
---|
69 | guint result; |
---|
70 | gpointer data; |
---|
71 | } GaimIOClosure; |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | static struct sockaddr_in *gaim_gethostbyname(const char *host, int port) |
---|
76 | { |
---|
77 | static struct sockaddr_in sin; |
---|
78 | |
---|
79 | if (!inet_aton(host, &sin.sin_addr)) { |
---|
80 | struct hostent *hp; |
---|
81 | if (!(hp = gethostbyname(host))) { |
---|
82 | return NULL; |
---|
83 | } |
---|
84 | memset(&sin, 0, sizeof(struct sockaddr_in)); |
---|
85 | memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); |
---|
86 | sin.sin_family = hp->h_addrtype; |
---|
87 | } else |
---|
88 | sin.sin_family = AF_INET; |
---|
89 | sin.sin_port = htons(port); |
---|
90 | |
---|
91 | return &sin; |
---|
92 | } |
---|
93 | |
---|
94 | static void gaim_io_destroy(gpointer data) |
---|
95 | { |
---|
96 | g_free(data); |
---|
97 | } |
---|
98 | |
---|
99 | static gboolean gaim_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data) |
---|
100 | { |
---|
101 | GaimIOClosure *closure = data; |
---|
102 | GaimInputCondition gaim_cond = 0; |
---|
103 | |
---|
104 | if (condition & GAIM_READ_COND) |
---|
105 | gaim_cond |= GAIM_INPUT_READ; |
---|
106 | if (condition & GAIM_WRITE_COND) |
---|
107 | gaim_cond |= GAIM_INPUT_WRITE; |
---|
108 | |
---|
109 | closure->function(closure->data, g_io_channel_unix_get_fd(source), gaim_cond); |
---|
110 | |
---|
111 | return TRUE; |
---|
112 | } |
---|
113 | |
---|
114 | static void gaim_io_connected(gpointer data, gint source, GaimInputCondition cond) |
---|
115 | { |
---|
116 | struct PHB *phb = data; |
---|
117 | unsigned int len; |
---|
118 | int error = ETIMEDOUT; |
---|
119 | len = sizeof(error); |
---|
120 | |
---|
121 | #ifndef _WIN32 |
---|
122 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
123 | closesocket(source); |
---|
124 | gaim_input_remove(phb->inpa); |
---|
125 | if( phb->proxy_func ) |
---|
126 | phb->proxy_func(phb->proxy_data, -1, GAIM_INPUT_READ); |
---|
127 | else { |
---|
128 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
129 | g_free(phb); |
---|
130 | } |
---|
131 | return; |
---|
132 | } |
---|
133 | #endif |
---|
134 | sock_make_blocking(source); |
---|
135 | gaim_input_remove(phb->inpa); |
---|
136 | if( phb->proxy_func ) |
---|
137 | phb->proxy_func(phb->proxy_data, source, GAIM_INPUT_READ); |
---|
138 | else { |
---|
139 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
140 | g_free(phb); |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | static int proxy_connect_none(const char *host, unsigned short port, struct PHB *phb) |
---|
145 | { |
---|
146 | struct sockaddr_in *sin; |
---|
147 | int fd = -1; |
---|
148 | |
---|
149 | if (!(sin = gaim_gethostbyname(host, port))) { |
---|
150 | g_free(phb); |
---|
151 | return -1; |
---|
152 | } |
---|
153 | |
---|
154 | if ((fd = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) { |
---|
155 | g_free(phb); |
---|
156 | return -1; |
---|
157 | } |
---|
158 | |
---|
159 | sock_make_nonblocking(fd); |
---|
160 | |
---|
161 | if (connect(fd, (struct sockaddr *)sin, sizeof(*sin)) < 0) { |
---|
162 | if (sockerr_again()) { |
---|
163 | phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, gaim_io_connected, phb); |
---|
164 | phb->fd = fd; |
---|
165 | } else { |
---|
166 | closesocket(fd); |
---|
167 | g_free(phb); |
---|
168 | return -1; |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | return fd; |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | /* Connecting to HTTP proxies */ |
---|
177 | |
---|
178 | #define HTTP_GOODSTRING "HTTP/1.0 200 Connection established" |
---|
179 | #define HTTP_GOODSTRING2 "HTTP/1.1 200 Connection established" |
---|
180 | |
---|
181 | static void http_canread(gpointer data, gint source, GaimInputCondition cond) |
---|
182 | { |
---|
183 | int nlc = 0; |
---|
184 | int pos = 0; |
---|
185 | struct PHB *phb = data; |
---|
186 | char inputline[8192]; |
---|
187 | |
---|
188 | gaim_input_remove(phb->inpa); |
---|
189 | |
---|
190 | while ((pos < sizeof(inputline)-1) && (nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) { |
---|
191 | if (inputline[pos - 1] == '\n') |
---|
192 | nlc++; |
---|
193 | else if (inputline[pos - 1] != '\r') |
---|
194 | nlc = 0; |
---|
195 | } |
---|
196 | inputline[pos] = '\0'; |
---|
197 | |
---|
198 | if ((memcmp(HTTP_GOODSTRING, inputline, strlen(HTTP_GOODSTRING)) == 0) || |
---|
199 | (memcmp(HTTP_GOODSTRING2, inputline, strlen(HTTP_GOODSTRING2)) == 0)) { |
---|
200 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
201 | g_free(phb->host); |
---|
202 | g_free(phb); |
---|
203 | return; |
---|
204 | } |
---|
205 | |
---|
206 | close(source); |
---|
207 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
208 | g_free(phb->host); |
---|
209 | g_free(phb); |
---|
210 | return; |
---|
211 | } |
---|
212 | |
---|
213 | static void http_canwrite(gpointer data, gint source, GaimInputCondition cond) |
---|
214 | { |
---|
215 | char cmd[384]; |
---|
216 | struct PHB *phb = data; |
---|
217 | unsigned int len; |
---|
218 | int error = ETIMEDOUT; |
---|
219 | if (phb->inpa > 0) |
---|
220 | gaim_input_remove(phb->inpa); |
---|
221 | len = sizeof(error); |
---|
222 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
223 | close(source); |
---|
224 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
225 | g_free(phb->host); |
---|
226 | g_free(phb); |
---|
227 | return; |
---|
228 | } |
---|
229 | sock_make_blocking(source); |
---|
230 | |
---|
231 | g_snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n", phb->host, phb->port, |
---|
232 | phb->host, phb->port); |
---|
233 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
234 | close(source); |
---|
235 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
236 | g_free(phb->host); |
---|
237 | g_free(phb); |
---|
238 | return; |
---|
239 | } |
---|
240 | |
---|
241 | if (proxyuser && *proxyuser) { |
---|
242 | char *t1, *t2; |
---|
243 | t1 = g_strdup_printf("%s:%s", proxyuser, proxypass); |
---|
244 | t2 = tobase64(t1); |
---|
245 | g_free(t1); |
---|
246 | g_snprintf(cmd, sizeof(cmd), "Proxy-Authorization: Basic %s\r\n", t2); |
---|
247 | g_free(t2); |
---|
248 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
249 | close(source); |
---|
250 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
251 | g_free(phb->host); |
---|
252 | g_free(phb); |
---|
253 | return; |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | g_snprintf(cmd, sizeof(cmd), "\r\n"); |
---|
258 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
259 | close(source); |
---|
260 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
261 | g_free(phb->host); |
---|
262 | g_free(phb); |
---|
263 | return; |
---|
264 | } |
---|
265 | |
---|
266 | phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, http_canread, phb); |
---|
267 | } |
---|
268 | |
---|
269 | static int proxy_connect_http(const char *host, unsigned short port, struct PHB *phb) |
---|
270 | { |
---|
271 | phb->host = g_strdup(host); |
---|
272 | phb->port = port; |
---|
273 | phb->proxy_func = http_canwrite; |
---|
274 | phb->proxy_data = phb; |
---|
275 | |
---|
276 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
277 | } |
---|
278 | |
---|
279 | |
---|
280 | /* Connecting to SOCKS4 proxies */ |
---|
281 | |
---|
282 | static void s4_canread(gpointer data, gint source, GaimInputCondition cond) |
---|
283 | { |
---|
284 | unsigned char packet[12]; |
---|
285 | struct PHB *phb = data; |
---|
286 | |
---|
287 | gaim_input_remove(phb->inpa); |
---|
288 | |
---|
289 | memset(packet, 0, sizeof(packet)); |
---|
290 | if (read(source, packet, 9) >= 4 && packet[1] == 90) { |
---|
291 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
292 | g_free(phb->host); |
---|
293 | g_free(phb); |
---|
294 | return; |
---|
295 | } |
---|
296 | |
---|
297 | close(source); |
---|
298 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
299 | g_free(phb->host); |
---|
300 | g_free(phb); |
---|
301 | } |
---|
302 | |
---|
303 | static void s4_canwrite(gpointer data, gint source, GaimInputCondition cond) |
---|
304 | { |
---|
305 | unsigned char packet[12]; |
---|
306 | struct hostent *hp; |
---|
307 | struct PHB *phb = data; |
---|
308 | unsigned int len; |
---|
309 | int error = ETIMEDOUT; |
---|
310 | if (phb->inpa > 0) |
---|
311 | gaim_input_remove(phb->inpa); |
---|
312 | len = sizeof(error); |
---|
313 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
314 | close(source); |
---|
315 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
316 | g_free(phb->host); |
---|
317 | g_free(phb); |
---|
318 | return; |
---|
319 | } |
---|
320 | sock_make_blocking(source); |
---|
321 | |
---|
322 | /* XXX does socks4 not support host name lookups by the proxy? */ |
---|
323 | if (!(hp = gethostbyname(phb->host))) { |
---|
324 | close(source); |
---|
325 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
326 | g_free(phb->host); |
---|
327 | g_free(phb); |
---|
328 | return; |
---|
329 | } |
---|
330 | |
---|
331 | packet[0] = 4; |
---|
332 | packet[1] = 1; |
---|
333 | packet[2] = phb->port >> 8; |
---|
334 | packet[3] = phb->port & 0xff; |
---|
335 | packet[4] = (unsigned char)(hp->h_addr_list[0])[0]; |
---|
336 | packet[5] = (unsigned char)(hp->h_addr_list[0])[1]; |
---|
337 | packet[6] = (unsigned char)(hp->h_addr_list[0])[2]; |
---|
338 | packet[7] = (unsigned char)(hp->h_addr_list[0])[3]; |
---|
339 | packet[8] = 0; |
---|
340 | if (write(source, packet, 9) != 9) { |
---|
341 | close(source); |
---|
342 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
343 | g_free(phb->host); |
---|
344 | g_free(phb); |
---|
345 | return; |
---|
346 | } |
---|
347 | |
---|
348 | phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s4_canread, phb); |
---|
349 | } |
---|
350 | |
---|
351 | static int proxy_connect_socks4(const char *host, unsigned short port, struct PHB *phb) |
---|
352 | { |
---|
353 | phb->host = g_strdup(host); |
---|
354 | phb->port = port; |
---|
355 | phb->proxy_func = s4_canwrite; |
---|
356 | phb->proxy_data = phb; |
---|
357 | |
---|
358 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
359 | } |
---|
360 | |
---|
361 | |
---|
362 | /* Connecting to SOCKS5 proxies */ |
---|
363 | |
---|
364 | static void s5_canread_again(gpointer data, gint source, GaimInputCondition cond) |
---|
365 | { |
---|
366 | unsigned char buf[512]; |
---|
367 | struct PHB *phb = data; |
---|
368 | |
---|
369 | gaim_input_remove(phb->inpa); |
---|
370 | |
---|
371 | if (read(source, buf, 10) < 10) { |
---|
372 | close(source); |
---|
373 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
374 | g_free(phb->host); |
---|
375 | g_free(phb); |
---|
376 | return; |
---|
377 | } |
---|
378 | if ((buf[0] != 0x05) || (buf[1] != 0x00)) { |
---|
379 | close(source); |
---|
380 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
381 | g_free(phb->host); |
---|
382 | g_free(phb); |
---|
383 | return; |
---|
384 | } |
---|
385 | |
---|
386 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
387 | g_free(phb->host); |
---|
388 | g_free(phb); |
---|
389 | return; |
---|
390 | } |
---|
391 | |
---|
392 | static void s5_sendconnect(gpointer data, gint source) |
---|
393 | { |
---|
394 | unsigned char buf[512]; |
---|
395 | struct PHB *phb = data; |
---|
396 | int hlen = strlen(phb->host); |
---|
397 | |
---|
398 | buf[0] = 0x05; |
---|
399 | buf[1] = 0x01; /* CONNECT */ |
---|
400 | buf[2] = 0x00; /* reserved */ |
---|
401 | buf[3] = 0x03; /* address type -- host name */ |
---|
402 | buf[4] = hlen; |
---|
403 | memcpy(buf + 5, phb->host, hlen); |
---|
404 | buf[5 + strlen(phb->host)] = phb->port >> 8; |
---|
405 | buf[5 + strlen(phb->host) + 1] = phb->port & 0xff; |
---|
406 | |
---|
407 | if (write(source, buf, (5 + strlen(phb->host) + 2)) < (5 + strlen(phb->host) + 2)) { |
---|
408 | close(source); |
---|
409 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
410 | g_free(phb->host); |
---|
411 | g_free(phb); |
---|
412 | return; |
---|
413 | } |
---|
414 | |
---|
415 | phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread_again, phb); |
---|
416 | } |
---|
417 | |
---|
418 | static void s5_readauth(gpointer data, gint source, GaimInputCondition cond) |
---|
419 | { |
---|
420 | unsigned char buf[512]; |
---|
421 | struct PHB *phb = data; |
---|
422 | |
---|
423 | gaim_input_remove(phb->inpa); |
---|
424 | |
---|
425 | if (read(source, buf, 2) < 2) { |
---|
426 | close(source); |
---|
427 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
428 | g_free(phb->host); |
---|
429 | g_free(phb); |
---|
430 | return; |
---|
431 | } |
---|
432 | |
---|
433 | if ((buf[0] != 0x01) || (buf[1] != 0x00)) { |
---|
434 | close(source); |
---|
435 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
436 | g_free(phb->host); |
---|
437 | g_free(phb); |
---|
438 | return; |
---|
439 | } |
---|
440 | |
---|
441 | s5_sendconnect(phb, source); |
---|
442 | } |
---|
443 | |
---|
444 | static void s5_canread(gpointer data, gint source, GaimInputCondition cond) |
---|
445 | { |
---|
446 | unsigned char buf[512]; |
---|
447 | struct PHB *phb = data; |
---|
448 | |
---|
449 | gaim_input_remove(phb->inpa); |
---|
450 | |
---|
451 | if (read(source, buf, 2) < 2) { |
---|
452 | close(source); |
---|
453 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
454 | g_free(phb->host); |
---|
455 | g_free(phb); |
---|
456 | return; |
---|
457 | } |
---|
458 | |
---|
459 | if ((buf[0] != 0x05) || (buf[1] == 0xff)) { |
---|
460 | close(source); |
---|
461 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
462 | g_free(phb->host); |
---|
463 | g_free(phb); |
---|
464 | return; |
---|
465 | } |
---|
466 | |
---|
467 | if (buf[1] == 0x02) { |
---|
468 | unsigned int i = strlen(proxyuser), j = strlen(proxypass); |
---|
469 | buf[0] = 0x01; /* version 1 */ |
---|
470 | buf[1] = i; |
---|
471 | memcpy(buf + 2, proxyuser, i); |
---|
472 | buf[2 + i] = j; |
---|
473 | memcpy(buf + 2 + i + 1, proxypass, j); |
---|
474 | if (write(source, buf, 3 + i + j) < 3 + i + j) { |
---|
475 | close(source); |
---|
476 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
477 | g_free(phb->host); |
---|
478 | g_free(phb); |
---|
479 | return; |
---|
480 | } |
---|
481 | |
---|
482 | phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_readauth, phb); |
---|
483 | } else { |
---|
484 | s5_sendconnect(phb, source); |
---|
485 | } |
---|
486 | } |
---|
487 | |
---|
488 | static void s5_canwrite(gpointer data, gint source, GaimInputCondition cond) |
---|
489 | { |
---|
490 | unsigned char buf[512]; |
---|
491 | int i; |
---|
492 | struct PHB *phb = data; |
---|
493 | unsigned int len; |
---|
494 | int error = ETIMEDOUT; |
---|
495 | if (phb->inpa > 0) |
---|
496 | gaim_input_remove(phb->inpa); |
---|
497 | len = sizeof(error); |
---|
498 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
499 | close(source); |
---|
500 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
501 | g_free(phb->host); |
---|
502 | g_free(phb); |
---|
503 | return; |
---|
504 | } |
---|
505 | sock_make_blocking(source); |
---|
506 | |
---|
507 | i = 0; |
---|
508 | buf[0] = 0x05; /* SOCKS version 5 */ |
---|
509 | if (proxyuser[0]) { |
---|
510 | buf[1] = 0x02; /* two methods */ |
---|
511 | buf[2] = 0x00; /* no authentication */ |
---|
512 | buf[3] = 0x02; /* username/password authentication */ |
---|
513 | i = 4; |
---|
514 | } else { |
---|
515 | buf[1] = 0x01; |
---|
516 | buf[2] = 0x00; |
---|
517 | i = 3; |
---|
518 | } |
---|
519 | |
---|
520 | if (write(source, buf, i) < i) { |
---|
521 | close(source); |
---|
522 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
523 | g_free(phb->host); |
---|
524 | g_free(phb); |
---|
525 | return; |
---|
526 | } |
---|
527 | |
---|
528 | phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread, phb); |
---|
529 | } |
---|
530 | |
---|
531 | static int proxy_connect_socks5(const char *host, unsigned short port, struct PHB *phb) |
---|
532 | { |
---|
533 | phb->host = g_strdup(host); |
---|
534 | phb->port = port; |
---|
535 | phb->proxy_func = s5_canwrite; |
---|
536 | phb->proxy_data = phb; |
---|
537 | |
---|
538 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
539 | } |
---|
540 | |
---|
541 | |
---|
542 | /* Export functions */ |
---|
543 | |
---|
544 | gint gaim_input_add(gint source, GaimInputCondition condition, GaimInputFunction function, gpointer data) |
---|
545 | { |
---|
546 | GaimIOClosure *closure = g_new0(GaimIOClosure, 1); |
---|
547 | GIOChannel *channel; |
---|
548 | GIOCondition cond = 0; |
---|
549 | |
---|
550 | closure->function = function; |
---|
551 | closure->data = data; |
---|
552 | |
---|
553 | if (condition & GAIM_INPUT_READ) |
---|
554 | cond |= GAIM_READ_COND; |
---|
555 | if (condition & GAIM_INPUT_WRITE) |
---|
556 | cond |= GAIM_WRITE_COND; |
---|
557 | |
---|
558 | channel = g_io_channel_unix_new(source); |
---|
559 | closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, |
---|
560 | gaim_io_invoke, closure, gaim_io_destroy); |
---|
561 | |
---|
562 | g_io_channel_unref(channel); |
---|
563 | return closure->result; |
---|
564 | } |
---|
565 | |
---|
566 | void gaim_input_remove(gint tag) |
---|
567 | { |
---|
568 | if (tag > 0) |
---|
569 | g_source_remove(tag); |
---|
570 | } |
---|
571 | |
---|
572 | int proxy_connect(const char *host, int port, GaimInputFunction func, gpointer data) |
---|
573 | { |
---|
574 | struct PHB *phb; |
---|
575 | |
---|
576 | if (!host || !port || (port == -1) || !func || strlen(host) > 128) { |
---|
577 | return -1; |
---|
578 | } |
---|
579 | |
---|
580 | phb = g_new0(struct PHB, 1); |
---|
581 | phb->func = func; |
---|
582 | phb->data = data; |
---|
583 | |
---|
584 | #ifndef _WIN32 |
---|
585 | sethostent(1); |
---|
586 | #endif |
---|
587 | |
---|
588 | if ((proxytype == PROXY_NONE) || !proxyhost || !proxyhost[0] || !proxyport || (proxyport == -1)) |
---|
589 | return proxy_connect_none(host, port, phb); |
---|
590 | else if (proxytype == PROXY_HTTP) |
---|
591 | return proxy_connect_http(host, port, phb); |
---|
592 | else if (proxytype == PROXY_SOCKS4) |
---|
593 | return proxy_connect_socks4(host, port, phb); |
---|
594 | else if (proxytype == PROXY_SOCKS5) |
---|
595 | return proxy_connect_socks5(host, port, phb); |
---|
596 | |
---|
597 | if (phb->host) g_free(phb); |
---|
598 | g_free(phb); |
---|
599 | return -1; |
---|
600 | } |
---|