[gaim-migrate @ 2985]
[pidgin-git.git] / src / proxy.c
blob2dbe49705a3800755dbe622361445245656c06f4
1 /*
2 * gaim
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 */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netdb.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #include "gaim.h"
41 #include "proxy.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 };
47 int proxyport = 0;
48 int proxytype = 0;
49 char proxyuser[128] = { 0 };
50 char proxypass[128] = { 0 };
52 struct PHB {
53 GaimInputFunction func;
54 gpointer data;
55 char *host;
56 int port;
57 gint inpa;
60 typedef struct _GaimIOClosure {
61 GaimInputFunction function;
62 guint result;
63 gpointer data;
64 } GaimIOClosure;
66 static void gaim_io_destroy(gpointer data)
68 g_free(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);
88 return TRUE;
91 gint gaim_input_add(gint source, GaimInputCondition condition, GaimInputFunction function, gpointer data)
93 GaimIOClosure *closure = g_new0(GaimIOClosure, 1);
94 GIOChannel *channel;
95 GIOCondition cond = 0;
97 closure->function = function;
98 closure->data = data;
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); */
118 if (tag > 0)
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)) {
127 struct hostent *hp;
128 if (!(hp = gethostbyname(host))) {
129 return NULL;
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;
134 } else
135 sin.sin_family = AF_INET;
136 sin.sin_port = htons(port);
138 return &sin;
141 static void no_one_calls(gpointer data, gint source, GaimInputCondition cond)
143 struct PHB *phb = data;
144 unsigned int len;
145 int error = ETIMEDOUT;
146 debug_printf("Connected\n");
147 len = sizeof(error);
148 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
149 close(source);
150 gaim_input_remove(phb->inpa);
151 phb->func(phb->data, -1, GAIM_INPUT_READ);
152 g_free(phb);
153 return;
155 fcntl(source, F_SETFL, 0);
156 gaim_input_remove(phb->inpa);
157 phb->func(phb->data, source, GAIM_INPUT_READ);
158 g_free(phb);
161 static gboolean clean_connect(gpointer data)
163 struct PHB *phb = data;
165 phb->func(phb->data, phb->port, GAIM_INPUT_READ);
166 g_free(phb);
168 return FALSE;
172 static int proxy_connect_none(char *host, unsigned short port, struct PHB *phb)
174 struct sockaddr_in *sin;
175 int fd = -1;
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");
181 g_free(phb);
182 return -1;
185 if ((fd = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) {
186 debug_printf("unable to create socket\n");
187 g_free(phb);
188 return -1;
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);
196 } else {
197 debug_printf("connect failed (errno %d)\n", errno);
198 close(fd);
199 g_free(phb);
200 return -1;
202 } else {
203 unsigned int len;
204 int error = ETIMEDOUT;
205 debug_printf("Connect didn't block\n");
206 len = sizeof(error);
207 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
208 debug_printf("getsockopt failed\n");
209 close(fd);
210 g_free(phb);
211 return -1;
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
217 before we return. */
220 return fd;
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)
228 int nlc = 0;
229 int pos = 0;
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')
237 nlc++;
238 else if (inputline[pos - 1] != '\r')
239 nlc = 0;
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);
248 g_free(phb->host);
249 g_free(phb);
250 return;
253 close(source);
254 phb->func(phb->data, -1, GAIM_INPUT_READ);
255 g_free(phb->host);
256 g_free(phb);
257 return;
260 static void http_canwrite(gpointer data, gint source, GaimInputCondition cond)
262 char cmd[384];
263 struct PHB *phb = data;
264 unsigned int len;
265 int error = ETIMEDOUT;
266 debug_printf("Connected\n");
267 if (phb->inpa > 0)
268 gaim_input_remove(phb->inpa);
269 len = sizeof(error);
270 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
271 close(source);
272 phb->func(phb->data, -1, GAIM_INPUT_READ);
273 g_free(phb->host);
274 g_free(phb);
275 return;
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) {
282 close(source);
283 phb->func(phb->data, -1, GAIM_INPUT_READ);
284 g_free(phb->host);
285 g_free(phb);
286 return;
289 if (proxyuser) {
290 char *t1, *t2;
291 t1 = g_strdup_printf("%s:%s", proxyuser, proxypass);
292 t2 = tobase64(t1);
293 g_free(t1);
294 g_snprintf(cmd, sizeof(cmd), "Proxy-Authorization: Basic %s\r\n", t2);
295 g_free(t2);
296 if (send(source, cmd, strlen(cmd), 0) < 0) {
297 close(source);
298 phb->func(phb->data, -1, GAIM_INPUT_READ);
299 g_free(phb->host);
300 g_free(phb);
301 return;
305 g_snprintf(cmd, sizeof(cmd), "\r\n");
306 if (send(source, cmd, strlen(cmd), 0) < 0) {
307 close(source);
308 phb->func(phb->data, -1, GAIM_INPUT_READ);
309 g_free(phb->host);
310 g_free(phb);
311 return;
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;
320 int fd = -1;
322 debug_printf("connecting to %s:%d via %s:%d using HTTP\n", host, port, proxyhost, proxyport);
324 if (!(sin = gaim_gethostbyname(proxyhost, proxyport))) {
325 g_free(phb);
326 return -1;
329 if ((fd = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) {
330 g_free(phb);
331 return -1;
334 phb->host = g_strdup(host);
335 phb->port = port;
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);
342 } else {
343 close(fd);
344 g_free(phb->host);
345 g_free(phb);
346 return -1;
348 } else {
349 unsigned int len;
350 int error = ETIMEDOUT;
351 debug_printf("Connect didn't block\n");
352 len = sizeof(error);
353 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
354 close(fd);
355 g_free(phb->host);
356 g_free(phb);
357 return -1;
359 fcntl(fd, F_SETFL, 0);
360 http_canwrite(phb, fd, GAIM_INPUT_WRITE);
363 return fd;
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);
376 g_free(phb->host);
377 g_free(phb);
378 return;
381 close(source);
382 phb->func(phb->data, -1, GAIM_INPUT_READ);
383 g_free(phb->host);
384 g_free(phb);
387 static void s4_canwrite(gpointer data, gint source, GaimInputCondition cond)
389 unsigned char packet[12];
390 struct hostent *hp;
391 struct PHB *phb = data;
392 unsigned int len;
393 int error = ETIMEDOUT;
394 debug_printf("Connected\n");
395 if (phb->inpa > 0)
396 gaim_input_remove(phb->inpa);
397 len = sizeof(error);
398 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
399 close(source);
400 phb->func(phb->data, -1, GAIM_INPUT_READ);
401 g_free(phb->host);
402 g_free(phb);
403 return;
405 fcntl(source, F_SETFL, 0);
407 /* XXX does socks4 not support host name lookups by the proxy? */
408 if (!(hp = gethostbyname(phb->host))) {
409 close(source);
410 phb->func(phb->data, -1, GAIM_INPUT_READ);
411 g_free(phb->host);
412 g_free(phb);
413 return;
416 packet[0] = 4;
417 packet[1] = 1;
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];
424 packet[8] = 0;
425 if (write(source, packet, 9) != 9) {
426 close(source);
427 phb->func(phb->data, -1, GAIM_INPUT_READ);
428 g_free(phb->host);
429 g_free(phb);
430 return;
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;
439 int fd = -1;
441 debug_printf("connecting to %s:%d via %s:%d using SOCKS4\n", host, port, proxyhost, proxyport);
443 if (!(sin = gaim_gethostbyname(proxyhost, proxyport))) {
444 g_free(phb);
445 return -1;
448 if ((fd = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) {
449 g_free(phb);
450 return -1;
453 phb->host = g_strdup(host);
454 phb->port = port;
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);
461 } else {
462 close(fd);
463 g_free(phb->host);
464 g_free(phb);
465 return -1;
467 } else {
468 unsigned int len;
469 int error = ETIMEDOUT;
470 debug_printf("Connect didn't block\n");
471 len = sizeof(error);
472 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
473 close(fd);
474 g_free(phb->host);
475 g_free(phb);
476 return -1;
478 fcntl(fd, F_SETFL, 0);
479 s4_canwrite(phb, fd, GAIM_INPUT_WRITE);
482 return fd;
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");
495 close(source);
496 phb->func(phb->data, -1, GAIM_INPUT_READ);
497 g_free(phb->host);
498 g_free(phb);
499 return;
501 if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
502 debug_printf("bad data\n");
503 close(source);
504 phb->func(phb->data, -1, GAIM_INPUT_READ);
505 g_free(phb->host);
506 g_free(phb);
507 return;
510 phb->func(phb->data, source, GAIM_INPUT_READ);
511 g_free(phb->host);
512 g_free(phb);
513 return;
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);
522 buf[0] = 0x05;
523 buf[1] = 0x01; /* CONNECT */
524 buf[2] = 0x00; /* reserved */
525 buf[3] = 0x03; /* address type -- host name */
526 buf[4] = hlen;
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)) {
532 close(source);
533 phb->func(phb->data, -1, GAIM_INPUT_READ);
534 g_free(phb->host);
535 g_free(phb);
536 return;
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) {
551 close(source);
552 phb->func(phb->data, -1, GAIM_INPUT_READ);
553 g_free(phb->host);
554 g_free(phb);
555 return;
558 if ((buf[0] != 0x01) || (buf[1] != 0x00)) {
559 close(source);
560 phb->func(phb->data, -1, GAIM_INPUT_READ);
561 g_free(phb->host);
562 g_free(phb);
563 return;
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) {
578 close(source);
579 phb->func(phb->data, -1, GAIM_INPUT_READ);
580 g_free(phb->host);
581 g_free(phb);
582 return;
585 if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
586 close(source);
587 phb->func(phb->data, -1, GAIM_INPUT_READ);
588 g_free(phb->host);
589 g_free(phb);
590 return;
593 if (buf[1] == 0x02) {
594 unsigned int i = strlen(proxyuser), j = strlen(proxypass);
595 buf[0] = 0x01; /* version 1 */
596 buf[1] = i;
597 memcpy(buf + 2, proxyuser, i);
598 buf[2 + i] = j;
599 memcpy(buf + 2 + i + 1, proxypass, j);
600 if (write(source, buf, 3 + i + j) < 3 + i + j) {
601 close(source);
602 phb->func(phb->data, -1, GAIM_INPUT_READ);
603 g_free(phb->host);
604 g_free(phb);
605 return;
608 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_readauth, phb);
609 } else {
610 s5_sendconnect(phb, source);
614 static void s5_canwrite(gpointer data, gint source, GaimInputCondition cond)
616 unsigned char buf[512];
617 int i;
618 struct PHB *phb = data;
619 unsigned int len;
620 int error = ETIMEDOUT;
621 debug_printf("Connected\n");
622 if (phb->inpa > 0)
623 gaim_input_remove(phb->inpa);
624 len = sizeof(error);
625 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
626 close(source);
627 phb->func(phb->data, -1, GAIM_INPUT_READ);
628 g_free(phb->host);
629 g_free(phb);
630 return;
632 fcntl(source, F_SETFL, 0);
634 i = 0;
635 buf[0] = 0x05; /* SOCKS version 5 */
636 if (proxyuser[0]) {
637 buf[1] = 0x02; /* two methods */
638 buf[2] = 0x00; /* no authentication */
639 buf[3] = 0x02; /* username/password authentication */
640 i = 4;
641 } else {
642 buf[1] = 0x01;
643 buf[2] = 0x00;
644 i = 3;
647 if (write(source, buf, i) < i) {
648 debug_printf("unable to write\n");
649 close(source);
650 phb->func(phb->data, -1, GAIM_INPUT_READ);
651 g_free(phb->host);
652 g_free(phb);
653 return;
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;
662 int fd = -1;
664 debug_printf("connecting to %s:%d via %s:%d using SOCKS5\n", host, port, proxyhost, proxyport);
666 if (!(sin = gaim_gethostbyname(proxyhost, proxyport))) {
667 g_free(phb);
668 return -1;
671 if ((fd = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) {
672 g_free(phb);
673 return -1;
676 phb->host = g_strdup(host);
677 phb->port = port;
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);
684 } else {
685 close(fd);
686 g_free(phb->host);
687 g_free(phb);
688 return -1;
690 } else {
691 unsigned int len;
692 int error = ETIMEDOUT;
693 debug_printf("Connect didn't block\n");
694 len = sizeof(error);
695 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
696 close(fd);
697 g_free(phb->host);
698 g_free(phb);
699 return -1;
701 fcntl(fd, F_SETFL, 0);
702 s5_canwrite(phb, fd, GAIM_INPUT_WRITE);
705 return fd;
708 int proxy_connect(char *host, int port, GaimInputFunction func, gpointer data)
710 struct PHB *phb = g_new0(struct PHB, 1);
711 phb->func = func;
712 phb->data = data;
714 if (!host || !port || (port == -1) || !func) {
715 g_free(phb);
716 return -1;
719 sethostent(1);
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);
730 g_free(phb);
731 return -1;