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(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 | // if (condition & GAIM_ERR_COND) |
---|
109 | // fprintf( stderr, "ERROR! fd=%d\n", g_io_channel_unix_get_fd( source ) ); |
---|
110 | |
---|
111 | closure->function(closure->data, g_io_channel_unix_get_fd(source), gaim_cond); |
---|
112 | |
---|
113 | return TRUE; |
---|
114 | } |
---|
115 | |
---|
116 | static void no_one_calls(gpointer data, gint source, GaimInputCondition cond) |
---|
117 | { |
---|
118 | struct PHB *phb = data; |
---|
119 | unsigned int len; |
---|
120 | int error = ETIMEDOUT; |
---|
121 | len = sizeof(error); |
---|
122 | |
---|
123 | #ifndef _WIN32 |
---|
124 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
125 | closesocket(source); |
---|
126 | gaim_input_remove(phb->inpa); |
---|
127 | if( phb->proxy_func ) |
---|
128 | phb->proxy_func(phb->proxy_data, -1, GAIM_INPUT_READ); |
---|
129 | else { |
---|
130 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
131 | g_free(phb); |
---|
132 | } |
---|
133 | return; |
---|
134 | } |
---|
135 | fcntl(source, F_SETFL, 0); |
---|
136 | #endif |
---|
137 | gaim_input_remove(phb->inpa); |
---|
138 | if( phb->proxy_func ) |
---|
139 | phb->proxy_func(phb->proxy_data, source, GAIM_INPUT_READ); |
---|
140 | else { |
---|
141 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
142 | g_free(phb); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | static int proxy_connect_none(char *host, unsigned short port, struct PHB *phb) |
---|
147 | { |
---|
148 | struct sockaddr_in *sin; |
---|
149 | int fd = -1; |
---|
150 | |
---|
151 | if (!(sin = gaim_gethostbyname(host, port))) { |
---|
152 | g_free(phb); |
---|
153 | return -1; |
---|
154 | } |
---|
155 | |
---|
156 | if ((fd = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) { |
---|
157 | g_free(phb); |
---|
158 | return -1; |
---|
159 | } |
---|
160 | |
---|
161 | sock_make_nonblocking(fd); |
---|
162 | |
---|
163 | if (connect(fd, (struct sockaddr *)sin, sizeof(*sin)) < 0) { |
---|
164 | if (sockerr_again()) { |
---|
165 | phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, no_one_calls, phb); |
---|
166 | phb->fd = fd; |
---|
167 | } else { |
---|
168 | closesocket(fd); |
---|
169 | g_free(phb); |
---|
170 | return -1; |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | return fd; |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | /* Connecting to HTTP proxies */ |
---|
179 | |
---|
180 | #define HTTP_GOODSTRING "HTTP/1.0 200 Connection established" |
---|
181 | #define HTTP_GOODSTRING2 "HTTP/1.1 200 Connection established" |
---|
182 | |
---|
183 | static void http_canread(gpointer data, gint source, GaimInputCondition cond) |
---|
184 | { |
---|
185 | int nlc = 0; |
---|
186 | int pos = 0; |
---|
187 | struct PHB *phb = data; |
---|
188 | char inputline[8192]; |
---|
189 | |
---|
190 | gaim_input_remove(phb->inpa); |
---|
191 | |
---|
192 | while ((pos < sizeof(inputline)-1) && (nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) { |
---|
193 | if (inputline[pos - 1] == '\n') |
---|
194 | nlc++; |
---|
195 | else if (inputline[pos - 1] != '\r') |
---|
196 | nlc = 0; |
---|
197 | } |
---|
198 | inputline[pos] = '\0'; |
---|
199 | |
---|
200 | if ((memcmp(HTTP_GOODSTRING, inputline, strlen(HTTP_GOODSTRING)) == 0) || |
---|
201 | (memcmp(HTTP_GOODSTRING2, inputline, strlen(HTTP_GOODSTRING2)) == 0)) { |
---|
202 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
203 | g_free(phb->host); |
---|
204 | g_free(phb); |
---|
205 | return; |
---|
206 | } |
---|
207 | |
---|
208 | close(source); |
---|
209 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
210 | g_free(phb->host); |
---|
211 | g_free(phb); |
---|
212 | return; |
---|
213 | } |
---|
214 | |
---|
215 | static void http_canwrite(gpointer data, gint source, GaimInputCondition cond) |
---|
216 | { |
---|
217 | char cmd[384]; |
---|
218 | struct PHB *phb = data; |
---|
219 | unsigned int len; |
---|
220 | int error = ETIMEDOUT; |
---|
221 | if (phb->inpa > 0) |
---|
222 | gaim_input_remove(phb->inpa); |
---|
223 | len = sizeof(error); |
---|
224 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
225 | close(source); |
---|
226 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
227 | g_free(phb->host); |
---|
228 | g_free(phb); |
---|
229 | return; |
---|
230 | } |
---|
231 | #ifdef F_SETFL |
---|
232 | fcntl(source, F_SETFL, 0); |
---|
233 | #endif |
---|
234 | |
---|
235 | g_snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n", phb->host, phb->port, |
---|
236 | phb->host, phb->port); |
---|
237 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
238 | close(source); |
---|
239 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
240 | g_free(phb->host); |
---|
241 | g_free(phb); |
---|
242 | return; |
---|
243 | } |
---|
244 | |
---|
245 | if (proxyuser && *proxyuser) { |
---|
246 | char *t1, *t2; |
---|
247 | t1 = g_strdup_printf("%s:%s", proxyuser, proxypass); |
---|
248 | t2 = tobase64(t1); |
---|
249 | g_free(t1); |
---|
250 | g_snprintf(cmd, sizeof(cmd), "Proxy-Authorization: Basic %s\r\n", t2); |
---|
251 | g_free(t2); |
---|
252 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
253 | close(source); |
---|
254 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
255 | g_free(phb->host); |
---|
256 | g_free(phb); |
---|
257 | return; |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | g_snprintf(cmd, sizeof(cmd), "\r\n"); |
---|
262 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
263 | close(source); |
---|
264 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
265 | g_free(phb->host); |
---|
266 | g_free(phb); |
---|
267 | return; |
---|
268 | } |
---|
269 | |
---|
270 | phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, http_canread, phb); |
---|
271 | } |
---|
272 | |
---|
273 | static int proxy_connect_http(char *host, unsigned short port, struct PHB *phb) |
---|
274 | { |
---|
275 | phb->host = g_strdup(host); |
---|
276 | phb->port = port; |
---|
277 | phb->proxy_func = http_canwrite; |
---|
278 | phb->proxy_data = phb; |
---|
279 | |
---|
280 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
281 | } |
---|
282 | |
---|
283 | |
---|
284 | /* Connecting to SOCKS4 proxies */ |
---|
285 | |
---|
286 | static void s4_canread(gpointer data, gint source, GaimInputCondition cond) |
---|
287 | { |
---|
288 | unsigned char packet[12]; |
---|
289 | struct PHB *phb = data; |
---|
290 | |
---|
291 | gaim_input_remove(phb->inpa); |
---|
292 | |
---|
293 | memset(packet, 0, sizeof(packet)); |
---|
294 | if (read(source, packet, 9) >= 4 && packet[1] == 90) { |
---|
295 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
296 | g_free(phb->host); |
---|
297 | g_free(phb); |
---|
298 | return; |
---|
299 | } |
---|
300 | |
---|
301 | close(source); |
---|
302 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
303 | g_free(phb->host); |
---|
304 | g_free(phb); |
---|
305 | } |
---|
306 | |
---|
307 | static void s4_canwrite(gpointer data, gint source, GaimInputCondition cond) |
---|
308 | { |
---|
309 | unsigned char packet[12]; |
---|
310 | struct hostent *hp; |
---|
311 | struct PHB *phb = data; |
---|
312 | unsigned int len; |
---|
313 | int error = ETIMEDOUT; |
---|
314 | if (phb->inpa > 0) |
---|
315 | gaim_input_remove(phb->inpa); |
---|
316 | len = sizeof(error); |
---|
317 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
318 | close(source); |
---|
319 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
320 | g_free(phb->host); |
---|
321 | g_free(phb); |
---|
322 | return; |
---|
323 | } |
---|
324 | #ifdef F_SETFL |
---|
325 | fcntl(source, F_SETFL, 0); |
---|
326 | #endif |
---|
327 | |
---|
328 | /* XXX does socks4 not support host name lookups by the proxy? */ |
---|
329 | if (!(hp = gethostbyname(phb->host))) { |
---|
330 | close(source); |
---|
331 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
332 | g_free(phb->host); |
---|
333 | g_free(phb); |
---|
334 | return; |
---|
335 | } |
---|
336 | |
---|
337 | packet[0] = 4; |
---|
338 | packet[1] = 1; |
---|
339 | packet[2] = phb->port >> 8; |
---|
340 | packet[3] = phb->port & 0xff; |
---|
341 | packet[4] = (unsigned char)(hp->h_addr_list[0])[0]; |
---|
342 | packet[5] = (unsigned char)(hp->h_addr_list[0])[1]; |
---|
343 | packet[6] = (unsigned char)(hp->h_addr_list[0])[2]; |
---|
344 | packet[7] = (unsigned char)(hp->h_addr_list[0])[3]; |
---|
345 | packet[8] = 0; |
---|
346 | if (write(source, packet, 9) != 9) { |
---|
347 | close(source); |
---|
348 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
349 | g_free(phb->host); |
---|
350 | g_free(phb); |
---|
351 | return; |
---|
352 | } |
---|
353 | |
---|
354 | phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s4_canread, phb); |
---|
355 | } |
---|
356 | |
---|
357 | static int proxy_connect_socks4(char *host, unsigned short port, struct PHB *phb) |
---|
358 | { |
---|
359 | phb->host = g_strdup(host); |
---|
360 | phb->port = port; |
---|
361 | phb->proxy_func = s4_canwrite; |
---|
362 | phb->proxy_data = phb; |
---|
363 | |
---|
364 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
365 | } |
---|
366 | |
---|
367 | |
---|
368 | /* Connecting to SOCKS5 proxies */ |
---|
369 | |
---|
370 | static void s5_canread_again(gpointer data, gint source, GaimInputCondition cond) |
---|
371 | { |
---|
372 | unsigned char buf[512]; |
---|
373 | struct PHB *phb = data; |
---|
374 | |
---|
375 | gaim_input_remove(phb->inpa); |
---|
376 | |
---|
377 | if (read(source, buf, 10) < 10) { |
---|
378 | close(source); |
---|
379 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
380 | g_free(phb->host); |
---|
381 | g_free(phb); |
---|
382 | return; |
---|
383 | } |
---|
384 | if ((buf[0] != 0x05) || (buf[1] != 0x00)) { |
---|
385 | close(source); |
---|
386 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
387 | g_free(phb->host); |
---|
388 | g_free(phb); |
---|
389 | return; |
---|
390 | } |
---|
391 | |
---|
392 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
393 | g_free(phb->host); |
---|
394 | g_free(phb); |
---|
395 | return; |
---|
396 | } |
---|
397 | |
---|
398 | static void s5_sendconnect(gpointer data, gint source) |
---|
399 | { |
---|
400 | unsigned char buf[512]; |
---|
401 | struct PHB *phb = data; |
---|
402 | int hlen = strlen(phb->host); |
---|
403 | |
---|
404 | buf[0] = 0x05; |
---|
405 | buf[1] = 0x01; /* CONNECT */ |
---|
406 | buf[2] = 0x00; /* reserved */ |
---|
407 | buf[3] = 0x03; /* address type -- host name */ |
---|
408 | buf[4] = hlen; |
---|
409 | memcpy(buf + 5, phb->host, hlen); |
---|
410 | buf[5 + strlen(phb->host)] = phb->port >> 8; |
---|
411 | buf[5 + strlen(phb->host) + 1] = phb->port & 0xff; |
---|
412 | |
---|
413 | if (write(source, buf, (5 + strlen(phb->host) + 2)) < (5 + strlen(phb->host) + 2)) { |
---|
414 | close(source); |
---|
415 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
416 | g_free(phb->host); |
---|
417 | g_free(phb); |
---|
418 | return; |
---|
419 | } |
---|
420 | |
---|
421 | phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread_again, phb); |
---|
422 | } |
---|
423 | |
---|
424 | static void s5_readauth(gpointer data, gint source, GaimInputCondition cond) |
---|
425 | { |
---|
426 | unsigned char buf[512]; |
---|
427 | struct PHB *phb = data; |
---|
428 | |
---|
429 | gaim_input_remove(phb->inpa); |
---|
430 | |
---|
431 | if (read(source, buf, 2) < 2) { |
---|
432 | close(source); |
---|
433 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
434 | g_free(phb->host); |
---|
435 | g_free(phb); |
---|
436 | return; |
---|
437 | } |
---|
438 | |
---|
439 | if ((buf[0] != 0x01) || (buf[1] != 0x00)) { |
---|
440 | close(source); |
---|
441 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
442 | g_free(phb->host); |
---|
443 | g_free(phb); |
---|
444 | return; |
---|
445 | } |
---|
446 | |
---|
447 | s5_sendconnect(phb, source); |
---|
448 | } |
---|
449 | |
---|
450 | static void s5_canread(gpointer data, gint source, GaimInputCondition cond) |
---|
451 | { |
---|
452 | unsigned char buf[512]; |
---|
453 | struct PHB *phb = data; |
---|
454 | |
---|
455 | gaim_input_remove(phb->inpa); |
---|
456 | |
---|
457 | if (read(source, buf, 2) < 2) { |
---|
458 | close(source); |
---|
459 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
460 | g_free(phb->host); |
---|
461 | g_free(phb); |
---|
462 | return; |
---|
463 | } |
---|
464 | |
---|
465 | if ((buf[0] != 0x05) || (buf[1] == 0xff)) { |
---|
466 | close(source); |
---|
467 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
468 | g_free(phb->host); |
---|
469 | g_free(phb); |
---|
470 | return; |
---|
471 | } |
---|
472 | |
---|
473 | if (buf[1] == 0x02) { |
---|
474 | unsigned int i = strlen(proxyuser), j = strlen(proxypass); |
---|
475 | buf[0] = 0x01; /* version 1 */ |
---|
476 | buf[1] = i; |
---|
477 | memcpy(buf + 2, proxyuser, i); |
---|
478 | buf[2 + i] = j; |
---|
479 | memcpy(buf + 2 + i + 1, proxypass, j); |
---|
480 | if (write(source, buf, 3 + i + j) < 3 + i + j) { |
---|
481 | close(source); |
---|
482 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
483 | g_free(phb->host); |
---|
484 | g_free(phb); |
---|
485 | return; |
---|
486 | } |
---|
487 | |
---|
488 | phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_readauth, phb); |
---|
489 | } else { |
---|
490 | s5_sendconnect(phb, source); |
---|
491 | } |
---|
492 | } |
---|
493 | |
---|
494 | static void s5_canwrite(gpointer data, gint source, GaimInputCondition cond) |
---|
495 | { |
---|
496 | unsigned char buf[512]; |
---|
497 | int i; |
---|
498 | struct PHB *phb = data; |
---|
499 | unsigned int len; |
---|
500 | int error = ETIMEDOUT; |
---|
501 | if (phb->inpa > 0) |
---|
502 | gaim_input_remove(phb->inpa); |
---|
503 | len = sizeof(error); |
---|
504 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
505 | close(source); |
---|
506 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
507 | g_free(phb->host); |
---|
508 | g_free(phb); |
---|
509 | return; |
---|
510 | } |
---|
511 | #ifdef F_SETFL |
---|
512 | fcntl(source, F_SETFL, 0); |
---|
513 | #endif |
---|
514 | |
---|
515 | i = 0; |
---|
516 | buf[0] = 0x05; /* SOCKS version 5 */ |
---|
517 | if (proxyuser[0]) { |
---|
518 | buf[1] = 0x02; /* two methods */ |
---|
519 | buf[2] = 0x00; /* no authentication */ |
---|
520 | buf[3] = 0x02; /* username/password authentication */ |
---|
521 | i = 4; |
---|
522 | } else { |
---|
523 | buf[1] = 0x01; |
---|
524 | buf[2] = 0x00; |
---|
525 | i = 3; |
---|
526 | } |
---|
527 | |
---|
528 | if (write(source, buf, i) < i) { |
---|
529 | close(source); |
---|
530 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
531 | g_free(phb->host); |
---|
532 | g_free(phb); |
---|
533 | return; |
---|
534 | } |
---|
535 | |
---|
536 | phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread, phb); |
---|
537 | } |
---|
538 | |
---|
539 | static int proxy_connect_socks5(char *host, unsigned short port, struct PHB *phb) |
---|
540 | { |
---|
541 | phb->host = g_strdup(host); |
---|
542 | phb->port = port; |
---|
543 | phb->proxy_func = s5_canwrite; |
---|
544 | phb->proxy_data = phb; |
---|
545 | |
---|
546 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
547 | } |
---|
548 | |
---|
549 | |
---|
550 | /* Export functions */ |
---|
551 | |
---|
552 | gint gaim_input_add(gint source, GaimInputCondition condition, GaimInputFunction function, gpointer data) |
---|
553 | { |
---|
554 | GaimIOClosure *closure = g_new0(GaimIOClosure, 1); |
---|
555 | GIOChannel *channel; |
---|
556 | GIOCondition cond = 0; |
---|
557 | |
---|
558 | closure->function = function; |
---|
559 | closure->data = data; |
---|
560 | |
---|
561 | if (condition & GAIM_INPUT_READ) |
---|
562 | cond |= GAIM_READ_COND; |
---|
563 | if (condition & GAIM_INPUT_WRITE) |
---|
564 | cond |= GAIM_WRITE_COND; |
---|
565 | |
---|
566 | channel = g_io_channel_unix_new(source); |
---|
567 | closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, |
---|
568 | gaim_io_invoke, closure, gaim_io_destroy); |
---|
569 | |
---|
570 | g_io_channel_unref(channel); |
---|
571 | return closure->result; |
---|
572 | } |
---|
573 | |
---|
574 | void gaim_input_remove(gint tag) |
---|
575 | { |
---|
576 | if (tag > 0) |
---|
577 | g_source_remove(tag); |
---|
578 | } |
---|
579 | |
---|
580 | int proxy_connect(char *host, int port, GaimInputFunction func, gpointer data) |
---|
581 | { |
---|
582 | struct PHB *phb; |
---|
583 | |
---|
584 | if (!host || !port || (port == -1) || !func || strlen(host) > 128) { |
---|
585 | return -1; |
---|
586 | } |
---|
587 | |
---|
588 | phb = g_new0(struct PHB, 1); |
---|
589 | phb->func = func; |
---|
590 | phb->data = data; |
---|
591 | |
---|
592 | #ifndef _WIN32 |
---|
593 | sethostent(1); |
---|
594 | #endif |
---|
595 | |
---|
596 | if ((proxytype == PROXY_NONE) || !proxyhost || !proxyhost[0] || !proxyport || (proxyport == -1)) |
---|
597 | return proxy_connect_none(host, port, phb); |
---|
598 | else if (proxytype == PROXY_HTTP) |
---|
599 | return proxy_connect_http(host, port, phb); |
---|
600 | else if (proxytype == PROXY_SOCKS4) |
---|
601 | return proxy_connect_socks4(host, port, phb); |
---|
602 | else if (proxytype == PROXY_SOCKS5) |
---|
603 | return proxy_connect_socks5(host, port, phb); |
---|
604 | |
---|
605 | if (phb->host) g_free(phb); |
---|
606 | g_free(phb); |
---|
607 | return -1; |
---|
608 | } |
---|