source: lib/proxy.c @ 31c28a4

Last change on this file since 31c28a4 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
RevLine 
[b7d3cc34]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"
[7ed3199]43#include "base64.h"
[b7d3cc34]44
45char proxyhost[128] = "";
46int proxyport = 0;
47int proxytype = PROXY_NONE;
48char proxyuser[128] = "";
49char proxypass[128] = "";
50
[762d96f]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
[389ce9f]54#endif
55#ifndef AI_ADDRCONFIG
56#define AI_ADDRCONFIG 0
[762d96f]57#endif
58
[b7d3cc34]59struct PHB {
[ba9edaa]60        b_event_handler func, proxy_func;
[b7d3cc34]61        gpointer data, proxy_data;
62        char *host;
63        int port;
64        int fd;
65        gint inpa;
[762d96f]66        struct addrinfo *gai, *gai_cur;
[b7d3cc34]67};
68
[762d96f]69static int proxy_connect_none(const char *host, unsigned short port_, struct PHB *phb);
70
[ba9edaa]71static gboolean gaim_io_connected(gpointer data, gint source, b_input_condition cond)
[b7d3cc34]72{
73        struct PHB *phb = data;
74        unsigned int len;
75        int error = ETIMEDOUT;
76        len = sizeof(error);
77       
78#ifndef _WIN32
[762d96f]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);
[b7d3cc34]92                closesocket(source);
[ba9edaa]93                b_event_remove(phb->inpa);
[b7d3cc34]94                if( phb->proxy_func )
[e046390]95                        phb->proxy_func(phb->proxy_data, -1, B_EV_IO_READ);
[b7d3cc34]96                else {
[e046390]97                        phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]98                        g_free(phb);
99                }
[ba9edaa]100                return FALSE;
[b7d3cc34]101        }
102#endif
[762d96f]103        freeaddrinfo(phb->gai);
[701acdd4]104        sock_make_blocking(source);
[ba9edaa]105        b_event_remove(phb->inpa);
[b7d3cc34]106        if( phb->proxy_func )
[e046390]107                phb->proxy_func(phb->proxy_data, source, B_EV_IO_READ);
[b7d3cc34]108        else {
[e046390]109                phb->func(phb->data, source, B_EV_IO_READ);
[b7d3cc34]110                g_free(phb);
111        }
[ba9edaa]112       
113        return FALSE;
[b7d3cc34]114}
115
[289bd2d]116static int proxy_connect_none(const char *host, unsigned short port_, struct PHB *phb)
[b7d3cc34]117{
[aefaac3a]118        struct sockaddr_in me;
[b7d3cc34]119        int fd = -1;
[762d96f]120       
121        if (phb->gai_cur == NULL)
[289bd2d]122        {
[762d96f]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                }
[289bd2d]146
[762d96f]147                sock_make_nonblocking(fd);
[289bd2d]148
[762d96f]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 );
[289bd2d]154                               
[762d96f]155                        if (bind(fd, (struct sockaddr *) &me, sizeof(me)) != 0)
156                                event_debug("bind( %d, \"%s\" ) failure\n", fd, global.conf->iface_out);
157                }
[289bd2d]158
[762d96f]159                event_debug("proxy_connect_none( \"%s\", %d ) = %d\n", host, port, fd);
[19ac9c5]160       
[762d96f]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;
[289bd2d]171                }
[aefaac3a]172        }
173       
[762d96f]174        if(fd < 0 && host)
[6f7ac17]175                g_free(phb);
[289bd2d]176
177        return fd;
[b7d3cc34]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
[ba9edaa]186static gboolean http_canread(gpointer data, gint source, b_input_condition cond)
[b7d3cc34]187{
188        int nlc = 0;
189        int pos = 0;
190        struct PHB *phb = data;
191        char inputline[8192];
192
[ba9edaa]193        b_event_remove(phb->inpa);
[b7d3cc34]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)) {
[e046390]205                phb->func(phb->data, source, B_EV_IO_READ);
[b7d3cc34]206                g_free(phb->host);
207                g_free(phb);
[ba9edaa]208                return FALSE;
[b7d3cc34]209        }
210
211        close(source);
[e046390]212        phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]213        g_free(phb->host);
214        g_free(phb);
[ba9edaa]215       
216        return FALSE;
[b7d3cc34]217}
218
[ba9edaa]219static gboolean http_canwrite(gpointer data, gint source, b_input_condition cond)
[b7d3cc34]220{
221        char cmd[384];
222        struct PHB *phb = data;
223        unsigned int len;
224        int error = ETIMEDOUT;
225        if (phb->inpa > 0)
[ba9edaa]226                b_event_remove(phb->inpa);
[b7d3cc34]227        len = sizeof(error);
228        if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
229                close(source);
[e046390]230                phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]231                g_free(phb->host);
232                g_free(phb);
[ba9edaa]233                return FALSE;
[b7d3cc34]234        }
[701acdd4]235        sock_make_blocking(source);
[b7d3cc34]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);
[e046390]241                phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]242                g_free(phb->host);
243                g_free(phb);
[ba9edaa]244                return FALSE;
[b7d3cc34]245        }
246
[f618a4a]247        if (strlen(proxyuser) > 0) {
[b7d3cc34]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);
[e046390]256                        phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]257                        g_free(phb->host);
258                        g_free(phb);
[ba9edaa]259                        return FALSE;
[b7d3cc34]260                }
261        }
262
263        g_snprintf(cmd, sizeof(cmd), "\r\n");
264        if (send(source, cmd, strlen(cmd), 0) < 0) {
265                close(source);
[e046390]266                phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]267                g_free(phb->host);
268                g_free(phb);
[ba9edaa]269                return FALSE;
[b7d3cc34]270        }
271
[e046390]272        phb->inpa = b_input_add(source, B_EV_IO_READ, http_canread, phb);
[ba9edaa]273       
274        return FALSE;
[b7d3cc34]275}
276
[c3ffa45]277static int proxy_connect_http(const char *host, unsigned short port, struct PHB *phb)
[b7d3cc34]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
[ba9edaa]290static gboolean s4_canread(gpointer data, gint source, b_input_condition cond)
[b7d3cc34]291{
292        unsigned char packet[12];
293        struct PHB *phb = data;
294
[ba9edaa]295        b_event_remove(phb->inpa);
[b7d3cc34]296
297        memset(packet, 0, sizeof(packet));
298        if (read(source, packet, 9) >= 4 && packet[1] == 90) {
[e046390]299                phb->func(phb->data, source, B_EV_IO_READ);
[b7d3cc34]300                g_free(phb->host);
301                g_free(phb);
[ba9edaa]302                return FALSE;
[b7d3cc34]303        }
304
305        close(source);
[e046390]306        phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]307        g_free(phb->host);
308        g_free(phb);
[ba9edaa]309       
310        return FALSE;
[b7d3cc34]311}
312
[ba9edaa]313static gboolean s4_canwrite(gpointer data, gint source, b_input_condition cond)
[b7d3cc34]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)
[ba9edaa]321                b_event_remove(phb->inpa);
[b7d3cc34]322        len = sizeof(error);
323        if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
324                close(source);
[e046390]325                phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]326                g_free(phb->host);
327                g_free(phb);
[ba9edaa]328                return FALSE;
[b7d3cc34]329        }
[701acdd4]330        sock_make_blocking(source);
[b7d3cc34]331
332        /* XXX does socks4 not support host name lookups by the proxy? */
333        if (!(hp = gethostbyname(phb->host))) {
334                close(source);
[e046390]335                phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]336                g_free(phb->host);
337                g_free(phb);
[ba9edaa]338                return FALSE;
[b7d3cc34]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);
[e046390]352                phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]353                g_free(phb->host);
354                g_free(phb);
[ba9edaa]355                return FALSE;
[b7d3cc34]356        }
357
[e046390]358        phb->inpa = b_input_add(source, B_EV_IO_READ, s4_canread, phb);
[ba9edaa]359       
360        return FALSE;
[b7d3cc34]361}
362
[c3ffa45]363static int proxy_connect_socks4(const char *host, unsigned short port, struct PHB *phb)
[b7d3cc34]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
[ba9edaa]376static gboolean s5_canread_again(gpointer data, gint source, b_input_condition cond)
[b7d3cc34]377{
378        unsigned char buf[512];
379        struct PHB *phb = data;
380
[ba9edaa]381        b_event_remove(phb->inpa);
[b7d3cc34]382
383        if (read(source, buf, 10) < 10) {
384                close(source);
[e046390]385                phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]386                g_free(phb->host);
387                g_free(phb);
[ba9edaa]388                return FALSE;
[b7d3cc34]389        }
390        if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
391                close(source);
[e046390]392                phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]393                g_free(phb->host);
394                g_free(phb);
[ba9edaa]395                return FALSE;
[b7d3cc34]396        }
397
[e046390]398        phb->func(phb->data, source, B_EV_IO_READ);
[b7d3cc34]399        g_free(phb->host);
400        g_free(phb);
[ba9edaa]401       
402        return FALSE;
[b7d3cc34]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);
[ba9edaa]410       
[b7d3cc34]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);
[e046390]422                phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]423                g_free(phb->host);
424                g_free(phb);
425                return;
426        }
427
[e046390]428        phb->inpa = b_input_add(source, B_EV_IO_READ, s5_canread_again, phb);
[b7d3cc34]429}
430
[ba9edaa]431static gboolean s5_readauth(gpointer data, gint source, b_input_condition cond)
[b7d3cc34]432{
433        unsigned char buf[512];
434        struct PHB *phb = data;
435
[ba9edaa]436        b_event_remove(phb->inpa);
[b7d3cc34]437
438        if (read(source, buf, 2) < 2) {
439                close(source);
[e046390]440                phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]441                g_free(phb->host);
442                g_free(phb);
[ba9edaa]443                return FALSE;
[b7d3cc34]444        }
445
446        if ((buf[0] != 0x01) || (buf[1] != 0x00)) {
447                close(source);
[e046390]448                phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]449                g_free(phb->host);
450                g_free(phb);
[ba9edaa]451                return FALSE;
[b7d3cc34]452        }
453
454        s5_sendconnect(phb, source);
[ba9edaa]455       
456        return FALSE;
[b7d3cc34]457}
458
[ba9edaa]459static gboolean s5_canread(gpointer data, gint source, b_input_condition cond)
[b7d3cc34]460{
461        unsigned char buf[512];
462        struct PHB *phb = data;
463
[ba9edaa]464        b_event_remove(phb->inpa);
[b7d3cc34]465
466        if (read(source, buf, 2) < 2) {
467                close(source);
[e046390]468                phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]469                g_free(phb->host);
470                g_free(phb);
[ba9edaa]471                return FALSE;
[b7d3cc34]472        }
473
474        if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
475                close(source);
[e046390]476                phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]477                g_free(phb->host);
478                g_free(phb);
[ba9edaa]479                return FALSE;
[b7d3cc34]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);
[e046390]491                        phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]492                        g_free(phb->host);
493                        g_free(phb);
[ba9edaa]494                        return FALSE;
[b7d3cc34]495                }
496
[e046390]497                phb->inpa = b_input_add(source, B_EV_IO_READ, s5_readauth, phb);
[b7d3cc34]498        } else {
499                s5_sendconnect(phb, source);
500        }
[ba9edaa]501       
502        return FALSE;
[b7d3cc34]503}
504
[ba9edaa]505static gboolean s5_canwrite(gpointer data, gint source, b_input_condition cond)
[b7d3cc34]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)
[ba9edaa]513                b_event_remove(phb->inpa);
[b7d3cc34]514        len = sizeof(error);
515        if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
516                close(source);
[e046390]517                phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]518                g_free(phb->host);
519                g_free(phb);
[ba9edaa]520                return FALSE;
[b7d3cc34]521        }
[701acdd4]522        sock_make_blocking(source);
[b7d3cc34]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);
[e046390]539                phb->func(phb->data, -1, B_EV_IO_READ);
[b7d3cc34]540                g_free(phb->host);
541                g_free(phb);
[ba9edaa]542                return FALSE;
[b7d3cc34]543        }
544
[e046390]545        phb->inpa = b_input_add(source, B_EV_IO_READ, s5_canread, phb);
[ba9edaa]546       
547        return FALSE;
[b7d3cc34]548}
549
[c3ffa45]550static int proxy_connect_socks5(const char *host, unsigned short port, struct PHB *phb)
[b7d3cc34]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
[ba9edaa]563int proxy_connect(const char *host, int port, b_event_handler func, gpointer data)
[b7d3cc34]564{
565        struct PHB *phb;
566       
[58a1449]567        if (!host || port <= 0 || !func || strlen(host) > 128) {
[b7d3cc34]568                return -1;
569        }
570       
571        phb = g_new0(struct PHB, 1);
572        phb->func = func;
573        phb->data = data;
574       
[58a1449]575        if (proxytype == PROXY_NONE || !proxyhost[0] || proxyport <= 0)
[b7d3cc34]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.