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