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 | static GHashTable *phb_hash = NULL; |
---|
54 | |
---|
55 | struct PHB { |
---|
56 | b_event_handler func, proxy_func; |
---|
57 | gpointer data, proxy_data; |
---|
58 | char *host; |
---|
59 | int port; |
---|
60 | int fd; |
---|
61 | gint inpa; |
---|
62 | struct addrinfo *gai, *gai_cur; |
---|
63 | }; |
---|
64 | |
---|
65 | typedef int (*proxy_connect_func)(const char *host, unsigned short port_, struct PHB *phb); |
---|
66 | |
---|
67 | static int proxy_connect_none(const char *host, unsigned short port_, struct PHB *phb); |
---|
68 | |
---|
69 | static gboolean phb_free(struct PHB *phb, gboolean success) |
---|
70 | { |
---|
71 | g_hash_table_remove(phb_hash, &phb->fd); |
---|
72 | |
---|
73 | if (!success) { |
---|
74 | if (phb->fd > 0) { |
---|
75 | closesocket(phb->fd); |
---|
76 | } |
---|
77 | if (phb->func) { |
---|
78 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
79 | } |
---|
80 | } |
---|
81 | if (phb->gai) { |
---|
82 | freeaddrinfo(phb->gai); |
---|
83 | } |
---|
84 | g_free(phb->host); |
---|
85 | g_free(phb); |
---|
86 | return FALSE; |
---|
87 | } |
---|
88 | |
---|
89 | /* calls phb->func safely by ensuring that the phb struct doesn't exist in the |
---|
90 | * case that proxy_disconnect() is called down there */ |
---|
91 | static gboolean phb_connected(struct PHB *phb, gint source) |
---|
92 | { |
---|
93 | /* save func and data here */ |
---|
94 | b_event_handler func = phb->func; |
---|
95 | gpointer data = phb->data; |
---|
96 | |
---|
97 | /* free the struct so that it can't be freed by the callback */ |
---|
98 | phb_free(phb, TRUE); |
---|
99 | |
---|
100 | /* if any proxy_disconnect() call happens here, it will use the |
---|
101 | * fd (still open), look it up in the hash table, get NULL, and |
---|
102 | * proceed to close the fd and do nothing else */ |
---|
103 | func(data, source, B_EV_IO_READ); |
---|
104 | |
---|
105 | return FALSE; |
---|
106 | } |
---|
107 | |
---|
108 | static gboolean proxy_connected(gpointer data, gint source, b_input_condition cond) |
---|
109 | { |
---|
110 | struct PHB *phb = data; |
---|
111 | socklen_t len; |
---|
112 | int error = ETIMEDOUT; |
---|
113 | |
---|
114 | len = sizeof(error); |
---|
115 | |
---|
116 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) { |
---|
117 | if ((phb->gai_cur = phb->gai_cur->ai_next)) { |
---|
118 | int new_fd; |
---|
119 | b_event_remove(phb->inpa); |
---|
120 | if ((new_fd = proxy_connect_none(NULL, 0, phb))) { |
---|
121 | b_event_remove(phb->inpa); |
---|
122 | closesocket(source); |
---|
123 | dup2(new_fd, source); |
---|
124 | closesocket(new_fd); |
---|
125 | phb->fd = source; |
---|
126 | phb->inpa = b_input_add(source, B_EV_IO_WRITE, proxy_connected, phb); |
---|
127 | return FALSE; |
---|
128 | } |
---|
129 | } |
---|
130 | closesocket(source); |
---|
131 | source = -1; |
---|
132 | /* socket is dead, but continue to clean up */ |
---|
133 | } else { |
---|
134 | sock_make_blocking(source); |
---|
135 | } |
---|
136 | |
---|
137 | freeaddrinfo(phb->gai); |
---|
138 | phb->gai = NULL; |
---|
139 | |
---|
140 | b_event_remove(phb->inpa); |
---|
141 | phb->inpa = 0; |
---|
142 | |
---|
143 | if (phb->proxy_func) { |
---|
144 | phb->proxy_func(phb->proxy_data, source, B_EV_IO_READ); |
---|
145 | } else { |
---|
146 | phb_connected(phb, source); |
---|
147 | } |
---|
148 | |
---|
149 | return FALSE; |
---|
150 | } |
---|
151 | |
---|
152 | static int proxy_connect_none(const char *host, unsigned short port_, struct PHB *phb) |
---|
153 | { |
---|
154 | struct sockaddr_in me; |
---|
155 | int fd = -1; |
---|
156 | |
---|
157 | if (phb->gai_cur == NULL) { |
---|
158 | int ret; |
---|
159 | char port[6]; |
---|
160 | struct addrinfo hints; |
---|
161 | |
---|
162 | g_snprintf(port, sizeof(port), "%d", port_); |
---|
163 | |
---|
164 | memset(&hints, 0, sizeof(struct addrinfo)); |
---|
165 | hints.ai_family = AF_UNSPEC; |
---|
166 | hints.ai_socktype = SOCK_STREAM; |
---|
167 | hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV; |
---|
168 | |
---|
169 | if (!(ret = getaddrinfo(host, port, &hints, &phb->gai))) { |
---|
170 | phb->gai_cur = phb->gai; |
---|
171 | } else { |
---|
172 | event_debug("gai(): %s\n", gai_strerror(ret)); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | for (; phb->gai_cur; phb->gai_cur = phb->gai_cur->ai_next) { |
---|
177 | if ((fd = socket(phb->gai_cur->ai_family, phb->gai_cur->ai_socktype, phb->gai_cur->ai_protocol)) < 0) { |
---|
178 | event_debug("socket failed: %d\n", errno); |
---|
179 | continue; |
---|
180 | } |
---|
181 | |
---|
182 | sock_make_nonblocking(fd); |
---|
183 | |
---|
184 | if (global.conf->iface_out) { |
---|
185 | me.sin_family = AF_INET; |
---|
186 | me.sin_port = 0; |
---|
187 | me.sin_addr.s_addr = inet_addr(global.conf->iface_out); |
---|
188 | |
---|
189 | if (bind(fd, (struct sockaddr *) &me, sizeof(me)) != 0) { |
---|
190 | event_debug("bind( %d, \"%s\" ) failure\n", fd, global.conf->iface_out); |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | event_debug("proxy_connect_none( \"%s\", %d ) = %d\n", host, port_, fd); |
---|
195 | |
---|
196 | if (connect(fd, phb->gai_cur->ai_addr, phb->gai_cur->ai_addrlen) < 0 && !sockerr_again()) { |
---|
197 | event_debug("connect failed: %s\n", strerror(errno)); |
---|
198 | closesocket(fd); |
---|
199 | fd = -1; |
---|
200 | continue; |
---|
201 | } else { |
---|
202 | phb->inpa = b_input_add(fd, B_EV_IO_WRITE, proxy_connected, phb); |
---|
203 | phb->fd = fd; |
---|
204 | |
---|
205 | break; |
---|
206 | } |
---|
207 | } |
---|
208 | |
---|
209 | if (fd < 0 && host) { |
---|
210 | phb_free(phb, TRUE); |
---|
211 | } |
---|
212 | |
---|
213 | return fd; |
---|
214 | } |
---|
215 | |
---|
216 | |
---|
217 | /* Connecting to HTTP proxies */ |
---|
218 | |
---|
219 | #define HTTP_GOODSTRING "HTTP/1.0 200" |
---|
220 | #define HTTP_GOODSTRING2 "HTTP/1.1 200" |
---|
221 | |
---|
222 | static gboolean http_canread(gpointer data, gint source, b_input_condition cond) |
---|
223 | { |
---|
224 | int nlc = 0; |
---|
225 | int pos = 0; |
---|
226 | struct PHB *phb = data; |
---|
227 | char inputline[8192]; |
---|
228 | |
---|
229 | b_event_remove(phb->inpa); |
---|
230 | |
---|
231 | while ((pos < sizeof(inputline) - 1) && (nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) { |
---|
232 | if (inputline[pos - 1] == '\n') { |
---|
233 | nlc++; |
---|
234 | } else if (inputline[pos - 1] != '\r') { |
---|
235 | nlc = 0; |
---|
236 | } |
---|
237 | } |
---|
238 | inputline[pos] = '\0'; |
---|
239 | |
---|
240 | if ((memcmp(HTTP_GOODSTRING, inputline, strlen(HTTP_GOODSTRING)) == 0) || |
---|
241 | (memcmp(HTTP_GOODSTRING2, inputline, strlen(HTTP_GOODSTRING2)) == 0)) { |
---|
242 | return phb_connected(phb, source); |
---|
243 | } |
---|
244 | |
---|
245 | return phb_free(phb, FALSE); |
---|
246 | } |
---|
247 | |
---|
248 | static gboolean http_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
249 | { |
---|
250 | char cmd[384]; |
---|
251 | struct PHB *phb = data; |
---|
252 | socklen_t len; |
---|
253 | int error = ETIMEDOUT; |
---|
254 | |
---|
255 | if (phb->inpa > 0) { |
---|
256 | b_event_remove(phb->inpa); |
---|
257 | } |
---|
258 | len = sizeof(error); |
---|
259 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
260 | return phb_free(phb, FALSE); |
---|
261 | } |
---|
262 | sock_make_blocking(source); |
---|
263 | |
---|
264 | g_snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n", phb->host, phb->port, |
---|
265 | phb->host, phb->port); |
---|
266 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
267 | return phb_free(phb, FALSE); |
---|
268 | } |
---|
269 | |
---|
270 | if (strlen(proxyuser) > 0) { |
---|
271 | char *t1, *t2; |
---|
272 | t1 = g_strdup_printf("%s:%s", proxyuser, proxypass); |
---|
273 | t2 = tobase64(t1); |
---|
274 | g_free(t1); |
---|
275 | g_snprintf(cmd, sizeof(cmd), "Proxy-Authorization: Basic %s\r\n", t2); |
---|
276 | g_free(t2); |
---|
277 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
278 | return phb_free(phb, FALSE); |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | g_snprintf(cmd, sizeof(cmd), "\r\n"); |
---|
283 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
284 | return phb_free(phb, FALSE); |
---|
285 | } |
---|
286 | |
---|
287 | phb->inpa = b_input_add(source, B_EV_IO_READ, http_canread, phb); |
---|
288 | |
---|
289 | return FALSE; |
---|
290 | } |
---|
291 | |
---|
292 | static int proxy_connect_http(const char *host, unsigned short port, struct PHB *phb) |
---|
293 | { |
---|
294 | phb->host = g_strdup(host); |
---|
295 | phb->port = port; |
---|
296 | phb->proxy_func = http_canwrite; |
---|
297 | phb->proxy_data = phb; |
---|
298 | |
---|
299 | return(proxy_connect_none(proxyhost, proxyport, phb)); |
---|
300 | } |
---|
301 | |
---|
302 | |
---|
303 | /* Connecting to SOCKS4 proxies */ |
---|
304 | |
---|
305 | static gboolean s4_canread(gpointer data, gint source, b_input_condition cond) |
---|
306 | { |
---|
307 | unsigned char packet[12]; |
---|
308 | struct PHB *phb = data; |
---|
309 | |
---|
310 | b_event_remove(phb->inpa); |
---|
311 | |
---|
312 | memset(packet, 0, sizeof(packet)); |
---|
313 | if (read(source, packet, 9) >= 4 && packet[1] == 90) { |
---|
314 | return phb_connected(phb, source); |
---|
315 | } |
---|
316 | |
---|
317 | return phb_free(phb, FALSE); |
---|
318 | } |
---|
319 | |
---|
320 | static gboolean s4_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
321 | { |
---|
322 | unsigned char packet[12]; |
---|
323 | struct hostent *hp; |
---|
324 | struct PHB *phb = data; |
---|
325 | socklen_t len; |
---|
326 | int error = ETIMEDOUT; |
---|
327 | gboolean is_socks4a = (proxytype == PROXY_SOCKS4A); |
---|
328 | |
---|
329 | if (phb->inpa > 0) { |
---|
330 | b_event_remove(phb->inpa); |
---|
331 | } |
---|
332 | len = sizeof(error); |
---|
333 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
334 | return phb_free(phb, FALSE); |
---|
335 | } |
---|
336 | sock_make_blocking(source); |
---|
337 | |
---|
338 | if (!is_socks4a && !(hp = gethostbyname(phb->host))) { |
---|
339 | return phb_free(phb, FALSE); |
---|
340 | } |
---|
341 | |
---|
342 | packet[0] = 4; |
---|
343 | packet[1] = 1; |
---|
344 | packet[2] = phb->port >> 8; |
---|
345 | packet[3] = phb->port & 0xff; |
---|
346 | if (is_socks4a) { |
---|
347 | packet[4] = 0; |
---|
348 | packet[5] = 0; |
---|
349 | packet[6] = 0; |
---|
350 | packet[7] = 1; |
---|
351 | } else { |
---|
352 | packet[4] = (unsigned char) (hp->h_addr_list[0])[0]; |
---|
353 | packet[5] = (unsigned char) (hp->h_addr_list[0])[1]; |
---|
354 | packet[6] = (unsigned char) (hp->h_addr_list[0])[2]; |
---|
355 | packet[7] = (unsigned char) (hp->h_addr_list[0])[3]; |
---|
356 | } |
---|
357 | packet[8] = 0; |
---|
358 | if (write(source, packet, 9) != 9) { |
---|
359 | return phb_free(phb, FALSE); |
---|
360 | } |
---|
361 | |
---|
362 | if (is_socks4a) { |
---|
363 | size_t host_len = strlen(phb->host) + 1; /* include the \0 */ |
---|
364 | |
---|
365 | if (write(source, phb->host, host_len) != host_len) { |
---|
366 | return phb_free(phb, FALSE); |
---|
367 | } |
---|
368 | } |
---|
369 | |
---|
370 | phb->inpa = b_input_add(source, B_EV_IO_READ, s4_canread, phb); |
---|
371 | |
---|
372 | return FALSE; |
---|
373 | } |
---|
374 | |
---|
375 | static int proxy_connect_socks4(const char *host, unsigned short port, struct PHB *phb) |
---|
376 | { |
---|
377 | phb->host = g_strdup(host); |
---|
378 | phb->port = port; |
---|
379 | phb->proxy_func = s4_canwrite; |
---|
380 | phb->proxy_data = phb; |
---|
381 | |
---|
382 | return(proxy_connect_none(proxyhost, proxyport, phb)); |
---|
383 | } |
---|
384 | |
---|
385 | |
---|
386 | /* Connecting to SOCKS5 proxies */ |
---|
387 | |
---|
388 | static gboolean s5_canread_again(gpointer data, gint source, b_input_condition cond) |
---|
389 | { |
---|
390 | unsigned char buf[512]; |
---|
391 | struct PHB *phb = data; |
---|
392 | |
---|
393 | b_event_remove(phb->inpa); |
---|
394 | |
---|
395 | if (read(source, buf, 10) < 10) { |
---|
396 | return phb_free(phb, FALSE); |
---|
397 | } |
---|
398 | if ((buf[0] != 0x05) || (buf[1] != 0x00)) { |
---|
399 | return phb_free(phb, FALSE); |
---|
400 | } |
---|
401 | |
---|
402 | return phb_connected(phb, source); |
---|
403 | } |
---|
404 | |
---|
405 | static void s5_sendconnect(gpointer data, gint source) |
---|
406 | { |
---|
407 | unsigned char buf[512]; |
---|
408 | struct PHB *phb = data; |
---|
409 | int hlen = strlen(phb->host); |
---|
410 | |
---|
411 | buf[0] = 0x05; |
---|
412 | buf[1] = 0x01; /* CONNECT */ |
---|
413 | buf[2] = 0x00; /* reserved */ |
---|
414 | buf[3] = 0x03; /* address type -- host name */ |
---|
415 | buf[4] = hlen; |
---|
416 | memcpy(buf + 5, phb->host, hlen); |
---|
417 | buf[5 + strlen(phb->host)] = phb->port >> 8; |
---|
418 | buf[5 + strlen(phb->host) + 1] = phb->port & 0xff; |
---|
419 | |
---|
420 | if (write(source, buf, (5 + strlen(phb->host) + 2)) < (5 + strlen(phb->host) + 2)) { |
---|
421 | phb_free(phb, FALSE); |
---|
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 | return phb_free(phb, FALSE); |
---|
437 | } |
---|
438 | |
---|
439 | if ((buf[0] != 0x01) || (buf[1] != 0x00)) { |
---|
440 | return phb_free(phb, FALSE); |
---|
441 | } |
---|
442 | |
---|
443 | s5_sendconnect(phb, source); |
---|
444 | |
---|
445 | return FALSE; |
---|
446 | } |
---|
447 | |
---|
448 | static gboolean s5_canread(gpointer data, gint source, b_input_condition cond) |
---|
449 | { |
---|
450 | unsigned char buf[512]; |
---|
451 | struct PHB *phb = data; |
---|
452 | |
---|
453 | b_event_remove(phb->inpa); |
---|
454 | |
---|
455 | if (read(source, buf, 2) < 2) { |
---|
456 | return phb_free(phb, FALSE); |
---|
457 | } |
---|
458 | |
---|
459 | if ((buf[0] != 0x05) || (buf[1] == 0xff)) { |
---|
460 | return phb_free(phb, FALSE); |
---|
461 | } |
---|
462 | |
---|
463 | if (buf[1] == 0x02) { |
---|
464 | unsigned int i = strlen(proxyuser), j = strlen(proxypass); |
---|
465 | buf[0] = 0x01; /* version 1 */ |
---|
466 | buf[1] = i; |
---|
467 | memcpy(buf + 2, proxyuser, i); |
---|
468 | buf[2 + i] = j; |
---|
469 | memcpy(buf + 2 + i + 1, proxypass, j); |
---|
470 | if (write(source, buf, 3 + i + j) < 3 + i + j) { |
---|
471 | return phb_free(phb, FALSE); |
---|
472 | } |
---|
473 | |
---|
474 | phb->inpa = b_input_add(source, B_EV_IO_READ, s5_readauth, phb); |
---|
475 | } else { |
---|
476 | s5_sendconnect(phb, source); |
---|
477 | } |
---|
478 | |
---|
479 | return FALSE; |
---|
480 | } |
---|
481 | |
---|
482 | static gboolean s5_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
483 | { |
---|
484 | unsigned char buf[512]; |
---|
485 | int i; |
---|
486 | struct PHB *phb = data; |
---|
487 | socklen_t len; |
---|
488 | int error = ETIMEDOUT; |
---|
489 | |
---|
490 | if (phb->inpa > 0) { |
---|
491 | b_event_remove(phb->inpa); |
---|
492 | } |
---|
493 | len = sizeof(error); |
---|
494 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
495 | return phb_free(phb, FALSE); |
---|
496 | } |
---|
497 | sock_make_blocking(source); |
---|
498 | |
---|
499 | i = 0; |
---|
500 | buf[0] = 0x05; /* SOCKS version 5 */ |
---|
501 | if (proxyuser[0]) { |
---|
502 | buf[1] = 0x02; /* two methods */ |
---|
503 | buf[2] = 0x00; /* no authentication */ |
---|
504 | buf[3] = 0x02; /* username/password authentication */ |
---|
505 | i = 4; |
---|
506 | } else { |
---|
507 | buf[1] = 0x01; |
---|
508 | buf[2] = 0x00; |
---|
509 | i = 3; |
---|
510 | } |
---|
511 | |
---|
512 | if (write(source, buf, i) < i) { |
---|
513 | return phb_free(phb, FALSE); |
---|
514 | } |
---|
515 | |
---|
516 | phb->inpa = b_input_add(source, B_EV_IO_READ, s5_canread, phb); |
---|
517 | |
---|
518 | return FALSE; |
---|
519 | } |
---|
520 | |
---|
521 | static int proxy_connect_socks5(const char *host, unsigned short port, struct PHB *phb) |
---|
522 | { |
---|
523 | phb->host = g_strdup(host); |
---|
524 | phb->port = port; |
---|
525 | phb->proxy_func = s5_canwrite; |
---|
526 | phb->proxy_data = phb; |
---|
527 | |
---|
528 | return(proxy_connect_none(proxyhost, proxyport, phb)); |
---|
529 | } |
---|
530 | |
---|
531 | static const proxy_connect_func proxy_connect_funcs_array[] = { |
---|
532 | proxy_connect_none, /* PROXY_NONE */ |
---|
533 | proxy_connect_http, /* PROXY_HTTP */ |
---|
534 | proxy_connect_socks4, /* PROXY_SOCKS4 */ |
---|
535 | proxy_connect_socks5, /* PROXY_SOCKS5 */ |
---|
536 | proxy_connect_socks4, /* PROXY_SOCKS4A */ |
---|
537 | }; |
---|
538 | |
---|
539 | /* Export functions */ |
---|
540 | |
---|
541 | int proxy_connect(const char *host, int port, b_event_handler func, gpointer data) |
---|
542 | { |
---|
543 | struct PHB *phb; |
---|
544 | proxy_connect_func fun; |
---|
545 | int fd; |
---|
546 | |
---|
547 | if (!phb_hash) { |
---|
548 | phb_hash = g_hash_table_new(g_int_hash, g_int_equal); |
---|
549 | } |
---|
550 | |
---|
551 | if (!host || port <= 0 || !func || strlen(host) > 128) { |
---|
552 | return -1; |
---|
553 | } |
---|
554 | |
---|
555 | phb = g_new0(struct PHB, 1); |
---|
556 | phb->func = func; |
---|
557 | phb->data = data; |
---|
558 | |
---|
559 | if (proxyhost[0] && proxyport > 0 && proxytype >= 0 && proxytype < G_N_ELEMENTS(proxy_connect_funcs_array)) { |
---|
560 | fun = proxy_connect_funcs_array[proxytype]; |
---|
561 | } else { |
---|
562 | fun = proxy_connect_none; |
---|
563 | } |
---|
564 | |
---|
565 | fd = fun(host, port, phb); |
---|
566 | |
---|
567 | if (fd != -1) { |
---|
568 | g_hash_table_insert(phb_hash, &phb->fd, phb); |
---|
569 | } |
---|
570 | |
---|
571 | return fd; |
---|
572 | } |
---|
573 | |
---|
574 | void proxy_disconnect(int fd) |
---|
575 | { |
---|
576 | struct PHB *phb = g_hash_table_lookup(phb_hash, &fd); |
---|
577 | |
---|
578 | if (!phb) { |
---|
579 | /* not in the early part of the connection - just close the fd */ |
---|
580 | closesocket(fd); |
---|
581 | return; |
---|
582 | } |
---|
583 | |
---|
584 | if (phb->inpa) { |
---|
585 | b_event_remove(phb->inpa); |
---|
586 | phb->inpa = 0; |
---|
587 | } |
---|
588 | |
---|
589 | /* avoid calling the callback, which might result in double-free */ |
---|
590 | phb->func = NULL; |
---|
591 | |
---|
592 | /* close and free */ |
---|
593 | phb_free(phb, FALSE); |
---|
594 | } |
---|