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