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