source: lib/proxy.c @ ce617f0

Last change on this file since ce617f0 was 389ce9f, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-10-24T22:20:40Z

Another compatibility fix: AI_ADDRCONFIG doesn't exist on some systems.

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