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>
36 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
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
;
61 GaimInputFunction func
;
66 struct gaim_proxy_info
*gpi
;
67 struct gaim_account
*account
;
70 typedef struct _GaimIOClosure
{
71 GaimInputFunction function
;
76 static void gaim_io_destroy(gpointer 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
);
102 gint
gaim_input_add(gint source
, GaimInputCondition condition
, GaimInputFunction function
, gpointer data
)
104 GaimIOClosure
*closure
= g_new0(GaimIOClosure
, 1);
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); */
135 g_source_remove(tag
);
139 typedef void (*dns_callback_t
)(GSList
*hosts
, gpointer data
,
140 const char *error_message
);
144 /* This structure represents both a pending DNS request and
145 * a free child process.
150 dns_callback_t callback
;
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;
171 dns_callback_t callback
;
173 } queued_dns_request_t
;
175 static void req_free(pending_dns_request_t
*req
)
177 g_return_if_fail(req
!= NULL
);
183 number_of_dns_children
--;
186 static int send_dns_request_to_child(pending_dns_request_t
*req
, dns_params_t
*dns_params
)
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
);
198 /* Let's contact this lost child! */
199 rc
= write(req
->fd_in
, dns_params
, sizeof(*dns_params
));
201 gaim_debug(GAIM_DEBUG_ERROR
, "dns",
202 "Unable to write to DNS child %d: %d\n",
203 req
->dns_pid
, strerror(errno
));
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",
216 kill(req
->dns_pid
, SIGKILL
);
220 gaim_debug(GAIM_DEBUG_INFO
, "dns",
221 "Successfully sent DNS request to child %d\n", req
->dns_pid
);
225 static void host_resolved(gpointer data
, gint source
, GaimInputCondition cond
);
227 static void release_dns_child(pending_dns_request_t
*req
)
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
;
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) {
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
);
252 req
->inpa
= gaim_input_add(req
->fd_out
, GAIM_INPUT_READ
, host_resolved
, req
);
258 req
->callback
= 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
;
268 GSList
*hosts
= NULL
;
269 struct sockaddr
*addr
= NULL
;
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)) {
278 g_snprintf(message
, sizeof(message
), "DNS error: %s (pid=%d)",
279 #ifdef HAVE_GETADDRINFO
285 gaim_debug(GAIM_DEBUG_ERROR
, "dns", "%s\n", message
);
286 req
->callback(NULL
, req
->data
, message
);
287 release_dns_child(req
);
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
);
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
);
311 g_snprintf(message
, sizeof(message
), "EOF reading from DNS child");
313 gaim_debug(GAIM_DEBUG_ERROR
, "dns", "%s\n", message
);
314 req
->callback(NULL
, req
->data
, message
);
319 /* wait4(req->dns_pid, NULL, WNOHANG, NULL); */
321 req
->callback(hosts
, req
->data
, NULL
);
324 hosts
= g_slist_remove(hosts
, 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
);
348 static void cope_with_gdb_brokenness()
350 static gboolean already_done
= FALSE
;
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");
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
);
388 if(send_dns_request_to_child(req
, &dns_params
) != 0) {
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
;
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
);
414 if(pipe(child_out
) || pipe(child_in
)) {
415 gaim_debug(GAIM_DEBUG_ERROR
, "dns",
416 "Could not create pipes: %s\n", strerror(errno
));
420 /* We need to create a new child. */
421 req
= g_new(pending_dns_request_t
,1);
423 cope_with_gdb_brokenness();
426 if(req
->dns_pid
==0) {
430 #ifdef HAVE_GETADDRINFO
431 struct addrinfo hints
, *res
, *tmp
;
434 struct sockaddr_in sin
;
435 const socklen_t addrlen
= sizeof(sin
);
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
);
451 if(dns_params
.hostname
[0] == '\0') {
454 struct timeval tv
= { .tv_sec
= 40 , .tv_usec
= 0 };
456 FD_SET(child_in
[0], &fds
);
457 rc
= select(child_in
[0]+1, &fds
, NULL
, NULL
, &tv
);
460 fprintf(stderr
,"dns[%d]: nobody needs me... =(\n", getpid());
463 rc
= read(child_in
[0], &dns_params
, sizeof(dns_params
));
470 fprintf(stderr
,"dns[%d]: Ops, father has gone, wait for me, wait...!\n", getpid());
473 if(dns_params
.hostname
[0] == '\0') {
474 fprintf(stderr
, "dns[%d]: hostname = \"\" (port = %d)!!!\n", getpid(), dns_params
.port
);
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
490 hints
.ai_socktype
= SOCK_STREAM
;
491 rc
= getaddrinfo(dns_params
.hostname
, servname
, &hints
, &res
);
493 write(child_out
[1], &rc
, sizeof(int));
496 fprintf(stderr
,"dns[%d] Error: getaddrinfo returned %d\n",
498 dns_params
.hostname
[0] = '\0';
501 write(child_out
[1], &zero
, sizeof(zero
));
504 write(child_out
[1], &(res
->ai_addrlen
), sizeof(res
->ai_addrlen
));
505 write(child_out
[1], res
->ai_addr
, res
->ai_addrlen
);
509 write(child_out
[1], &zero
, sizeof(zero
));
511 if (!inet_aton(hostname
, &sin
.sin_addr
)) {
513 if(!(hp
= gethostbyname(dns_params
.hostname
))) {
514 write(child_out
[1], &h_errno
, sizeof(int));
517 fprintf(stderr
,"DNS Error: %s\n",hstrerror(h_errno
));
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
;
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
));
531 dns_params
.hostname
[0] = '\0';
539 if(req
->dns_pid
==-1) {
540 gaim_debug(GAIM_DEBUG_ERROR
, "dns",
541 "Could not create child process for DNS: %s\n",
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
);
555 req
->callback
=callback
;
557 req
->inpa
= gaim_input_add(req
->fd_out
, GAIM_INPUT_READ
, host_resolved
, req
);
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
);
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
)) {
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
));
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
;
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
);
606 req
->callback
= callback
;
607 g_timeout_add(10, host_resolved
, req
);
613 static void no_one_calls(gpointer data
, gint source
, GaimInputCondition cond
)
615 struct PHB
*phb
= data
;
620 gaim_debug(GAIM_DEBUG_INFO
, "proxy", "Connected.\n");
624 ret
= getsockopt(source
, SOL_SOCKET
, SO_ERROR
, &error
, &len
);
625 if (ret
< 0 || error
!= 0) {
626 if(ret
==0) errno
= error
;
628 gaim_input_remove(phb
->inpa
);
629 if(!phb
->account
|| phb
->account
->gc
)
630 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
634 gaim_debug(GAIM_DEBUG_ERROR
, "proxy",
635 "getsockopt SO_ERROR check: %s\n", strerror(errno
));
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
);
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
);
659 static int proxy_connect_none(struct PHB
*phb
, struct sockaddr
*addr
, socklen_t addrlen
)
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
));
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
);
679 gaim_debug(GAIM_DEBUG_ERROR
, "proxy",
680 "Connect failed (errno %d)\n", errno
);
686 int error
= ETIMEDOUT
;
687 gaim_debug(GAIM_DEBUG_MISC
, "proxy", "Connect didn't block.\n");
689 if (getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
) < 0) {
690 gaim_debug(GAIM_DEBUG_ERROR
, "proxy", "getsockopt failed.\n");
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
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
)
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')
720 else if (inputline
[pos
- 1] != '\r')
723 inputline
[pos
] = '\0';
725 error
= strncmp(inputline
, "HTTP/", 5) != 0;
728 major
= strtol(p
, &p
, 10);
729 error
= (major
==0) || (*p
!= '.');
732 minor
= strtol(p
, &p
, 10);
736 status
= strtol(p
, &p
, 10);
743 gaim_debug(GAIM_DEBUG_ERROR
, "proxy",
744 "Unable to parse proxy's response: %s\n", inputline
);
747 } else if(status
!=200) {
748 gaim_debug(GAIM_DEBUG_ERROR
, "proxy",
749 "Proxy server replied: (%s)\n", p
);
754 if(!phb
->account
|| phb
->account
->gc
)
755 phb
->func(phb
->data
, source
, GAIM_INPUT_READ
);
761 static void http_canwrite(gpointer data
, gint source
, GaimInputCondition cond
)
765 struct PHB
*phb
= data
;
767 int error
= ETIMEDOUT
;
769 gaim_debug(GAIM_DEBUG_INFO
, "http proxy", "Connected.\n");
772 gaim_input_remove(phb
->inpa
);
774 if (getsockopt(source
, SOL_SOCKET
, SO_ERROR
, &error
, &len
) < 0) {
776 if(!phb
->account
|| phb
->account
->gc
)
777 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
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
) {
787 t1
= g_strdup_printf("%s:%s", phb
->gpi
->proxyuser
, phb
->gpi
->proxypass
);
788 t2
= tobase64(t1
, -1);
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
);
795 g_return_if_fail(request_len
< sizeof(request
));
796 strcpy(request
+ request_len
, "\r\n");
799 if (write(source
, request
, request_len
) < 0) {
801 if(!phb
->account
|| phb
->account
->gc
)
802 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
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
)
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) {
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
);
837 int error
= ETIMEDOUT
;
839 gaim_debug(GAIM_DEBUG_MISC
, "http proxy",
840 "Connect didn't block.\n");
843 if (getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
) < 0) {
847 fcntl(fd
, F_SETFL
, 0);
848 http_canwrite(phb
, fd
, GAIM_INPUT_WRITE
);
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
);
872 if(!phb
->account
|| phb
->account
->gc
)
873 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
878 static void s4_canwrite(gpointer data
, gint source
, GaimInputCondition cond
)
880 unsigned char packet
[12];
882 struct PHB
*phb
= data
;
884 int error
= ETIMEDOUT
;
886 gaim_debug(GAIM_DEBUG_INFO
, "s4 proxy", "Connected.\n");
889 gaim_input_remove(phb
->inpa
);
891 if (getsockopt(source
, SOL_SOCKET
, SO_ERROR
, &error
, &len
) < 0) {
893 if(!phb
->account
|| phb
->account
->gc
)
894 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
899 fcntl(source
, F_SETFL
, 0);
901 /* XXX does socks4 not support host name lookups by the proxy? */
902 if (!(hp
= gethostbyname(phb
->host
))) {
904 if(!phb
->account
|| phb
->account
->gc
)
905 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
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];
921 if (write(source
, packet
, 9) != 9) {
923 if(!phb
->account
|| phb
->account
->gc
)
924 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
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
)
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) {
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
);
958 int error
= ETIMEDOUT
;
960 gaim_debug(GAIM_DEBUG_MISC
, "socks4 proxy",
961 "Connect didn't block.\n");
964 if (getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
) < 0) {
968 fcntl(fd
, F_SETFL
, 0);
969 s4_canwrite(phb
, fd
, GAIM_INPUT_WRITE
);
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");
986 if(!phb
->account
|| phb
->account
->gc
)
987 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
992 if ((buf
[0] != 0x05) || (buf
[1] != 0x00)) {
993 gaim_debug(GAIM_DEBUG_ERROR
, "socks5 proxy", "Bad data.\n");
995 if(!phb
->account
|| phb
->account
->gc
)
996 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
1002 if(!phb
->account
|| phb
->account
->gc
)
1003 phb
->func(phb
->data
, source
, GAIM_INPUT_READ
);
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
);
1016 buf
[1] = 0x01; /* CONNECT */
1017 buf
[2] = 0x00; /* reserved */
1018 buf
[3] = 0x03; /* address type -- host name */
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)) {
1026 if(!phb
->account
|| phb
->account
->gc
)
1027 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
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) {
1046 if(!phb
->account
|| phb
->account
->gc
)
1047 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
1053 if ((buf
[0] != 0x01) || (buf
[1] != 0x00)) {
1055 if(!phb
->account
|| phb
->account
->gc
)
1056 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
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) {
1075 if(!phb
->account
|| phb
->account
->gc
)
1076 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
1082 if ((buf
[0] != 0x05) || (buf
[1] == 0xff)) {
1084 if(!phb
->account
|| phb
->account
->gc
)
1085 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
1091 if (buf
[1] == 0x02) {
1092 unsigned int i
= strlen(phb
->gpi
->proxyuser
), j
= strlen(phb
->gpi
->proxypass
);
1093 buf
[0] = 0x01; /* version 1 */
1095 memcpy(buf
+ 2, phb
->gpi
->proxyuser
, i
);
1097 memcpy(buf
+ 2 + i
+ 1, phb
->gpi
->proxypass
, j
);
1099 if (write(source
, buf
, 3 + i
+ j
) < 3 + i
+ j
) {
1101 if(!phb
->account
|| phb
->account
->gc
)
1102 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
1108 phb
->inpa
= gaim_input_add(source
, GAIM_INPUT_READ
, s5_readauth
, phb
);
1110 s5_sendconnect(phb
, source
);
1114 static void s5_canwrite(gpointer data
, gint source
, GaimInputCondition cond
)
1116 unsigned char buf
[512];
1118 struct PHB
*phb
= data
;
1120 int error
= ETIMEDOUT
;
1122 gaim_debug(GAIM_INFO
, "socks5 proxy", "Connected.\n");
1125 gaim_input_remove(phb
->inpa
);
1126 len
= sizeof(error
);
1127 if (getsockopt(source
, SOL_SOCKET
, SO_ERROR
, &error
, &len
) < 0) {
1129 if(!phb
->account
|| phb
->account
->gc
)
1130 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
1135 fcntl(source
, F_SETFL
, 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 */
1150 if (write(source
, buf
, i
) < i
) {
1151 gaim_debug(GAIM_DEBUG_ERROR
, "socks5 proxy", "Unable to write\n");
1153 if(!phb
->account
|| phb
->account
->gc
)
1154 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
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
)
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) {
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
);
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) {
1197 fcntl(fd
, F_SETFL
, 0);
1198 s5_canwrite(phb
, fd
, GAIM_INPUT_WRITE
);
1204 static void connection_host_resolved(GSList
*hosts
, gpointer data
, const char *error_message
)
1206 struct PHB
*phb
= (struct PHB
*)data
;
1208 struct sockaddr
*addr
;
1212 addrlen
= GPOINTER_TO_INT(hosts
->data
);
1213 hosts
= hosts
->next
;
1215 hosts
= hosts
->next
;
1217 switch(phb
->gpi
->proxytype
)
1220 ret
= proxy_connect_none(phb
, addr
, addrlen
);
1223 ret
= proxy_connect_http(phb
, addr
, addrlen
);
1226 ret
= proxy_connect_socks4(phb
, addr
, addrlen
);
1229 ret
= proxy_connect_socks5(phb
, addr
, addrlen
);
1236 if(!phb
->account
|| phb
->account
->gc
)
1237 phb
->func(phb
->data
, -1, GAIM_INPUT_READ
);
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
;
1252 phb
->gpi
= account
->gpi
;
1255 phb
->host
= g_strdup(host
);
1257 phb
->account
= account
;
1259 if (!host
|| !port
|| (port
== -1) || !func
) {
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
)
1276 connecthost
=phb
->gpi
->proxyhost
;
1277 connectport
=phb
->gpi
->proxyport
;
1285 return gaim_gethostbyname_async(connecthost
, connectport
, connection_host_resolved
, phb
);