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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | #define BITLBEE_CORE |
---|
24 | #include <stdio.h> |
---|
25 | #include <stdlib.h> |
---|
26 | #include <string.h> |
---|
27 | #include <sys/types.h> |
---|
28 | #include <sys/socket.h> |
---|
29 | #include <netdb.h> |
---|
30 | #include <netinet/in.h> |
---|
31 | #include <arpa/inet.h> |
---|
32 | #include <unistd.h> |
---|
33 | #include <fcntl.h> |
---|
34 | #include <errno.h> |
---|
35 | #include "nogaim.h" |
---|
36 | #include "proxy.h" |
---|
37 | #include "base64.h" |
---|
38 | |
---|
39 | char proxyhost[128] = ""; |
---|
40 | int proxyport = 0; |
---|
41 | int proxytype = PROXY_NONE; |
---|
42 | char proxyuser[128] = ""; |
---|
43 | char proxypass[128] = ""; |
---|
44 | |
---|
45 | /* Some systems don't know this one. It's not essential, so set it to 0 then. */ |
---|
46 | #ifndef AI_NUMERICSERV |
---|
47 | #define AI_NUMERICSERV 0 |
---|
48 | #endif |
---|
49 | #ifndef AI_ADDRCONFIG |
---|
50 | #define AI_ADDRCONFIG 0 |
---|
51 | #endif |
---|
52 | |
---|
53 | struct PHB { |
---|
54 | b_event_handler func, proxy_func; |
---|
55 | gpointer data, proxy_data; |
---|
56 | char *host; |
---|
57 | int port; |
---|
58 | int fd; |
---|
59 | gint inpa; |
---|
60 | struct addrinfo *gai, *gai_cur; |
---|
61 | }; |
---|
62 | |
---|
63 | static int proxy_connect_none(const char *host, unsigned short port_, struct PHB *phb); |
---|
64 | |
---|
65 | static gboolean gaim_io_connected(gpointer data, gint source, b_input_condition cond) |
---|
66 | { |
---|
67 | struct PHB *phb = data; |
---|
68 | socklen_t len; |
---|
69 | int error = ETIMEDOUT; |
---|
70 | len = sizeof(error); |
---|
71 | |
---|
72 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) { |
---|
73 | if ((phb->gai_cur = phb->gai_cur->ai_next)) { |
---|
74 | int new_fd; |
---|
75 | b_event_remove(phb->inpa); |
---|
76 | if ((new_fd = proxy_connect_none(NULL, 0, phb))) { |
---|
77 | b_event_remove(phb->inpa); |
---|
78 | closesocket(source); |
---|
79 | dup2(new_fd, source); |
---|
80 | closesocket(new_fd); |
---|
81 | phb->inpa = b_input_add(source, B_EV_IO_WRITE, gaim_io_connected, phb); |
---|
82 | return FALSE; |
---|
83 | } |
---|
84 | } |
---|
85 | freeaddrinfo(phb->gai); |
---|
86 | closesocket(source); |
---|
87 | b_event_remove(phb->inpa); |
---|
88 | phb->inpa = 0; |
---|
89 | if( phb->proxy_func ) |
---|
90 | phb->proxy_func(phb->proxy_data, -1, B_EV_IO_READ); |
---|
91 | else { |
---|
92 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
93 | g_free(phb); |
---|
94 | } |
---|
95 | return FALSE; |
---|
96 | } |
---|
97 | freeaddrinfo(phb->gai); |
---|
98 | sock_make_blocking(source); |
---|
99 | b_event_remove(phb->inpa); |
---|
100 | phb->inpa = 0; |
---|
101 | if( phb->proxy_func ) |
---|
102 | phb->proxy_func(phb->proxy_data, source, B_EV_IO_READ); |
---|
103 | else { |
---|
104 | phb->func(phb->data, source, B_EV_IO_READ); |
---|
105 | g_free(phb); |
---|
106 | } |
---|
107 | |
---|
108 | return FALSE; |
---|
109 | } |
---|
110 | |
---|
111 | static int proxy_connect_none(const char *host, unsigned short port_, struct PHB *phb) |
---|
112 | { |
---|
113 | struct sockaddr_in me; |
---|
114 | int fd = -1; |
---|
115 | |
---|
116 | if (phb->gai_cur == NULL) |
---|
117 | { |
---|
118 | int ret; |
---|
119 | char port[6]; |
---|
120 | struct addrinfo hints; |
---|
121 | |
---|
122 | g_snprintf(port, sizeof(port), "%d", port_); |
---|
123 | |
---|
124 | memset(&hints, 0, sizeof(struct addrinfo)); |
---|
125 | hints.ai_family = AF_UNSPEC; |
---|
126 | hints.ai_socktype = SOCK_STREAM; |
---|
127 | hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV; |
---|
128 | |
---|
129 | if (!(ret = getaddrinfo(host, port, &hints, &phb->gai))) |
---|
130 | phb->gai_cur = phb->gai; |
---|
131 | else |
---|
132 | event_debug("gai(): %s\n", gai_strerror(ret)); |
---|
133 | } |
---|
134 | |
---|
135 | for (; phb->gai_cur; phb->gai_cur = phb->gai_cur->ai_next) |
---|
136 | { |
---|
137 | if ((fd = socket(phb->gai_cur->ai_family, phb->gai_cur->ai_socktype, phb->gai_cur->ai_protocol)) < 0) { |
---|
138 | event_debug( "socket failed: %d\n", errno); |
---|
139 | continue; |
---|
140 | } |
---|
141 | |
---|
142 | sock_make_nonblocking(fd); |
---|
143 | |
---|
144 | if (global.conf->iface_out) |
---|
145 | { |
---|
146 | me.sin_family = AF_INET; |
---|
147 | me.sin_port = 0; |
---|
148 | me.sin_addr.s_addr = inet_addr( global.conf->iface_out ); |
---|
149 | |
---|
150 | if (bind(fd, (struct sockaddr *) &me, sizeof(me)) != 0) |
---|
151 | event_debug("bind( %d, \"%s\" ) failure\n", fd, global.conf->iface_out); |
---|
152 | } |
---|
153 | |
---|
154 | event_debug("proxy_connect_none( \"%s\", %d ) = %d\n", host, port_, fd); |
---|
155 | |
---|
156 | if (connect(fd, phb->gai_cur->ai_addr, phb->gai_cur->ai_addrlen) < 0 && !sockerr_again()) { |
---|
157 | event_debug( "connect failed: %s\n", strerror(errno)); |
---|
158 | closesocket(fd); |
---|
159 | fd = -1; |
---|
160 | continue; |
---|
161 | } else { |
---|
162 | phb->inpa = b_input_add(fd, B_EV_IO_WRITE, gaim_io_connected, phb); |
---|
163 | phb->fd = fd; |
---|
164 | |
---|
165 | break; |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | if(fd < 0 && host) |
---|
170 | g_free(phb); |
---|
171 | |
---|
172 | return fd; |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | /* Connecting to HTTP proxies */ |
---|
177 | |
---|
178 | #define HTTP_GOODSTRING "HTTP/1.0 200" |
---|
179 | #define HTTP_GOODSTRING2 "HTTP/1.1 200" |
---|
180 | |
---|
181 | static gboolean http_canread(gpointer data, gint source, b_input_condition cond) |
---|
182 | { |
---|
183 | int nlc = 0; |
---|
184 | int pos = 0; |
---|
185 | struct PHB *phb = data; |
---|
186 | char inputline[8192]; |
---|
187 | |
---|
188 | b_event_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, B_EV_IO_READ); |
---|
201 | g_free(phb->host); |
---|
202 | g_free(phb); |
---|
203 | return FALSE; |
---|
204 | } |
---|
205 | |
---|
206 | close(source); |
---|
207 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
208 | g_free(phb->host); |
---|
209 | g_free(phb); |
---|
210 | |
---|
211 | return FALSE; |
---|
212 | } |
---|
213 | |
---|
214 | static gboolean http_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
215 | { |
---|
216 | char cmd[384]; |
---|
217 | struct PHB *phb = data; |
---|
218 | socklen_t len; |
---|
219 | int error = ETIMEDOUT; |
---|
220 | if (phb->inpa > 0) |
---|
221 | b_event_remove(phb->inpa); |
---|
222 | len = sizeof(error); |
---|
223 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
224 | close(source); |
---|
225 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
226 | g_free(phb->host); |
---|
227 | g_free(phb); |
---|
228 | return FALSE; |
---|
229 | } |
---|
230 | sock_make_blocking(source); |
---|
231 | |
---|
232 | g_snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n", phb->host, phb->port, |
---|
233 | phb->host, phb->port); |
---|
234 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
235 | close(source); |
---|
236 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
237 | g_free(phb->host); |
---|
238 | g_free(phb); |
---|
239 | return FALSE; |
---|
240 | } |
---|
241 | |
---|
242 | if (strlen(proxyuser) > 0) { |
---|
243 | char *t1, *t2; |
---|
244 | t1 = g_strdup_printf("%s:%s", proxyuser, proxypass); |
---|
245 | t2 = tobase64(t1); |
---|
246 | g_free(t1); |
---|
247 | g_snprintf(cmd, sizeof(cmd), "Proxy-Authorization: Basic %s\r\n", t2); |
---|
248 | g_free(t2); |
---|
249 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
250 | close(source); |
---|
251 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
252 | g_free(phb->host); |
---|
253 | g_free(phb); |
---|
254 | return FALSE; |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | g_snprintf(cmd, sizeof(cmd), "\r\n"); |
---|
259 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
260 | close(source); |
---|
261 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
262 | g_free(phb->host); |
---|
263 | g_free(phb); |
---|
264 | return FALSE; |
---|
265 | } |
---|
266 | |
---|
267 | phb->inpa = b_input_add(source, B_EV_IO_READ, http_canread, phb); |
---|
268 | |
---|
269 | return FALSE; |
---|
270 | } |
---|
271 | |
---|
272 | static int proxy_connect_http(const char *host, unsigned short port, struct PHB *phb) |
---|
273 | { |
---|
274 | phb->host = g_strdup(host); |
---|
275 | phb->port = port; |
---|
276 | phb->proxy_func = http_canwrite; |
---|
277 | phb->proxy_data = phb; |
---|
278 | |
---|
279 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | /* Connecting to SOCKS4 proxies */ |
---|
284 | |
---|
285 | static gboolean s4_canread(gpointer data, gint source, b_input_condition cond) |
---|
286 | { |
---|
287 | unsigned char packet[12]; |
---|
288 | struct PHB *phb = data; |
---|
289 | |
---|
290 | b_event_remove(phb->inpa); |
---|
291 | |
---|
292 | memset(packet, 0, sizeof(packet)); |
---|
293 | if (read(source, packet, 9) >= 4 && packet[1] == 90) { |
---|
294 | phb->func(phb->data, source, B_EV_IO_READ); |
---|
295 | g_free(phb->host); |
---|
296 | g_free(phb); |
---|
297 | return FALSE; |
---|
298 | } |
---|
299 | |
---|
300 | close(source); |
---|
301 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
302 | g_free(phb->host); |
---|
303 | g_free(phb); |
---|
304 | |
---|
305 | return FALSE; |
---|
306 | } |
---|
307 | |
---|
308 | static gboolean s4_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
309 | { |
---|
310 | unsigned char packet[12]; |
---|
311 | struct hostent *hp; |
---|
312 | struct PHB *phb = data; |
---|
313 | socklen_t len; |
---|
314 | int error = ETIMEDOUT; |
---|
315 | if (phb->inpa > 0) |
---|
316 | b_event_remove(phb->inpa); |
---|
317 | len = sizeof(error); |
---|
318 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
319 | close(source); |
---|
320 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
321 | g_free(phb->host); |
---|
322 | g_free(phb); |
---|
323 | return FALSE; |
---|
324 | } |
---|
325 | sock_make_blocking(source); |
---|
326 | |
---|
327 | /* XXX does socks4 not support host name lookups by the proxy? */ |
---|
328 | if (!(hp = gethostbyname(phb->host))) { |
---|
329 | close(source); |
---|
330 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
331 | g_free(phb->host); |
---|
332 | g_free(phb); |
---|
333 | return FALSE; |
---|
334 | } |
---|
335 | |
---|
336 | packet[0] = 4; |
---|
337 | packet[1] = 1; |
---|
338 | packet[2] = phb->port >> 8; |
---|
339 | packet[3] = phb->port & 0xff; |
---|
340 | packet[4] = (unsigned char)(hp->h_addr_list[0])[0]; |
---|
341 | packet[5] = (unsigned char)(hp->h_addr_list[0])[1]; |
---|
342 | packet[6] = (unsigned char)(hp->h_addr_list[0])[2]; |
---|
343 | packet[7] = (unsigned char)(hp->h_addr_list[0])[3]; |
---|
344 | packet[8] = 0; |
---|
345 | if (write(source, packet, 9) != 9) { |
---|
346 | close(source); |
---|
347 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
348 | g_free(phb->host); |
---|
349 | g_free(phb); |
---|
350 | return FALSE; |
---|
351 | } |
---|
352 | |
---|
353 | phb->inpa = b_input_add(source, B_EV_IO_READ, s4_canread, phb); |
---|
354 | |
---|
355 | return FALSE; |
---|
356 | } |
---|
357 | |
---|
358 | static int proxy_connect_socks4(const char *host, unsigned short port, struct PHB *phb) |
---|
359 | { |
---|
360 | phb->host = g_strdup(host); |
---|
361 | phb->port = port; |
---|
362 | phb->proxy_func = s4_canwrite; |
---|
363 | phb->proxy_data = phb; |
---|
364 | |
---|
365 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
366 | } |
---|
367 | |
---|
368 | |
---|
369 | /* Connecting to SOCKS5 proxies */ |
---|
370 | |
---|
371 | static gboolean s5_canread_again(gpointer data, gint source, b_input_condition cond) |
---|
372 | { |
---|
373 | unsigned char buf[512]; |
---|
374 | struct PHB *phb = data; |
---|
375 | |
---|
376 | b_event_remove(phb->inpa); |
---|
377 | |
---|
378 | if (read(source, buf, 10) < 10) { |
---|
379 | close(source); |
---|
380 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
381 | g_free(phb->host); |
---|
382 | g_free(phb); |
---|
383 | return FALSE; |
---|
384 | } |
---|
385 | if ((buf[0] != 0x05) || (buf[1] != 0x00)) { |
---|
386 | close(source); |
---|
387 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
388 | g_free(phb->host); |
---|
389 | g_free(phb); |
---|
390 | return FALSE; |
---|
391 | } |
---|
392 | |
---|
393 | phb->func(phb->data, source, B_EV_IO_READ); |
---|
394 | g_free(phb->host); |
---|
395 | g_free(phb); |
---|
396 | |
---|
397 | return FALSE; |
---|
398 | } |
---|
399 | |
---|
400 | static void s5_sendconnect(gpointer data, gint source) |
---|
401 | { |
---|
402 | unsigned char buf[512]; |
---|
403 | struct PHB *phb = data; |
---|
404 | int hlen = strlen(phb->host); |
---|
405 | |
---|
406 | buf[0] = 0x05; |
---|
407 | buf[1] = 0x01; /* CONNECT */ |
---|
408 | buf[2] = 0x00; /* reserved */ |
---|
409 | buf[3] = 0x03; /* address type -- host name */ |
---|
410 | buf[4] = hlen; |
---|
411 | memcpy(buf + 5, phb->host, hlen); |
---|
412 | buf[5 + strlen(phb->host)] = phb->port >> 8; |
---|
413 | buf[5 + strlen(phb->host) + 1] = phb->port & 0xff; |
---|
414 | |
---|
415 | if (write(source, buf, (5 + strlen(phb->host) + 2)) < (5 + strlen(phb->host) + 2)) { |
---|
416 | close(source); |
---|
417 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
418 | g_free(phb->host); |
---|
419 | g_free(phb); |
---|
420 | return; |
---|
421 | } |
---|
422 | |
---|
423 | phb->inpa = b_input_add(source, B_EV_IO_READ, s5_canread_again, phb); |
---|
424 | } |
---|
425 | |
---|
426 | static gboolean s5_readauth(gpointer data, gint source, b_input_condition cond) |
---|
427 | { |
---|
428 | unsigned char buf[512]; |
---|
429 | struct PHB *phb = data; |
---|
430 | |
---|
431 | b_event_remove(phb->inpa); |
---|
432 | |
---|
433 | if (read(source, buf, 2) < 2) { |
---|
434 | close(source); |
---|
435 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
436 | g_free(phb->host); |
---|
437 | g_free(phb); |
---|
438 | return FALSE; |
---|
439 | } |
---|
440 | |
---|
441 | if ((buf[0] != 0x01) || (buf[1] != 0x00)) { |
---|
442 | close(source); |
---|
443 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
444 | g_free(phb->host); |
---|
445 | g_free(phb); |
---|
446 | return FALSE; |
---|
447 | } |
---|
448 | |
---|
449 | s5_sendconnect(phb, source); |
---|
450 | |
---|
451 | return FALSE; |
---|
452 | } |
---|
453 | |
---|
454 | static gboolean s5_canread(gpointer data, gint source, b_input_condition cond) |
---|
455 | { |
---|
456 | unsigned char buf[512]; |
---|
457 | struct PHB *phb = data; |
---|
458 | |
---|
459 | b_event_remove(phb->inpa); |
---|
460 | |
---|
461 | if (read(source, buf, 2) < 2) { |
---|
462 | close(source); |
---|
463 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
464 | g_free(phb->host); |
---|
465 | g_free(phb); |
---|
466 | return FALSE; |
---|
467 | } |
---|
468 | |
---|
469 | if ((buf[0] != 0x05) || (buf[1] == 0xff)) { |
---|
470 | close(source); |
---|
471 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
472 | g_free(phb->host); |
---|
473 | g_free(phb); |
---|
474 | return FALSE; |
---|
475 | } |
---|
476 | |
---|
477 | if (buf[1] == 0x02) { |
---|
478 | unsigned int i = strlen(proxyuser), j = strlen(proxypass); |
---|
479 | buf[0] = 0x01; /* version 1 */ |
---|
480 | buf[1] = i; |
---|
481 | memcpy(buf + 2, proxyuser, i); |
---|
482 | buf[2 + i] = j; |
---|
483 | memcpy(buf + 2 + i + 1, proxypass, j); |
---|
484 | if (write(source, buf, 3 + i + j) < 3 + i + j) { |
---|
485 | close(source); |
---|
486 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
487 | g_free(phb->host); |
---|
488 | g_free(phb); |
---|
489 | return FALSE; |
---|
490 | } |
---|
491 | |
---|
492 | phb->inpa = b_input_add(source, B_EV_IO_READ, s5_readauth, phb); |
---|
493 | } else { |
---|
494 | s5_sendconnect(phb, source); |
---|
495 | } |
---|
496 | |
---|
497 | return FALSE; |
---|
498 | } |
---|
499 | |
---|
500 | static gboolean s5_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
501 | { |
---|
502 | unsigned char buf[512]; |
---|
503 | int i; |
---|
504 | struct PHB *phb = data; |
---|
505 | socklen_t len; |
---|
506 | int error = ETIMEDOUT; |
---|
507 | if (phb->inpa > 0) |
---|
508 | b_event_remove(phb->inpa); |
---|
509 | len = sizeof(error); |
---|
510 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
511 | close(source); |
---|
512 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
513 | g_free(phb->host); |
---|
514 | g_free(phb); |
---|
515 | return FALSE; |
---|
516 | } |
---|
517 | sock_make_blocking(source); |
---|
518 | |
---|
519 | i = 0; |
---|
520 | buf[0] = 0x05; /* SOCKS version 5 */ |
---|
521 | if (proxyuser[0]) { |
---|
522 | buf[1] = 0x02; /* two methods */ |
---|
523 | buf[2] = 0x00; /* no authentication */ |
---|
524 | buf[3] = 0x02; /* username/password authentication */ |
---|
525 | i = 4; |
---|
526 | } else { |
---|
527 | buf[1] = 0x01; |
---|
528 | buf[2] = 0x00; |
---|
529 | i = 3; |
---|
530 | } |
---|
531 | |
---|
532 | if (write(source, buf, i) < i) { |
---|
533 | close(source); |
---|
534 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
535 | g_free(phb->host); |
---|
536 | g_free(phb); |
---|
537 | return FALSE; |
---|
538 | } |
---|
539 | |
---|
540 | phb->inpa = b_input_add(source, B_EV_IO_READ, s5_canread, phb); |
---|
541 | |
---|
542 | return FALSE; |
---|
543 | } |
---|
544 | |
---|
545 | static int proxy_connect_socks5(const char *host, unsigned short port, struct PHB *phb) |
---|
546 | { |
---|
547 | phb->host = g_strdup(host); |
---|
548 | phb->port = port; |
---|
549 | phb->proxy_func = s5_canwrite; |
---|
550 | phb->proxy_data = phb; |
---|
551 | |
---|
552 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
553 | } |
---|
554 | |
---|
555 | |
---|
556 | /* Export functions */ |
---|
557 | |
---|
558 | int proxy_connect(const char *host, int port, b_event_handler func, gpointer data) |
---|
559 | { |
---|
560 | struct PHB *phb; |
---|
561 | |
---|
562 | if (!host || port <= 0 || !func || strlen(host) > 128) { |
---|
563 | return -1; |
---|
564 | } |
---|
565 | |
---|
566 | phb = g_new0(struct PHB, 1); |
---|
567 | phb->func = func; |
---|
568 | phb->data = data; |
---|
569 | |
---|
570 | if (proxytype == PROXY_NONE || !proxyhost[0] || proxyport <= 0) |
---|
571 | return proxy_connect_none(host, port, phb); |
---|
572 | else if (proxytype == PROXY_HTTP) |
---|
573 | return proxy_connect_http(host, port, phb); |
---|
574 | else if (proxytype == PROXY_SOCKS4) |
---|
575 | return proxy_connect_socks4(host, port, phb); |
---|
576 | else if (proxytype == PROXY_SOCKS5) |
---|
577 | return proxy_connect_socks5(host, port, phb); |
---|
578 | |
---|
579 | g_free(phb); |
---|
580 | return -1; |
---|
581 | } |
---|