[gaim-migrate @ 5891]
[pidgin-git.git] / src / proxy.c
blob7cf932c591481a68c8b09b80905488bb0a8da4cf
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/time.h>
35 #ifndef _WIN32
36 #include <sys/socket.h>
37 #include <netdb.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #include <unistd.h>
41 #include <signal.h>
42 #else
43 #include <winsock.h>
44 #endif
46 #include <fcntl.h>
47 #include <errno.h>
48 #include "gaim.h"
49 #include "proxy.h"
51 #ifdef _WIN32
52 #include "win32dep.h"
53 #endif
55 #define GAIM_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
56 #define GAIM_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)
58 struct gaim_proxy_info global_proxy_info;
60 struct PHB {
61 GaimInputFunction func;
62 gpointer data;
63 char *host;
64 int port;
65 gint inpa;
66 struct gaim_proxy_info *gpi;
67 struct gaim_account *account;
70 typedef struct _GaimIOClosure {
71 GaimInputFunction function;
72 guint result;
73 gpointer data;
74 } GaimIOClosure;
76 static void gaim_io_destroy(gpointer data)
78 g_free(data);
81 static gboolean gaim_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data)
83 GaimIOClosure *closure = data;
84 GaimInputCondition gaim_cond = 0;
86 if (condition & GAIM_READ_COND)
87 gaim_cond |= GAIM_INPUT_READ;
88 if (condition & GAIM_WRITE_COND)
89 gaim_cond |= GAIM_INPUT_WRITE;
92 gaim_debug(GAIM_DEBUG_MISC, "proxy",
93 "CLOSURE: callback for %d, fd is %d\n",
94 closure->result, g_io_channel_unix_get_fd(source));
97 closure->function(closure->data, g_io_channel_unix_get_fd(source), gaim_cond);
99 return TRUE;
102 gint gaim_input_add(gint source, GaimInputCondition condition, GaimInputFunction function, gpointer data)
104 GaimIOClosure *closure = g_new0(GaimIOClosure, 1);
105 GIOChannel *channel;
106 GIOCondition cond = 0;
108 closure->function = function;
109 closure->data = data;
111 if (condition & GAIM_INPUT_READ)
112 cond |= GAIM_READ_COND;
113 if (condition & GAIM_INPUT_WRITE)
114 cond |= GAIM_WRITE_COND;
116 channel = g_io_channel_unix_new(source);
117 closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond,
118 gaim_io_invoke, closure, gaim_io_destroy);
121 gaim_debug(GAIM_DEBUG_MISC, "proxy",
122 "CLOSURE: adding input watcher %d for fd %d\n",
123 closure->result, source);
126 g_io_channel_unref(channel);
127 return closure->result;
130 void gaim_input_remove(gint tag)
132 /* gaim_debug(GAIM_DEBUG_MISC, "proxy",
133 "CLOSURE: removing input watcher %d\n", tag); */
134 if (tag > 0)
135 g_source_remove(tag);
139 typedef void (*dns_callback_t)(GSList *hosts, gpointer data,
140 const char *error_message);
142 #ifdef __unix__
144 /* This structure represents both a pending DNS request and
145 * a free child process.
147 typedef struct {
148 char *host;
149 int port;
150 dns_callback_t callback;
151 gpointer data;
152 gint inpa;
153 int fd_in, fd_out;
154 pid_t dns_pid;
155 } pending_dns_request_t;
157 static GSList *free_dns_children = NULL;
158 static GQueue *queued_requests = NULL;
160 static int number_of_dns_children = 0;
162 const int MAX_DNS_CHILDREN = 2;
164 typedef struct {
165 char hostname[512];
166 int port;
167 } dns_params_t;
169 typedef struct {
170 dns_params_t params;
171 dns_callback_t callback;
172 gpointer data;
173 } queued_dns_request_t;
175 static void req_free(pending_dns_request_t *req)
177 g_return_if_fail(req != NULL);
178 if(req->host)
179 g_free(req->host);
180 close(req->fd_in);
181 close(req->fd_out);
182 g_free(req);
183 number_of_dns_children--;
186 static int send_dns_request_to_child(pending_dns_request_t *req, dns_params_t *dns_params)
188 char ch;
189 int rc;
191 /* Are you alive? */
192 if(kill(req->dns_pid, 0) != 0) {
193 gaim_debug(GAIM_DEBUG_WARNING, "dns",
194 "DNS child %d no longer exists\n", req->dns_pid);
195 return -1;
198 /* Let's contact this lost child! */
199 rc = write(req->fd_in, dns_params, sizeof(*dns_params));
200 if(rc<0) {
201 gaim_debug(GAIM_DEBUG_ERROR, "dns",
202 "Unable to write to DNS child %d: %d\n",
203 req->dns_pid, strerror(errno));
204 close(req->fd_in);
205 return -1;
208 g_return_val_if_fail(rc == sizeof(*dns_params), -1);
210 /* Did you hear me? (This avoids some race conditions) */
211 rc = read(req->fd_out, &ch, 1);
212 if(rc != 1 || ch!='Y') {
213 gaim_debug(GAIM_DEBUG_WARNING, "dns",
214 "DNS child %d not responding. Killing it!\n",
215 req->dns_pid);
216 kill(req->dns_pid, SIGKILL);
217 return -1;
220 gaim_debug(GAIM_DEBUG_INFO, "dns",
221 "Successfully sent DNS request to child %d\n", req->dns_pid);
222 return 0;
225 static void host_resolved(gpointer data, gint source, GaimInputCondition cond);
227 static void release_dns_child(pending_dns_request_t *req)
229 g_free(req->host);
230 req->host=NULL;
232 if(queued_requests && !g_queue_is_empty(queued_requests)) {
233 queued_dns_request_t *r = g_queue_pop_head(queued_requests);
234 req->host = g_strdup(r->params.hostname);
235 req->port = r->params.port;
236 req->callback = r->callback;
237 req->data = r->data;
239 gaim_debug(GAIM_DEBUG_INFO, "dns",
240 "Processing queued DNS query for '%s' with child %d\n",
241 req->host, req->dns_pid);
243 if(send_dns_request_to_child(req, &(r->params)) != 0) {
244 req_free(req);
245 req = NULL;
247 gaim_debug(GAIM_DEBUG_WARNING, "dns",
248 "Intent of process queued query of '%s' failed, "
249 "requeueing...\n", r->params.hostname);
250 g_queue_push_head(queued_requests, r);
251 } else {
252 req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req);
253 g_free(r);
256 } else {
257 req->host = NULL;
258 req->callback = NULL;
259 req->data = NULL;
260 free_dns_children = g_slist_append(free_dns_children, req);
264 static void host_resolved(gpointer data, gint source, GaimInputCondition cond)
266 pending_dns_request_t *req = (pending_dns_request_t*)data;
267 int rc, err;
268 GSList *hosts = NULL;
269 struct sockaddr *addr = NULL;
270 socklen_t addrlen;
272 gaim_debug(GAIM_DEBUG_INFO, "dns", "Host '%s' resolved\n", req->host);
273 gaim_input_remove(req->inpa);
275 rc=read(req->fd_out, &err, sizeof(err));
276 if((rc==4) && (err!=0)) {
277 char message[1024];
278 g_snprintf(message, sizeof(message), "DNS error: %s (pid=%d)",
279 #ifdef HAVE_GETADDRINFO
280 gai_strerror(err),
281 #else
282 hstrerror(err),
283 #endif
284 req->dns_pid);
285 gaim_debug(GAIM_DEBUG_ERROR, "dns", "%s\n", message);
286 req->callback(NULL, req->data, message);
287 release_dns_child(req);
288 return;
290 if(rc>0) {
291 while(rc > 0) {
292 rc=read(req->fd_out, &addrlen, sizeof(addrlen));
293 if(rc>0 && addrlen > 0) {
294 addr=g_malloc(addrlen);
295 rc=read(req->fd_out, addr, addrlen);
296 hosts = g_slist_append(hosts, GINT_TO_POINTER(addrlen));
297 hosts = g_slist_append(hosts, addr);
298 } else {
299 break;
302 } else if(rc==-1) {
303 char message[1024];
304 g_snprintf(message, sizeof(message), "Error reading from DNS child: %s",strerror(errno));
305 gaim_debug(GAIM_DEBUG_ERROR, "dns", "%s\n", message);
306 req->callback(NULL, req->data, message);
307 req_free(req);
308 return;
309 } else if(rc==0) {
310 char message[1024];
311 g_snprintf(message, sizeof(message), "EOF reading from DNS child");
312 close(req->fd_out);
313 gaim_debug(GAIM_DEBUG_ERROR, "dns", "%s\n", message);
314 req->callback(NULL, req->data, message);
315 req_free(req);
316 return;
319 /* wait4(req->dns_pid, NULL, WNOHANG, NULL); */
321 req->callback(hosts, req->data, NULL);
323 while(hosts) {
324 hosts = g_slist_remove(hosts, hosts->data);
325 g_free(hosts->data);
326 hosts = g_slist_remove(hosts, hosts->data);
329 release_dns_child(req);
332 static void trap_gdb_bug()
334 const char *message =
335 "Gaim's DNS child got a SIGTRAP signal. \n"
336 "This can be caused by trying to run gaim inside gdb.\n"
337 "There is a known gdb bug which prevents this. Supposedly gaim\n"
338 "should have detected you were using gdb and used an ugly hack,\n"
339 "check cope_with_gdb_brokenness() in proxy.c.\n\n"
340 "For more info about this bug, see http://sources.redhat.com/ml/gdb/2001-07/msg00349.html\n";
341 fputs("\n* * *\n",stderr);
342 fputs(message,stderr);
343 fputs("* * *\n\n",stderr);
344 execlp("xmessage","xmessage","-center", message, NULL);
345 _exit(1);
348 static void cope_with_gdb_brokenness()
350 static gboolean already_done = FALSE;
351 char s[300], e[300];
352 int n;
353 pid_t ppid;
355 #ifdef __linux__
356 if(already_done)
357 return;
358 already_done = TRUE;
359 ppid = getppid();
360 snprintf(s, 300, "/proc/%d/exe", ppid);
361 n = readlink(s, e, sizeof(e));
362 e[MAX(n,sizeof(e)-1)] = '\0';
364 if(strstr(e,"gdb")) {
365 gaim_debug(GAIM_DEBUG_INFO, "dns",
366 "Debugger detected, performing useless query...\n");
367 gethostbyname("x.x.x.x.x");
369 #endif
372 int gaim_gethostbyname_async(const char *hostname, int port, dns_callback_t callback, gpointer data)
374 pending_dns_request_t *req = NULL;
375 dns_params_t dns_params;
377 strncpy(dns_params.hostname, hostname, sizeof(dns_params.hostname)-1);
378 dns_params.hostname[sizeof(dns_params.hostname)-1] = '\0';
379 dns_params.port = port;
381 /* Is there a free available child? */
382 while(free_dns_children && !req) {
383 GSList *l = free_dns_children;
384 free_dns_children = g_slist_remove_link(free_dns_children, l);
385 req = l->data;
386 g_slist_free(l);
388 if(send_dns_request_to_child(req, &dns_params) != 0) {
389 req_free(req);
390 req = NULL;
391 continue;
396 if(!req) {
397 int child_out[2], child_in[2];
399 if(number_of_dns_children >= MAX_DNS_CHILDREN) {
400 queued_dns_request_t *r = g_new(queued_dns_request_t, 1);
401 memcpy(&(r->params), &dns_params, sizeof(dns_params));
402 r->callback = callback;
403 r->data = data;
404 if(!queued_requests)
405 queued_requests = g_queue_new();
406 g_queue_push_tail(queued_requests, r);
408 gaim_debug(GAIM_DEBUG_INFO, "dns",
409 "DNS query for '%s' queued\n", hostname);
411 return 0;
414 if(pipe(child_out) || pipe(child_in)) {
415 gaim_debug(GAIM_DEBUG_ERROR, "dns",
416 "Could not create pipes: %s\n", strerror(errno));
417 return -1;
420 /* We need to create a new child. */
421 req = g_new(pending_dns_request_t,1);
423 cope_with_gdb_brokenness();
425 req->dns_pid=fork();
426 if(req->dns_pid==0) {
427 const int zero = 0;
428 int rc;
430 #ifdef HAVE_GETADDRINFO
431 struct addrinfo hints, *res, *tmp;
432 char servname[20];
433 #else
434 struct sockaddr_in sin;
435 const socklen_t addrlen = sizeof(sin);
436 #endif
437 #ifdef HAVE_SIGNAL_H
438 signal(SIGHUP, SIG_DFL);
439 signal(SIGINT, SIG_DFL);
440 signal(SIGQUIT, SIG_DFL);
441 signal(SIGCHLD, SIG_DFL);
442 signal(SIGTERM, SIG_DFL);
443 signal(SIGTRAP, trap_gdb_bug);
444 #endif
447 close(child_out[0]);
448 close(child_in[1]);
450 while(1) {
451 if(dns_params.hostname[0] == '\0') {
452 const char Y = 'Y';
453 fd_set fds;
454 struct timeval tv = { .tv_sec = 40 , .tv_usec = 0 };
455 FD_ZERO(&fds);
456 FD_SET(child_in[0], &fds);
457 rc = select(child_in[0]+1, &fds, NULL, NULL, &tv);
458 if(!rc) {
459 if(opt_debug)
460 fprintf(stderr,"dns[%d]: nobody needs me... =(\n", getpid());
461 break;
463 rc = read(child_in[0], &dns_params, sizeof(dns_params));
464 if(rc < 0) {
465 perror("read()");
466 break;
468 if(rc==0) {
469 if(opt_debug)
470 fprintf(stderr,"dns[%d]: Ops, father has gone, wait for me, wait...!\n", getpid());
471 _exit(0);
473 if(dns_params.hostname[0] == '\0') {
474 fprintf(stderr, "dns[%d]: hostname = \"\" (port = %d)!!!\n", getpid(), dns_params.port);
475 _exit(1);
477 write(child_out[1], &Y, 1);
480 #ifdef HAVE_GETADDRINFO
481 g_snprintf(servname, sizeof(servname), "%d", dns_params.port);
482 memset(&hints,0,sizeof(hints));
484 /* This is only used to convert a service
485 * name to a port number. As we know we are
486 * passing a number already, we know this
487 * value will not be really used by the C
488 * library.
490 hints.ai_socktype = SOCK_STREAM;
491 rc = getaddrinfo(dns_params.hostname, servname, &hints, &res);
492 if(rc) {
493 write(child_out[1], &rc, sizeof(int));
494 close(child_out[1]);
495 if(opt_debug)
496 fprintf(stderr,"dns[%d] Error: getaddrinfo returned %d\n",
497 getpid(), rc);
498 dns_params.hostname[0] = '\0';
499 continue;
501 write(child_out[1], &zero, sizeof(zero));
502 tmp = res;
503 while(res) {
504 write(child_out[1], &(res->ai_addrlen), sizeof(res->ai_addrlen));
505 write(child_out[1], res->ai_addr, res->ai_addrlen);
506 res = res->ai_next;
508 freeaddrinfo(tmp);
509 write(child_out[1], &zero, sizeof(zero));
510 #else
511 if (!inet_aton(hostname, &sin.sin_addr)) {
512 struct hostent *hp;
513 if(!(hp = gethostbyname(dns_params.hostname))) {
514 write(child_out[1], &h_errno, sizeof(int));
515 close(child_out[1]);
516 if(opt_debug)
517 fprintf(stderr,"DNS Error: %s\n",hstrerror(h_errno));
518 _exit(0);
520 memset(&sin, 0, sizeof(struct sockaddr_in));
521 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
522 sin.sin_family = hp->h_addrtype;
523 } else
524 sin.sin_family = AF_INET;
525 sin.sin_port = htons(dns_params.port);
526 write(child_out[1], &zero, sizeof(zero));
527 write(child_out[1], &addrlen, sizeof(addrlen));
528 write(child_out[1], &sin, addrlen);
529 write(child_out[1], &zero, sizeof(zero));
530 #endif
531 dns_params.hostname[0] = '\0';
533 close(child_out[1]);
534 close(child_in[0]);
535 _exit(0);
537 close(child_out[1]);
538 close(child_in[0]);
539 if(req->dns_pid==-1) {
540 gaim_debug(GAIM_DEBUG_ERROR, "dns",
541 "Could not create child process for DNS: %s\n",
542 strerror(errno));
543 g_free(req);
544 return -1;
546 req->fd_in = child_in[1];
547 req->fd_out = child_out[0];
548 number_of_dns_children++;
549 gaim_debug(GAIM_DEBUG_INFO, "dns",
550 "Created new DNS child %d, there are now %d children.\n",
551 req->dns_pid, number_of_dns_children);
553 req->host=g_strdup(hostname);
554 req->port=port;
555 req->callback=callback;
556 req->data=data;
557 req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req);
558 return 0;
560 #else
562 typedef struct {
563 gpointer data;
564 size_t addrlen;
565 struct sockaddr *addr;
566 dns_callback_t callback;
567 } pending_dns_request_t;
569 static gboolean host_resolved(gpointer data)
571 pending_dns_request_t *req = (pending_dns_request_t*)data;
572 GSList *hosts = NULL;
573 hosts = g_slist_append(hosts, GINT_TO_POINTER(req->addrlen));
574 hosts = g_slist_append(hosts, req->addr);
575 req->callback(hosts, req->data, NULL);
576 g_slist_free(hosts);
577 g_free(req->addr);
578 g_free(req);
579 return FALSE;
582 int gaim_gethostbyname_async(const char *hostname, int port, dns_callback_t callback, gpointer data)
584 struct sockaddr_in sin;
585 pending_dns_request_t *req;
587 if (!inet_aton(hostname, &sin.sin_addr)) {
588 struct hostent *hp;
589 if(!(hp = gethostbyname(hostname))) {
590 gaim_debug(GAIM_DEBUG_ERROR, "dns",
591 "gaim_gethostbyname(\"%s\", %d) failed: %s\n",
592 hostname, port, hstrerror(h_errno));
593 return -1;
595 memset(&sin, 0, sizeof(struct sockaddr_in));
596 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
597 sin.sin_family = hp->h_addrtype;
598 } else
599 sin.sin_family = AF_INET;
600 sin.sin_port = htons(port);
602 req = g_new(pending_dns_request_t, 1);
603 req->addr = (struct sockaddr*) g_memdup(&sin, sizeof(sin));
604 req->addrlen = sizeof(sin);
605 req->data = data;
606 req->callback = callback;
607 g_timeout_add(10, host_resolved, req);
608 return 0;
611 #endif
613 static void no_one_calls(gpointer data, gint source, GaimInputCondition cond)
615 struct PHB *phb = data;
616 unsigned int len;
617 int error=0;
618 int ret=0;
620 gaim_debug(GAIM_DEBUG_INFO, "proxy", "Connected.\n");
622 len = sizeof(error);
624 ret = getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len);
625 if (ret < 0 || error != 0) {
626 if(ret==0) errno = error;
627 close(source);
628 gaim_input_remove(phb->inpa);
629 if(!phb->account || phb->account->gc)
630 phb->func(phb->data, -1, GAIM_INPUT_READ);
631 g_free(phb->host);
632 g_free(phb);
634 gaim_debug(GAIM_DEBUG_ERROR, "proxy",
635 "getsockopt SO_ERROR check: %s\n", strerror(errno));
636 return;
638 fcntl(source, F_SETFL, 0);
639 gaim_input_remove(phb->inpa);
640 if(!phb->account || phb->account->gc)
641 phb->func(phb->data, source, GAIM_INPUT_READ);
642 g_free(phb->host);
643 g_free(phb);
646 static gboolean clean_connect(gpointer data)
648 struct PHB *phb = data;
650 if(!phb->account || phb->account->gc)
651 phb->func(phb->data, phb->port, GAIM_INPUT_READ);
652 g_free(phb->host);
653 g_free(phb);
655 return FALSE;
659 static int proxy_connect_none(struct PHB *phb, struct sockaddr *addr, socklen_t addrlen)
661 int fd = -1;
663 gaim_debug(GAIM_DEBUG_INFO, "proxy",
664 "Connecting to %s:%d with no proxy\n", phb->host, phb->port);
666 if ((fd = socket(addr->sa_family, SOCK_STREAM, 0)) < 0) {
667 gaim_debug(GAIM_DEBUG_ERROR, "proxy",
668 "Unable to create socket: %s\n", strerror(errno));
669 return -1;
671 fcntl(fd, F_SETFL, O_NONBLOCK);
673 if (connect(fd, (struct sockaddr *)addr, addrlen) < 0) {
674 if ((errno == EINPROGRESS) || (errno == EINTR)) {
675 gaim_debug(GAIM_DEBUG_WARNING, "proxy",
676 "Connect would have blocked.\n");
677 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, no_one_calls, phb);
678 } else {
679 gaim_debug(GAIM_DEBUG_ERROR, "proxy",
680 "Connect failed (errno %d)\n", errno);
681 close(fd);
682 return -1;
684 } else {
685 unsigned int len;
686 int error = ETIMEDOUT;
687 gaim_debug(GAIM_DEBUG_MISC, "proxy", "Connect didn't block.\n");
688 len = sizeof(error);
689 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
690 gaim_debug(GAIM_DEBUG_ERROR, "proxy", "getsockopt failed.\n");
691 close(fd);
692 return -1;
694 fcntl(fd, F_SETFL, 0);
695 phb->port = fd; /* bleh */
696 g_timeout_add(50, clean_connect, phb); /* we do this because we never
697 want to call our callback
698 before we return. */
701 return fd;
704 #define HTTP_GOODSTRING "HTTP/1.0 200"
705 #define HTTP_GOODSTRING2 "HTTP/1.1 200"
707 static void http_canread(gpointer data, gint source, GaimInputCondition cond)
709 int nlc = 0;
710 int pos = 0;
711 int minor, major, status, error=0;
712 struct PHB *phb = data;
713 char inputline[8192], *p;
715 gaim_input_remove(phb->inpa);
717 while ((nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) {
718 if (inputline[pos - 1] == '\n')
719 nlc++;
720 else if (inputline[pos - 1] != '\r')
721 nlc = 0;
723 inputline[pos] = '\0';
725 error = strncmp(inputline, "HTTP/", 5) != 0;
726 if(!error) {
727 p = inputline + 5;
728 major = strtol(p, &p, 10);
729 error = (major==0) || (*p != '.');
730 if(!error) {
731 p++;
732 minor = strtol(p, &p, 10);
733 error = (*p!=' ');
734 if(!error) {
735 p++;
736 status = strtol(p, &p, 10);
737 error = (*p!=' ');
742 if(error) {
743 gaim_debug(GAIM_DEBUG_ERROR, "proxy",
744 "Unable to parse proxy's response: %s\n", inputline);
745 close(source);
746 source=-1;
747 } else if(status!=200) {
748 gaim_debug(GAIM_DEBUG_ERROR, "proxy",
749 "Proxy server replied: (%s)\n", p);
750 close(source);
751 source=-1;
754 if(!phb->account || phb->account->gc)
755 phb->func(phb->data, source, GAIM_INPUT_READ);
756 g_free(phb->host);
757 g_free(phb);
758 return;
761 static void http_canwrite(gpointer data, gint source, GaimInputCondition cond)
763 char request[8192];
764 int request_len = 0;
765 struct PHB *phb = data;
766 unsigned int len;
767 int error = ETIMEDOUT;
769 gaim_debug(GAIM_DEBUG_INFO, "http proxy", "Connected.\n");
771 if (phb->inpa > 0)
772 gaim_input_remove(phb->inpa);
773 len = sizeof(error);
774 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
775 close(source);
776 if(!phb->account || phb->account->gc)
777 phb->func(phb->data, -1, GAIM_INPUT_READ);
778 g_free(phb->host);
779 g_free(phb);
780 return;
782 request_len = g_snprintf(request, sizeof(request), "CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n", phb->host, phb->port,
783 phb->host, phb->port);
785 if (phb->gpi->proxyuser) {
786 char *t1, *t2;
787 t1 = g_strdup_printf("%s:%s", phb->gpi->proxyuser, phb->gpi->proxypass);
788 t2 = tobase64(t1, -1);
789 g_free(t1);
790 g_return_if_fail(request_len < sizeof(request));
791 request_len += g_snprintf(request + request_len, sizeof(request) - request_len, "Proxy-Authorization: Basic %s\r\n", t2);
792 g_free(t2);
795 g_return_if_fail(request_len < sizeof(request));
796 strcpy(request + request_len, "\r\n");
797 request_len += 2;
799 if (write(source, request, request_len) < 0) {
800 close(source);
801 if(!phb->account || phb->account->gc)
802 phb->func(phb->data, -1, GAIM_INPUT_READ);
803 g_free(phb->host);
804 g_free(phb);
805 return;
808 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, http_canread, phb);
811 static int proxy_connect_http(struct PHB *phb, struct sockaddr *addr, socklen_t addrlen)
813 int fd = -1;
815 gaim_debug(GAIM_DEBUG_INFO, "http proxy",
816 "Connecting to %s:%d via %s:%d using HTTP\n",
817 phb->host, phb->port, phb->gpi->proxyhost,
818 phb->gpi->proxyport);
820 if ((fd = socket(addr->sa_family, SOCK_STREAM, 0)) < 0) {
821 return -1;
824 fcntl(fd, F_SETFL, O_NONBLOCK);
826 if (connect(fd, addr, addrlen) < 0) {
827 if ((errno == EINPROGRESS) || (errno == EINTR)) {
828 gaim_debug(GAIM_DEBUG_WARNING, "http proxy",
829 "Connect would have blocked.\n");
830 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, http_canwrite, phb);
831 } else {
832 close(fd);
833 return -1;
835 } else {
836 unsigned int len;
837 int error = ETIMEDOUT;
839 gaim_debug(GAIM_DEBUG_MISC, "http proxy",
840 "Connect didn't block.\n");
842 len = sizeof(error);
843 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
844 close(fd);
845 return -1;
847 fcntl(fd, F_SETFL, 0);
848 http_canwrite(phb, fd, GAIM_INPUT_WRITE);
851 return fd;
854 static void s4_canread(gpointer data, gint source, GaimInputCondition cond)
856 unsigned char packet[12];
857 struct PHB *phb = data;
859 gaim_input_remove(phb->inpa);
861 memset(packet, 0, sizeof(packet));
863 if (read(source, packet, 9) >= 4 && packet[1] == 90) {
864 if(!phb->account || phb->account->gc)
865 phb->func(phb->data, source, GAIM_INPUT_READ);
866 g_free(phb->host);
867 g_free(phb);
868 return;
871 close(source);
872 if(!phb->account || phb->account->gc)
873 phb->func(phb->data, -1, GAIM_INPUT_READ);
874 g_free(phb->host);
875 g_free(phb);
878 static void s4_canwrite(gpointer data, gint source, GaimInputCondition cond)
880 unsigned char packet[12];
881 struct hostent *hp;
882 struct PHB *phb = data;
883 unsigned int len;
884 int error = ETIMEDOUT;
886 gaim_debug(GAIM_DEBUG_INFO, "s4 proxy", "Connected.\n");
888 if (phb->inpa > 0)
889 gaim_input_remove(phb->inpa);
890 len = sizeof(error);
891 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
892 close(source);
893 if(!phb->account || phb->account->gc)
894 phb->func(phb->data, -1, GAIM_INPUT_READ);
895 g_free(phb->host);
896 g_free(phb);
897 return;
899 fcntl(source, F_SETFL, 0);
901 /* XXX does socks4 not support host name lookups by the proxy? */
902 if (!(hp = gethostbyname(phb->host))) {
903 close(source);
904 if(!phb->account || phb->account->gc)
905 phb->func(phb->data, -1, GAIM_INPUT_READ);
906 g_free(phb->host);
907 g_free(phb);
908 return;
911 packet[0] = 4;
912 packet[1] = 1;
913 packet[2] = phb->port >> 8;
914 packet[3] = phb->port & 0xff;
915 packet[4] = (unsigned char)(hp->h_addr_list[0])[0];
916 packet[5] = (unsigned char)(hp->h_addr_list[0])[1];
917 packet[6] = (unsigned char)(hp->h_addr_list[0])[2];
918 packet[7] = (unsigned char)(hp->h_addr_list[0])[3];
919 packet[8] = 0;
921 if (write(source, packet, 9) != 9) {
922 close(source);
923 if(!phb->account || phb->account->gc)
924 phb->func(phb->data, -1, GAIM_INPUT_READ);
925 g_free(phb->host);
926 g_free(phb);
927 return;
930 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s4_canread, phb);
933 static int proxy_connect_socks4(struct PHB *phb, struct sockaddr *addr, socklen_t addrlen)
935 int fd = -1;
937 gaim_debug(GAIM_DEBUG_INFO, "socks4 proxy",
938 "Connecting to %s:%d via %s:%d using SOCKS4\n",
939 phb->host, phb->port, phb->gpi->proxyhost,
940 phb->gpi->proxyport);
942 if ((fd = socket(addr->sa_family, SOCK_STREAM, 0)) < 0) {
943 return -1;
946 fcntl(fd, F_SETFL, O_NONBLOCK);
947 if (connect(fd, addr, addrlen) < 0) {
948 if ((errno == EINPROGRESS) || (errno == EINTR)) {
949 gaim_debug(GAIM_DEBUG_WARNING, "socks4 proxy",
950 "Connect would have blocked.\n");
951 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, s4_canwrite, phb);
952 } else {
953 close(fd);
954 return -1;
956 } else {
957 unsigned int len;
958 int error = ETIMEDOUT;
960 gaim_debug(GAIM_DEBUG_MISC, "socks4 proxy",
961 "Connect didn't block.\n");
963 len = sizeof(error);
964 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
965 close(fd);
966 return -1;
968 fcntl(fd, F_SETFL, 0);
969 s4_canwrite(phb, fd, GAIM_INPUT_WRITE);
972 return fd;
975 static void s5_canread_again(gpointer data, gint source, GaimInputCondition cond)
977 unsigned char buf[512];
978 struct PHB *phb = data;
980 gaim_input_remove(phb->inpa);
981 gaim_debug(GAIM_DEBUG_INFO, "socks5 proxy", "Able to read again.\n");
983 if (read(source, buf, 10) < 10) {
984 gaim_debug(GAIM_DEBUG_WARNING, "socks5 proxy", "or not...\n");
985 close(source);
986 if(!phb->account || phb->account->gc)
987 phb->func(phb->data, -1, GAIM_INPUT_READ);
988 g_free(phb->host);
989 g_free(phb);
990 return;
992 if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
993 gaim_debug(GAIM_DEBUG_ERROR, "socks5 proxy", "Bad data.\n");
994 close(source);
995 if(!phb->account || phb->account->gc)
996 phb->func(phb->data, -1, GAIM_INPUT_READ);
997 g_free(phb->host);
998 g_free(phb);
999 return;
1002 if(!phb->account || phb->account->gc)
1003 phb->func(phb->data, source, GAIM_INPUT_READ);
1004 g_free(phb->host);
1005 g_free(phb);
1006 return;
1009 static void s5_sendconnect(gpointer data, gint source)
1011 unsigned char buf[512];
1012 struct PHB *phb = data;
1013 int hlen = strlen(phb->host);
1015 buf[0] = 0x05;
1016 buf[1] = 0x01; /* CONNECT */
1017 buf[2] = 0x00; /* reserved */
1018 buf[3] = 0x03; /* address type -- host name */
1019 buf[4] = hlen;
1020 memcpy(buf + 5, phb->host, hlen);
1021 buf[5 + strlen(phb->host)] = phb->port >> 8;
1022 buf[5 + strlen(phb->host) + 1] = phb->port & 0xff;
1024 if (write(source, buf, (5 + strlen(phb->host) + 2)) < (5 + strlen(phb->host) + 2)) {
1025 close(source);
1026 if(!phb->account || phb->account->gc)
1027 phb->func(phb->data, -1, GAIM_INPUT_READ);
1028 g_free(phb->host);
1029 g_free(phb);
1030 return;
1033 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread_again, phb);
1036 static void s5_readauth(gpointer data, gint source, GaimInputCondition cond)
1038 unsigned char buf[512];
1039 struct PHB *phb = data;
1041 gaim_input_remove(phb->inpa);
1042 gaim_debug(GAIM_DEBUG_INFO, "socks5 proxy", "Got auth response.\n");
1044 if (read(source, buf, 2) < 2) {
1045 close(source);
1046 if(!phb->account || phb->account->gc)
1047 phb->func(phb->data, -1, GAIM_INPUT_READ);
1048 g_free(phb->host);
1049 g_free(phb);
1050 return;
1053 if ((buf[0] != 0x01) || (buf[1] != 0x00)) {
1054 close(source);
1055 if(!phb->account || phb->account->gc)
1056 phb->func(phb->data, -1, GAIM_INPUT_READ);
1057 g_free(phb->host);
1058 g_free(phb);
1059 return;
1062 s5_sendconnect(phb, source);
1065 static void s5_canread(gpointer data, gint source, GaimInputCondition cond)
1067 unsigned char buf[512];
1068 struct PHB *phb = data;
1070 gaim_input_remove(phb->inpa);
1071 gaim_debug(GAIM_DEBUG_INFO, "socks5 proxy", "Able to read.\n");
1073 if (read(source, buf, 2) < 2) {
1074 close(source);
1075 if(!phb->account || phb->account->gc)
1076 phb->func(phb->data, -1, GAIM_INPUT_READ);
1077 g_free(phb->host);
1078 g_free(phb);
1079 return;
1082 if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
1083 close(source);
1084 if(!phb->account || phb->account->gc)
1085 phb->func(phb->data, -1, GAIM_INPUT_READ);
1086 g_free(phb->host);
1087 g_free(phb);
1088 return;
1091 if (buf[1] == 0x02) {
1092 unsigned int i = strlen(phb->gpi->proxyuser), j = strlen(phb->gpi->proxypass);
1093 buf[0] = 0x01; /* version 1 */
1094 buf[1] = i;
1095 memcpy(buf + 2, phb->gpi->proxyuser, i);
1096 buf[2 + i] = j;
1097 memcpy(buf + 2 + i + 1, phb->gpi->proxypass, j);
1099 if (write(source, buf, 3 + i + j) < 3 + i + j) {
1100 close(source);
1101 if(!phb->account || phb->account->gc)
1102 phb->func(phb->data, -1, GAIM_INPUT_READ);
1103 g_free(phb->host);
1104 g_free(phb);
1105 return;
1108 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_readauth, phb);
1109 } else {
1110 s5_sendconnect(phb, source);
1114 static void s5_canwrite(gpointer data, gint source, GaimInputCondition cond)
1116 unsigned char buf[512];
1117 int i;
1118 struct PHB *phb = data;
1119 unsigned int len;
1120 int error = ETIMEDOUT;
1122 gaim_debug(GAIM_INFO, "socks5 proxy", "Connected.\n");
1124 if (phb->inpa > 0)
1125 gaim_input_remove(phb->inpa);
1126 len = sizeof(error);
1127 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
1128 close(source);
1129 if(!phb->account || phb->account->gc)
1130 phb->func(phb->data, -1, GAIM_INPUT_READ);
1131 g_free(phb->host);
1132 g_free(phb);
1133 return;
1135 fcntl(source, F_SETFL, 0);
1137 i = 0;
1138 buf[0] = 0x05; /* SOCKS version 5 */
1139 if (phb->gpi->proxyuser[0]) {
1140 buf[1] = 0x02; /* two methods */
1141 buf[2] = 0x00; /* no authentication */
1142 buf[3] = 0x02; /* username/password authentication */
1143 i = 4;
1144 } else {
1145 buf[1] = 0x01;
1146 buf[2] = 0x00;
1147 i = 3;
1150 if (write(source, buf, i) < i) {
1151 gaim_debug(GAIM_DEBUG_ERROR, "socks5 proxy", "Unable to write\n");
1152 close(source);
1153 if(!phb->account || phb->account->gc)
1154 phb->func(phb->data, -1, GAIM_INPUT_READ);
1155 g_free(phb->host);
1156 g_free(phb);
1157 return;
1160 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread, phb);
1163 static int proxy_connect_socks5(struct PHB *phb, struct sockaddr *addr, socklen_t addrlen)
1165 int fd = -1;
1167 gaim_debug(GAIM_DEBUG_INFO, "socks5 proxy",
1168 "Connecting to %s:%d via %s:%d using SOCKS5\n",
1169 phb->host, phb->port, phb->gpi->proxyhost,
1170 phb->gpi->proxyport);
1172 if ((fd = socket(addr->sa_family, SOCK_STREAM, 0)) < 0) {
1173 return -1;
1176 fcntl(fd, F_SETFL, O_NONBLOCK);
1177 if (connect(fd, addr, addrlen) < 0) {
1178 if ((errno == EINPROGRESS) || (errno == EINTR)) {
1179 gaim_debug(GAIM_DEBUG_WARNING, "socks5 proxy",
1180 "Connect would have blocked.\n");
1181 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, s5_canwrite, phb);
1182 } else {
1183 close(fd);
1184 return -1;
1186 } else {
1187 unsigned int len;
1188 int error = ETIMEDOUT;
1190 gaim_debug(GAIM_DEBUG_MISC, "socks5 proxy",
1191 "Connect didn't block.\n");
1192 len = sizeof(error);
1193 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
1194 close(fd);
1195 return -1;
1197 fcntl(fd, F_SETFL, 0);
1198 s5_canwrite(phb, fd, GAIM_INPUT_WRITE);
1201 return fd;
1204 static void connection_host_resolved(GSList *hosts, gpointer data, const char *error_message)
1206 struct PHB *phb = (struct PHB*)data;
1207 size_t addrlen;
1208 struct sockaddr *addr;
1209 int ret = -1;
1211 while(hosts) {
1212 addrlen = GPOINTER_TO_INT(hosts->data);
1213 hosts = hosts->next;
1214 addr = hosts->data;
1215 hosts = hosts->next;
1217 switch(phb->gpi->proxytype)
1219 case PROXY_NONE:
1220 ret = proxy_connect_none(phb, addr, addrlen);
1221 break;
1222 case PROXY_HTTP:
1223 ret = proxy_connect_http(phb, addr, addrlen);
1224 break;
1225 case PROXY_SOCKS4:
1226 ret = proxy_connect_socks4(phb, addr, addrlen);
1227 break;
1228 case PROXY_SOCKS5:
1229 ret = proxy_connect_socks5(phb, addr, addrlen);
1230 break;
1232 if (ret > 0)
1233 break;
1235 if(ret < 0) {
1236 if(!phb->account || phb->account->gc)
1237 phb->func(phb->data, -1, GAIM_INPUT_READ);
1238 g_free(phb->host);
1239 g_free(phb);
1244 proxy_connect(struct gaim_account *account, char *host, int port, GaimInputFunction func, gpointer data)
1246 char *connecthost = host;
1247 int connectport = port;
1248 struct PHB *phb = g_new0(struct PHB, 1);
1249 if(!account || !account->gpi)
1250 phb->gpi = &global_proxy_info;
1251 else
1252 phb->gpi = account->gpi;
1253 phb->func = func;
1254 phb->data = data;
1255 phb->host = g_strdup(host);
1256 phb->port = port;
1257 phb->account = account;
1259 if (!host || !port || (port == -1) || !func) {
1260 if(host)
1261 g_free(phb->host);
1262 g_free(phb);
1263 return -1;
1266 if ((phb->gpi->proxytype!=PROXY_NONE) && (!phb->gpi->proxyhost || !phb->gpi->proxyhost[0] || !phb->gpi->proxyport || (phb->gpi->proxyport == -1)))
1267 phb->gpi->proxytype=PROXY_NONE;
1269 switch(phb->gpi->proxytype)
1271 case PROXY_NONE:
1272 break;
1273 case PROXY_HTTP:
1274 case PROXY_SOCKS4:
1275 case PROXY_SOCKS5:
1276 connecthost=phb->gpi->proxyhost;
1277 connectport=phb->gpi->proxyport;
1278 break;
1279 default:
1280 g_free(phb->host);
1281 g_free(phb);
1282 return -1;
1285 return gaim_gethostbyname_async(connecthost, connectport, connection_host_resolved, phb);