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