4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* this is a little piece of code to handle proxy connection */
23 /* it is intended to : 1st handle http proxy, using the CONNECT command
24 , 2nd provide an easy way to add socks support */
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
43 #define GAIM_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
44 #define GAIM_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)
46 char proxyhost
[128] = { 0 };
49 char proxyuser
[128] = { 0 };
50 char proxypass
[128] = { 0 };
53 GaimInputFunction func
;
60 typedef struct _GaimIOClosure
{
61 GaimInputFunction function
;
66 static void gaim_io_destroy(gpointer data
)
71 static gboolean
gaim_io_invoke(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
73 GaimIOClosure
*closure
= data
;
74 GaimInputCondition gaim_cond
= 0;
76 if (condition
& GAIM_READ_COND
)
77 gaim_cond
|= GAIM_INPUT_READ
;
78 if (condition
& GAIM_WRITE_COND
)
79 gaim_cond
|= GAIM_INPUT_WRITE
;
82 debug_printf("CLOSURE: callback for %d, fd is %d\n",
83 closure->result, g_io_channel_unix_get_fd(source));
86 closure
->function(closure
->data
, g_io_channel_unix_get_fd(source
), gaim_cond
);
91 gint
gaim_input_add(gint source
, GaimInputCondition condition
, GaimInputFunction function
, gpointer data
)
93 GaimIOClosure
*closure
= g_new0(GaimIOClosure
, 1);
95 GIOCondition cond
= 0;
97 closure
->function
= function
;
100 if (condition
& GAIM_INPUT_READ
)
101 cond
|= GAIM_READ_COND
;
102 if (condition
& GAIM_INPUT_WRITE
)
103 cond
|= GAIM_WRITE_COND
;
105 channel
= g_io_channel_unix_new(source
);
106 closure
->result
= g_io_add_watch_full(channel
, G_PRIORITY_DEFAULT
, cond
,
107 gaim_io_invoke
, closure
, gaim_io_destroy
);
109 /* debug_printf("CLOSURE: adding input watcher %d for fd %d\n", closure->result, source); */
111 g_io_channel_unref(channel
);
112 return closure
->result
;
115 void gaim_input_remove(gint tag
)
117 /* debug_printf("CLOSURE: removing input watcher %d\n", tag); */
119 g_source_remove(tag
);
122 static struct sockaddr_in
*gaim_gethostbyname(char *host
, int port
)
124 static struct sockaddr_in sin
;
126 if (!inet_aton(host
, &sin
.sin_addr
)) {
128 if (!(hp
= gethostbyname(host
))) {
131 memset(&sin
, 0, sizeof(struct sockaddr_in
));
132 memcpy(&sin
.sin_addr
.s_addr
, hp
->h_addr
, hp
->h_length
);
133 sin
.sin_family
= hp
->h_addrtype
;
135 sin
.sin_family
= AF_INET
;
136 sin
.sin_port
= htons(port
);
141 static void no_one_calls(gpointer data
, gint source
, GaimInputCondition cond
)
143 struct PHB
*phb
= data
;
145 int error
= ETIMEDOUT
;
146 debug_printf("Connected\n");
148 if (getsockopt(source
, SOL_SOCKET
, SO_ERROR
, &error
, &len
) < 0) {
150 gaim_input_remove(phb
->inpa
);
151 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
155 fcntl(source
, F_SETFL
, 0);
156 gaim_input_remove(phb
->inpa
);
157 phb
->func(phb
->data
, source
, GAIM_INPUT_READ
);
161 static gboolean
clean_connect(gpointer data
)
163 struct PHB
*phb
= data
;
165 phb
->func(phb
->data
, phb
->port
, GAIM_INPUT_READ
);
172 static int proxy_connect_none(char *host
, unsigned short port
, struct PHB
*phb
)
174 struct sockaddr_in
*sin
;
177 debug_printf("connecting to %s:%d with no proxy\n", host
, port
);
179 if (!(sin
= gaim_gethostbyname(host
, port
))) {
180 debug_printf("gethostbyname failed\n");
185 if ((fd
= socket(sin
->sin_family
, SOCK_STREAM
, 0)) < 0) {
186 debug_printf("unable to create socket\n");
191 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
192 if (connect(fd
, (struct sockaddr
*)sin
, sizeof(*sin
)) < 0) {
193 if ((errno
== EINPROGRESS
) || (errno
== EINTR
)) {
194 debug_printf("Connect would have blocked\n");
195 phb
->inpa
= gaim_input_add(fd
, GAIM_INPUT_WRITE
, no_one_calls
, phb
);
197 debug_printf("connect failed (errno %d)\n", errno
);
204 int error
= ETIMEDOUT
;
205 debug_printf("Connect didn't block\n");
207 if (getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
) < 0) {
208 debug_printf("getsockopt failed\n");
213 fcntl(fd
, F_SETFL
, 0);
214 phb
->port
= fd
; /* bleh */
215 g_timeout_add(50, clean_connect
, phb
); /* we do this because we never
216 want to call our callback
223 #define HTTP_GOODSTRING "HTTP/1.0 200 Connection established"
224 #define HTTP_GOODSTRING2 "HTTP/1.1 200 Connection established"
226 static void http_canread(gpointer data
, gint source
, GaimInputCondition cond
)
230 struct PHB
*phb
= data
;
231 char inputline
[8192];
233 gaim_input_remove(phb
->inpa
);
235 while ((nlc
!= 2) && (read(source
, &inputline
[pos
++], 1) == 1)) {
236 if (inputline
[pos
- 1] == '\n')
238 else if (inputline
[pos
- 1] != '\r')
241 inputline
[pos
] = '\0';
243 debug_printf("Proxy says: %s\n", inputline
);
245 if ((memcmp(HTTP_GOODSTRING
, inputline
, strlen(HTTP_GOODSTRING
)) == 0) ||
246 (memcmp(HTTP_GOODSTRING2
, inputline
, strlen(HTTP_GOODSTRING2
)) == 0)) {
247 phb
->func(phb
->data
, source
, GAIM_INPUT_READ
);
254 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
260 static void http_canwrite(gpointer data
, gint source
, GaimInputCondition cond
)
263 struct PHB
*phb
= data
;
265 int error
= ETIMEDOUT
;
266 debug_printf("Connected\n");
268 gaim_input_remove(phb
->inpa
);
270 if (getsockopt(source
, SOL_SOCKET
, SO_ERROR
, &error
, &len
) < 0) {
272 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
277 fcntl(source
, F_SETFL
, 0);
279 g_snprintf(cmd
, sizeof(cmd
), "CONNECT %s:%d HTTP/1.1\r\nHost = %s:%d\r\n", phb
->host
, phb
->port
,
280 phb
->host
, phb
->port
);
281 if (send(source
, cmd
, strlen(cmd
), 0) < 0) {
283 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
291 t1
= g_strdup_printf("%s:%s", proxyuser
, proxypass
);
294 g_snprintf(cmd
, sizeof(cmd
), "Proxy-Authorization: Basic %s\r\n", t2
);
296 if (send(source
, cmd
, strlen(cmd
), 0) < 0) {
298 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
305 g_snprintf(cmd
, sizeof(cmd
), "\r\n");
306 if (send(source
, cmd
, strlen(cmd
), 0) < 0) {
308 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
314 phb
->inpa
= gaim_input_add(source
, GAIM_INPUT_READ
, http_canread
, phb
);
317 static int proxy_connect_http(char *host
, unsigned short port
, struct PHB
*phb
)
319 struct sockaddr_in
*sin
;
322 debug_printf("connecting to %s:%d via %s:%d using HTTP\n", host
, port
, proxyhost
, proxyport
);
324 if (!(sin
= gaim_gethostbyname(proxyhost
, proxyport
))) {
329 if ((fd
= socket(sin
->sin_family
, SOCK_STREAM
, 0)) < 0) {
334 phb
->host
= g_strdup(host
);
337 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
338 if (connect(fd
, (struct sockaddr
*)sin
, sizeof(*sin
)) < 0) {
339 if ((errno
== EINPROGRESS
) || (errno
== EINTR
)) {
340 debug_printf("Connect would have blocked\n");
341 phb
->inpa
= gaim_input_add(fd
, GAIM_INPUT_WRITE
, http_canwrite
, phb
);
350 int error
= ETIMEDOUT
;
351 debug_printf("Connect didn't block\n");
353 if (getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
) < 0) {
359 fcntl(fd
, F_SETFL
, 0);
360 http_canwrite(phb
, fd
, GAIM_INPUT_WRITE
);
366 static void s4_canread(gpointer data
, gint source
, GaimInputCondition cond
)
368 unsigned char packet
[12];
369 struct PHB
*phb
= data
;
371 gaim_input_remove(phb
->inpa
);
373 memset(packet
, 0, sizeof(packet
));
374 if (read(source
, packet
, 9) >= 4 && packet
[1] == 90) {
375 phb
->func(phb
->data
, source
, GAIM_INPUT_READ
);
382 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
387 static void s4_canwrite(gpointer data
, gint source
, GaimInputCondition cond
)
389 unsigned char packet
[12];
391 struct PHB
*phb
= data
;
393 int error
= ETIMEDOUT
;
394 debug_printf("Connected\n");
396 gaim_input_remove(phb
->inpa
);
398 if (getsockopt(source
, SOL_SOCKET
, SO_ERROR
, &error
, &len
) < 0) {
400 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
405 fcntl(source
, F_SETFL
, 0);
407 /* XXX does socks4 not support host name lookups by the proxy? */
408 if (!(hp
= gethostbyname(phb
->host
))) {
410 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
418 packet
[2] = phb
->port
>> 8;
419 packet
[3] = phb
->port
& 0xff;
420 packet
[4] = (unsigned char)(hp
->h_addr_list
[0])[0];
421 packet
[5] = (unsigned char)(hp
->h_addr_list
[0])[1];
422 packet
[6] = (unsigned char)(hp
->h_addr_list
[0])[2];
423 packet
[7] = (unsigned char)(hp
->h_addr_list
[0])[3];
425 if (write(source
, packet
, 9) != 9) {
427 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
433 phb
->inpa
= gaim_input_add(source
, GAIM_INPUT_READ
, s4_canread
, phb
);
436 static int proxy_connect_socks4(char *host
, unsigned short port
, struct PHB
*phb
)
438 struct sockaddr_in
*sin
;
441 debug_printf("connecting to %s:%d via %s:%d using SOCKS4\n", host
, port
, proxyhost
, proxyport
);
443 if (!(sin
= gaim_gethostbyname(proxyhost
, proxyport
))) {
448 if ((fd
= socket(sin
->sin_family
, SOCK_STREAM
, 0)) < 0) {
453 phb
->host
= g_strdup(host
);
456 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
457 if (connect(fd
, (struct sockaddr
*)sin
, sizeof(*sin
)) < 0) {
458 if ((errno
== EINPROGRESS
) || (errno
== EINTR
)) {
459 debug_printf("Connect would have blocked\n");
460 phb
->inpa
= gaim_input_add(fd
, GAIM_INPUT_WRITE
, s4_canwrite
, phb
);
469 int error
= ETIMEDOUT
;
470 debug_printf("Connect didn't block\n");
472 if (getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
) < 0) {
478 fcntl(fd
, F_SETFL
, 0);
479 s4_canwrite(phb
, fd
, GAIM_INPUT_WRITE
);
485 static void s5_canread_again(gpointer data
, gint source
, GaimInputCondition cond
)
487 unsigned char buf
[512];
488 struct PHB
*phb
= data
;
490 gaim_input_remove(phb
->inpa
);
491 debug_printf("able to read again\n");
493 if (read(source
, buf
, 10) < 10) {
494 debug_printf("or not...\n");
496 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
501 if ((buf
[0] != 0x05) || (buf
[1] != 0x00)) {
502 debug_printf("bad data\n");
504 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
510 phb
->func(phb
->data
, source
, GAIM_INPUT_READ
);
516 static void s5_sendconnect(gpointer data
, gint source
)
518 unsigned char buf
[512];
519 struct PHB
*phb
= data
;
520 int hlen
= strlen(phb
->host
);
523 buf
[1] = 0x01; /* CONNECT */
524 buf
[2] = 0x00; /* reserved */
525 buf
[3] = 0x03; /* address type -- host name */
527 memcpy(buf
+ 5, phb
->host
, hlen
);
528 buf
[5 + strlen(phb
->host
)] = phb
->port
>> 8;
529 buf
[5 + strlen(phb
->host
) + 1] = phb
->port
& 0xff;
531 if (write(source
, buf
, (5 + strlen(phb
->host
) + 2)) < (5 + strlen(phb
->host
) + 2)) {
533 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
539 phb
->inpa
= gaim_input_add(source
, GAIM_INPUT_READ
, s5_canread_again
, phb
);
542 static void s5_readauth(gpointer data
, gint source
, GaimInputCondition cond
)
544 unsigned char buf
[512];
545 struct PHB
*phb
= data
;
547 gaim_input_remove(phb
->inpa
);
548 debug_printf("got auth response\n");
550 if (read(source
, buf
, 2) < 2) {
552 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
558 if ((buf
[0] != 0x01) || (buf
[1] != 0x00)) {
560 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
566 s5_sendconnect(phb
, source
);
569 static void s5_canread(gpointer data
, gint source
, GaimInputCondition cond
)
571 unsigned char buf
[512];
572 struct PHB
*phb
= data
;
574 gaim_input_remove(phb
->inpa
);
575 debug_printf("able to read\n");
577 if (read(source
, buf
, 2) < 2) {
579 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
585 if ((buf
[0] != 0x05) || (buf
[1] == 0xff)) {
587 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
593 if (buf
[1] == 0x02) {
594 unsigned int i
= strlen(proxyuser
), j
= strlen(proxypass
);
595 buf
[0] = 0x01; /* version 1 */
597 memcpy(buf
+ 2, proxyuser
, i
);
599 memcpy(buf
+ 2 + i
+ 1, proxypass
, j
);
600 if (write(source
, buf
, 3 + i
+ j
) < 3 + i
+ j
) {
602 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
608 phb
->inpa
= gaim_input_add(source
, GAIM_INPUT_READ
, s5_readauth
, phb
);
610 s5_sendconnect(phb
, source
);
614 static void s5_canwrite(gpointer data
, gint source
, GaimInputCondition cond
)
616 unsigned char buf
[512];
618 struct PHB
*phb
= data
;
620 int error
= ETIMEDOUT
;
621 debug_printf("Connected\n");
623 gaim_input_remove(phb
->inpa
);
625 if (getsockopt(source
, SOL_SOCKET
, SO_ERROR
, &error
, &len
) < 0) {
627 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
632 fcntl(source
, F_SETFL
, 0);
635 buf
[0] = 0x05; /* SOCKS version 5 */
637 buf
[1] = 0x02; /* two methods */
638 buf
[2] = 0x00; /* no authentication */
639 buf
[3] = 0x02; /* username/password authentication */
647 if (write(source
, buf
, i
) < i
) {
648 debug_printf("unable to write\n");
650 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
656 phb
->inpa
= gaim_input_add(source
, GAIM_INPUT_READ
, s5_canread
, phb
);
659 static int proxy_connect_socks5(char *host
, unsigned short port
, struct PHB
*phb
)
661 struct sockaddr_in
*sin
;
664 debug_printf("connecting to %s:%d via %s:%d using SOCKS5\n", host
, port
, proxyhost
, proxyport
);
666 if (!(sin
= gaim_gethostbyname(proxyhost
, proxyport
))) {
671 if ((fd
= socket(sin
->sin_family
, SOCK_STREAM
, 0)) < 0) {
676 phb
->host
= g_strdup(host
);
679 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
680 if (connect(fd
, (struct sockaddr
*)sin
, sizeof(*sin
)) < 0) {
681 if ((errno
== EINPROGRESS
) || (errno
== EINTR
)) {
682 debug_printf("Connect would have blocked\n");
683 phb
->inpa
= gaim_input_add(fd
, GAIM_INPUT_WRITE
, s5_canwrite
, phb
);
692 int error
= ETIMEDOUT
;
693 debug_printf("Connect didn't block\n");
695 if (getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
) < 0) {
701 fcntl(fd
, F_SETFL
, 0);
702 s5_canwrite(phb
, fd
, GAIM_INPUT_WRITE
);
708 int proxy_connect(char *host
, int port
, GaimInputFunction func
, gpointer data
)
710 struct PHB
*phb
= g_new0(struct PHB
, 1);
714 if (!host
|| !port
|| (port
== -1) || !func
) {
721 if ((proxytype
== PROXY_NONE
) || !proxyhost
|| !proxyhost
[0] || !proxyport
|| (proxyport
== -1))
722 return proxy_connect_none(host
, port
, phb
);
723 else if (proxytype
== PROXY_HTTP
)
724 return proxy_connect_http(host
, port
, phb
);
725 else if (proxytype
== PROXY_SOCKS4
)
726 return proxy_connect_socks4(host
, port
, phb
);
727 else if (proxytype
== PROXY_SOCKS5
)
728 return proxy_connect_socks5(host
, port
, phb
);