source: protocols/proxy.c @ 38cc9d4

Last change on this file since 38cc9d4 was 4b1a75a, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-06-25T15:16:44Z

Disabling call to sethostent() because it causes major problems on OpenBSD
(and because I don't see any use for it anyway).

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