annotate src/proxy.c @ 2374:9e9385cddcc6

[gaim-migrate @ 2387] i need to go to class. but just because i need to do something doesn't mean i will, especially when what i need to do is related to school. committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Thu, 27 Sep 2001 20:32:59 +0000
parents 2927c2c26fe6
children 8530d4d72fab
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
1 /*
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
2 * gaim
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
3 *
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
5 *
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
9 * (at your option) any later version.
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
10 *
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
11 * This program is distributed in the hope that it will be useful,
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
14 * GNU General Public License for more details.
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
15 *
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
17 * along with this program; if not, write to the Free Software
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
19 *
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
20 */
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
21
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
22 /* this is a little piece of code to handle proxy connection */
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
23 /* it is intended to : 1st handle http proxy, using the CONNECT command
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
24 , 2nd provide an easy way to add socks support */
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
25
349
b402a23f35df [gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 338
diff changeset
26 #ifdef HAVE_CONFIG_H
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
27 #include "config.h"
349
b402a23f35df [gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 338
diff changeset
28 #endif
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
29 #include <stdio.h>
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
30 #include <stdlib.h>
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
31 #include <string.h>
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
32 #include <sys/types.h>
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
33 #include <sys/socket.h>
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
34 #include <netdb.h>
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
35 #include <netinet/in.h>
1057
d50d3abb9eb7 [gaim-migrate @ 1067]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 743
diff changeset
36 #include <unistd.h>
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
37 #include <fcntl.h>
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
38 #include <errno.h>
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
39 #include "gaim.h"
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
40 #include "proxy.h"
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
41
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
42 #define GAIM_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
2229
b5ad79ad917b [gaim-migrate @ 2239]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2096
diff changeset
43 #define GAIM_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
44
1881
a02584b98823 [gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1868
diff changeset
45 char proxyhost[128] = { 0 };
2372
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
46 int proxyport = 0;
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
47 int proxytype = 0;
1881
a02584b98823 [gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1868
diff changeset
48 char proxyuser[128] = { 0 };
a02584b98823 [gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1868
diff changeset
49 char proxypass[128] = { 0 };
a02584b98823 [gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1868
diff changeset
50
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
51 struct PHB {
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
52 GaimInputFunction func;
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
53 gpointer data;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
54 char *host;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
55 int port;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
56 gint inpa;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
57 };
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
58
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
59 typedef struct _GaimIOClosure {
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
60 GaimInputFunction function;
2300
d2686f757d6e [gaim-migrate @ 2310]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2266
diff changeset
61 guint result;
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
62 gpointer data;
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
63 } GaimIOClosure;
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
64
2096
39b4413ce4b3 [gaim-migrate @ 2106]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2090
diff changeset
65 static void gaim_io_destroy(gpointer data)
39b4413ce4b3 [gaim-migrate @ 2106]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2090
diff changeset
66 {
39b4413ce4b3 [gaim-migrate @ 2106]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2090
diff changeset
67 g_free(data);
39b4413ce4b3 [gaim-migrate @ 2106]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2090
diff changeset
68 }
39b4413ce4b3 [gaim-migrate @ 2106]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2090
diff changeset
69
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
70 static gboolean gaim_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data)
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
71 {
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
72 GaimIOClosure *closure = data;
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
73 GaimInputCondition gaim_cond = 0;
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
74
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
75 if (condition & GAIM_READ_COND)
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
76 gaim_cond |= GAIM_INPUT_READ;
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
77 if (condition & GAIM_READ_COND)
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
78 gaim_cond |= GAIM_INPUT_WRITE;
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
79
2300
d2686f757d6e [gaim-migrate @ 2310]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2266
diff changeset
80 debug_printf("CLOSURE: callback for %d, fd is %d\n",
2372
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
81 closure->result, g_io_channel_unix_get_fd(source));
2300
d2686f757d6e [gaim-migrate @ 2310]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2266
diff changeset
82
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
83 closure->function(closure->data, g_io_channel_unix_get_fd(source), gaim_cond);
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
84
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
85 return TRUE;
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
86 }
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
87
2372
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
88 gint gaim_input_add(gint source, GaimInputCondition condition, GaimInputFunction function, gpointer data)
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
89 {
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
90 GaimIOClosure *closure = g_new0(GaimIOClosure, 1);
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
91 GIOChannel *channel;
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
92 GIOCondition cond = 0;
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
93
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
94 closure->function = function;
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
95 closure->data = data;
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
96
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
97 if (condition & GAIM_INPUT_READ)
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
98 cond |= GAIM_READ_COND;
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
99 if (condition & GAIM_INPUT_WRITE)
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
100 cond |= GAIM_WRITE_COND;
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
101
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
102 channel = g_io_channel_unix_new(source);
2300
d2686f757d6e [gaim-migrate @ 2310]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2266
diff changeset
103 closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond,
2372
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
104 gaim_io_invoke, closure, gaim_io_destroy);
2300
d2686f757d6e [gaim-migrate @ 2310]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2266
diff changeset
105
d2686f757d6e [gaim-migrate @ 2310]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2266
diff changeset
106 debug_printf("CLOSURE: adding input watcher %d for fd %d\n", closure->result, source);
d2686f757d6e [gaim-migrate @ 2310]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2266
diff changeset
107
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
108 g_io_channel_unref(channel);
2300
d2686f757d6e [gaim-migrate @ 2310]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2266
diff changeset
109 return closure->result;
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
110 }
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
111
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
112 void gaim_input_remove(gint tag)
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
113 {
2300
d2686f757d6e [gaim-migrate @ 2310]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2266
diff changeset
114 debug_printf("CLOSURE: removing input watcher %d\n", tag);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
115 g_source_remove(tag);
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
116 }
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
117
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
118 static void no_one_calls(gpointer data, gint source, GaimInputCondition cond)
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
119 {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
120 struct PHB *phb = data;
2048
3366311426df [gaim-migrate @ 2058]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1881
diff changeset
121 unsigned int len;
3366311426df [gaim-migrate @ 2058]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1881
diff changeset
122 int error = ETIMEDOUT;
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
123 debug_printf("Connected\n");
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
124 len = sizeof(error);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
125 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
126 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
127 gaim_input_remove(phb->inpa);
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
128 phb->func(phb->data, -1, GAIM_INPUT_READ);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
129 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
130 return;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
131 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
132 fcntl(source, F_SETFL, 0);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
133 gaim_input_remove(phb->inpa);
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
134 phb->func(phb->data, source, GAIM_INPUT_READ);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
135 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
136 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
137
1868
a49ed23b3c02 [gaim-migrate @ 1878]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1844
diff changeset
138 static gboolean clean_connect(gpointer data)
a49ed23b3c02 [gaim-migrate @ 1878]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1844
diff changeset
139 {
a49ed23b3c02 [gaim-migrate @ 1878]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1844
diff changeset
140 struct PHB *phb = data;
a49ed23b3c02 [gaim-migrate @ 1878]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1844
diff changeset
141
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
142 phb->func(phb->data, phb->port, GAIM_INPUT_READ);
1868
a49ed23b3c02 [gaim-migrate @ 1878]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1844
diff changeset
143 g_free(phb);
a49ed23b3c02 [gaim-migrate @ 1878]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1844
diff changeset
144
a49ed23b3c02 [gaim-migrate @ 1878]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1844
diff changeset
145 return FALSE;
a49ed23b3c02 [gaim-migrate @ 1878]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1844
diff changeset
146 }
a49ed23b3c02 [gaim-migrate @ 1878]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1844
diff changeset
147
a49ed23b3c02 [gaim-migrate @ 1878]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1844
diff changeset
148
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
149 static int proxy_connect_none(char *host, unsigned short port, struct PHB *phb)
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
150 {
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
151 struct sockaddr_in sin;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
152 struct hostent *hp;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
153 int fd = -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
154
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
155 debug_printf("connecting to %s:%d with no proxy\n", host, port);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
156
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
157 if (!(hp = gethostbyname(host))) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
158 g_free(phb);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
159 return -1;
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
160 }
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
161
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
162 memset(&sin, 0, sizeof(struct sockaddr_in));
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
163 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
164 sin.sin_family = hp->h_addrtype;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
165 sin.sin_port = htons(port);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
166
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
167 if ((fd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
168 g_free(phb);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
169 return -1;
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
170 }
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
171
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
172 fcntl(fd, F_SETFL, O_NONBLOCK);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
173 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
174 if ((errno == EINPROGRESS) || (errno == EINTR)) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
175 debug_printf("Connect would have blocked\n");
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
176 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, no_one_calls, phb);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
177 } else {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
178 close(fd);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
179 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
180 return -1;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
181 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
182 } else {
2048
3366311426df [gaim-migrate @ 2058]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1881
diff changeset
183 unsigned int len;
3366311426df [gaim-migrate @ 2058]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1881
diff changeset
184 int error = ETIMEDOUT;
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
185 debug_printf("Connect didn't block\n");
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
186 len = sizeof(error);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
187 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
188 close(fd);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
189 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
190 return -1;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
191 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
192 fcntl(fd, F_SETFL, 0);
2372
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
193 phb->port = fd; /* bleh */
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
194 g_timeout_add(50, clean_connect, phb); /* we do this because we never
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
195 want to call our callback
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
196 before we return. */
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
197 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
198
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
199 return fd;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
200 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
201
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
202 #define HTTP_GOODSTRING "HTTP/1.0 200 Connection established"
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
203 #define HTTP_GOODSTRING2 "HTTP/1.1 200 Connection established"
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
204
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
205 static void http_canread(gpointer data, gint source, GaimInputCondition cond)
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
206 {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
207 int nlc = 0;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
208 int pos = 0;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
209 struct PHB *phb = data;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
210 char inputline[8192];
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
211
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
212 gaim_input_remove(phb->inpa);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
213
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
214 while ((nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) {
2372
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
215 if (inputline[pos - 1] == '\n')
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
216 nlc++;
2372
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
217 else if (inputline[pos - 1] != '\r')
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
218 nlc = 0;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
219 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
220 inputline[pos] = '\0';
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
221
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
222 debug_printf("Proxy says: %s\n", inputline);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
223
2372
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
224 if ((memcmp(HTTP_GOODSTRING, inputline, strlen(HTTP_GOODSTRING)) == 0) ||
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
225 (memcmp(HTTP_GOODSTRING2, inputline, strlen(HTTP_GOODSTRING2)) == 0)) {
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
226 phb->func(phb->data, source, GAIM_INPUT_READ);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
227 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
228 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
229 return;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
230 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
231
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
232 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
233 phb->func(phb->data, -1, GAIM_INPUT_READ);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
234 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
235 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
236 return;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
237 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
238
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
239 static void http_canwrite(gpointer data, gint source, GaimInputCondition cond)
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
240 {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
241 char cmd[384];
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
242 struct PHB *phb = data;
2048
3366311426df [gaim-migrate @ 2058]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1881
diff changeset
243 unsigned int len;
3366311426df [gaim-migrate @ 2058]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1881
diff changeset
244 int error = ETIMEDOUT;
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
245 debug_printf("Connected\n");
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
246 if (phb->inpa > 0)
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
247 gaim_input_remove(phb->inpa);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
248 len = sizeof(error);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
249 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
250 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
251 phb->func(phb->data, -1, GAIM_INPUT_READ);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
252 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
253 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
254 return;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
255 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
256 fcntl(source, F_SETFL, 0);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
257
2372
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
258 g_snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\r\nHost = %s:%d\r\n", phb->host, phb->port,
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
259 phb->host, phb->port);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
260 if (send(source, cmd, strlen(cmd), 0) < 0) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
261 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
262 phb->func(phb->data, -1, GAIM_INPUT_READ);
1843
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
263 g_free(phb->host);
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
264 g_free(phb);
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
265 return;
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
266 }
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
267
1881
a02584b98823 [gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1868
diff changeset
268 if (proxyuser) {
1843
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
269 char *t1, *t2;
1881
a02584b98823 [gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1868
diff changeset
270 t1 = g_strdup_printf("%s:%s", proxyuser, proxypass);
1843
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
271 t2 = tobase64(t1);
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
272 g_free(t1);
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
273 g_snprintf(cmd, sizeof(cmd), "Proxy-Authorization: Basic %s\r\n", t2);
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
274 g_free(t2);
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
275 if (send(source, cmd, strlen(cmd), 0) < 0) {
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
276 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
277 phb->func(phb->data, -1, GAIM_INPUT_READ);
1843
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
278 g_free(phb->host);
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
279 g_free(phb);
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
280 return;
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
281 }
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
282 }
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
283
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
284 g_snprintf(cmd, sizeof(cmd), "\r\n");
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
285 if (send(source, cmd, strlen(cmd), 0) < 0) {
4dbd8533d209 [gaim-migrate @ 1853]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1840
diff changeset
286 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
287 phb->func(phb->data, -1, GAIM_INPUT_READ);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
288 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
289 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
290 return;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
291 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
292
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
293 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, http_canread, phb);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
294 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
295
1881
a02584b98823 [gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1868
diff changeset
296 static int proxy_connect_http(char *host, unsigned short port, struct PHB *phb)
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
297 {
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
298 struct hostent *hp;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
299 struct sockaddr_in sin;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
300 int fd = -1;
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
301
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
302 debug_printf("connecting to %s:%d via %s:%d using HTTP\n", host, port, proxyhost, proxyport);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
303
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
304 if (!(hp = gethostbyname(proxyhost))) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
305 g_free(phb);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
306 return -1;
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
307 }
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
308
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
309 memset(&sin, 0, sizeof(struct sockaddr_in));
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
310 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
311 sin.sin_family = hp->h_addrtype;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
312 sin.sin_port = htons(proxyport);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
313
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
314 if ((fd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
315 g_free(phb);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
316 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
317 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
318
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
319 phb->host = g_strdup(host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
320 phb->port = port;
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
321
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
322 fcntl(fd, F_SETFL, O_NONBLOCK);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
323 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
324 if ((errno == EINPROGRESS) || (errno == EINTR)) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
325 debug_printf("Connect would have blocked\n");
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
326 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, http_canwrite, phb);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
327 } else {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
328 close(fd);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
329 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
330 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
331 return -1;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
332 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
333 } else {
2048
3366311426df [gaim-migrate @ 2058]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1881
diff changeset
334 unsigned int len;
3366311426df [gaim-migrate @ 2058]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1881
diff changeset
335 int error = ETIMEDOUT;
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
336 debug_printf("Connect didn't block\n");
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
337 len = sizeof(error);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
338 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
339 close(fd);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
340 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
341 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
342 return -1;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
343 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
344 fcntl(fd, F_SETFL, 0);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
345 http_canwrite(phb, fd, GAIM_INPUT_WRITE);
1695
c0ab844d4790 [gaim-migrate @ 1705]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1451
diff changeset
346 }
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
347
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
348 return fd;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
349 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
350
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
351 static void s4_canread(gpointer data, gint source, GaimInputCondition cond)
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
352 {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
353 unsigned char packet[12];
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
354 struct PHB *phb = data;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
355
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
356 gaim_input_remove(phb->inpa);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
357
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
358 memset(packet, 0, sizeof(packet));
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
359 if (read(source, packet, 9) >= 4 && packet[1] == 90) {
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
360 phb->func(phb->data, source, GAIM_INPUT_READ);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
361 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
362 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
363 return;
1695
c0ab844d4790 [gaim-migrate @ 1705]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1451
diff changeset
364 }
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
365
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
366 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
367 phb->func(phb->data, -1, GAIM_INPUT_READ);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
368 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
369 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
370 }
1705
b4d454e5ee31 [gaim-migrate @ 1715]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1695
diff changeset
371
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
372 static void s4_canwrite(gpointer data, gint source, GaimInputCondition cond)
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
373 {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
374 unsigned char packet[12];
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
375 struct hostent *hp;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
376 struct PHB *phb = data;
2048
3366311426df [gaim-migrate @ 2058]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1881
diff changeset
377 unsigned int len;
3366311426df [gaim-migrate @ 2058]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1881
diff changeset
378 int error = ETIMEDOUT;
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
379 debug_printf("Connected\n");
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
380 if (phb->inpa > 0)
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
381 gaim_input_remove(phb->inpa);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
382 len = sizeof(error);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
383 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
384 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
385 phb->func(phb->data, -1, GAIM_INPUT_READ);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
386 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
387 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
388 return;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
389 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
390 fcntl(source, F_SETFL, 0);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
391
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
392 /* XXX does socks4 not support host name lookups by the proxy? */
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
393 if (!(hp = gethostbyname(phb->host))) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
394 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
395 phb->func(phb->data, -1, GAIM_INPUT_READ);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
396 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
397 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
398 return;
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
399 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
400
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
401 packet[0] = 4;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
402 packet[1] = 1;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
403 packet[2] = phb->port >> 8;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
404 packet[3] = phb->port & 0xff;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
405 packet[4] = (unsigned char)(hp->h_addr_list[0])[0];
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
406 packet[5] = (unsigned char)(hp->h_addr_list[0])[1];
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
407 packet[6] = (unsigned char)(hp->h_addr_list[0])[2];
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
408 packet[7] = (unsigned char)(hp->h_addr_list[0])[3];
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
409 packet[8] = 0;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
410 if (write(source, packet, 9) != 9) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
411 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
412 phb->func(phb->data, -1, GAIM_INPUT_READ);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
413 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
414 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
415 return;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
416 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
417
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
418 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s4_canread, phb);
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
419 }
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
420
1881
a02584b98823 [gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1868
diff changeset
421 static int proxy_connect_socks4(char *host, unsigned short port, struct PHB *phb)
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
422 {
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
423 struct sockaddr_in sin;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
424 struct hostent *hp;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
425 int fd = -1;
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
426
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
427 debug_printf("connecting to %s:%d via %s:%d using SOCKS4\n", host, port, proxyhost, proxyport);
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
428
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
429 if (!(hp = gethostbyname(proxyhost))) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
430 g_free(phb);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
431 return -1;
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
432 }
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
433
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
434 memset(&sin, 0, sizeof(struct sockaddr_in));
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
435 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
436 sin.sin_family = hp->h_addrtype;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
437 sin.sin_port = htons(proxyport);
664
5cb19972df52 [gaim-migrate @ 674]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 349
diff changeset
438
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
439 if ((fd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
440 g_free(phb);
664
5cb19972df52 [gaim-migrate @ 674]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 349
diff changeset
441 return -1;
5cb19972df52 [gaim-migrate @ 674]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 349
diff changeset
442 }
670
9e51c1a4abba [gaim-migrate @ 680]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 664
diff changeset
443
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
444 phb->host = g_strdup(host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
445 phb->port = port;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
446
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
447 fcntl(fd, F_SETFL, O_NONBLOCK);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
448 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
449 if ((errno == EINPROGRESS) || (errno == EINTR)) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
450 debug_printf("Connect would have blocked\n");
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
451 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, s4_canwrite, phb);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
452 } else {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
453 close(fd);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
454 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
455 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
456 return -1;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
457 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
458 } else {
2048
3366311426df [gaim-migrate @ 2058]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1881
diff changeset
459 unsigned int len;
3366311426df [gaim-migrate @ 2058]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1881
diff changeset
460 int error = ETIMEDOUT;
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
461 debug_printf("Connect didn't block\n");
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
462 len = sizeof(error);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
463 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
464 close(fd);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
465 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
466 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
467 return -1;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
468 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
469 fcntl(fd, F_SETFL, 0);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
470 s4_canwrite(phb, fd, GAIM_INPUT_WRITE);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
471 }
670
9e51c1a4abba [gaim-migrate @ 680]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 664
diff changeset
472
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
473 return fd;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
474 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
475
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
476 static void s5_canread_again(gpointer data, gint source, GaimInputCondition cond)
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
477 {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
478 unsigned char buf[512];
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
479 struct PHB *phb = data;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
480
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
481 gaim_input_remove(phb->inpa);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
482 debug_printf("able to read again\n");
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
483
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
484 if (read(source, buf, 10) < 10) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
485 debug_printf("or not...\n");
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
486 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
487 phb->func(phb->data, -1, GAIM_INPUT_READ);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
488 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
489 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
490 return;
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
491 }
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
492 if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
493 debug_printf("bad data\n");
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
494 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
495 phb->func(phb->data, -1, GAIM_INPUT_READ);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
496 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
497 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
498 return;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
499 }
670
9e51c1a4abba [gaim-migrate @ 680]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 664
diff changeset
500
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
501 phb->func(phb->data, source, GAIM_INPUT_READ);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
502 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
503 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
504 return;
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
505 }
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
506
1844
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
507 static void s5_sendconnect(gpointer data, gint source)
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
508 {
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
509 unsigned char buf[512];
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
510 struct PHB *phb = data;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
511 int hlen = strlen(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
512
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
513 buf[0] = 0x05;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
514 buf[1] = 0x01; /* CONNECT */
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
515 buf[2] = 0x00; /* reserved */
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
516 buf[3] = 0x03; /* address type -- host name */
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
517 buf[4] = hlen;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
518 memcpy(buf + 5, phb->host, hlen);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
519 buf[5 + strlen(phb->host)] = phb->port >> 8;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
520 buf[5 + strlen(phb->host) + 1] = phb->port & 0xff;
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
521
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
522 if (write(source, buf, (5 + strlen(phb->host) + 2)) < (5 + strlen(phb->host) + 2)) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
523 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
524 phb->func(phb->data, -1, GAIM_INPUT_READ);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
525 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
526 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
527 return;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
528 }
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
529
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
530 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread_again, phb);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
531 }
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
532
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
533 static void s5_readauth(gpointer data, gint source, GaimInputCondition cond)
1844
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
534 {
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
535 unsigned char buf[512];
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
536 struct PHB *phb = data;
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
537
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
538 gaim_input_remove(phb->inpa);
1844
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
539 debug_printf("got auth response\n");
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
540
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
541 if (read(source, buf, 2) < 2) {
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
542 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
543 phb->func(phb->data, -1, GAIM_INPUT_READ);
1844
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
544 g_free(phb->host);
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
545 g_free(phb);
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
546 return;
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
547 }
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
548
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
549 if ((buf[0] != 0x01) || (buf[1] == 0x00)) {
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
550 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
551 phb->func(phb->data, -1, GAIM_INPUT_READ);
1844
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
552 g_free(phb->host);
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
553 g_free(phb);
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
554 return;
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
555 }
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
556
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
557 s5_sendconnect(phb, source);
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
558 }
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
559
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
560 static void s5_canread(gpointer data, gint source, GaimInputCondition cond)
1844
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
561 {
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
562 unsigned char buf[512];
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
563 struct PHB *phb = data;
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
564
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
565 gaim_input_remove(phb->inpa);
1844
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
566 debug_printf("able to read\n");
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
567
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
568 if (read(source, buf, 2) < 2) {
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
569 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
570 phb->func(phb->data, -1, GAIM_INPUT_READ);
1844
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
571 g_free(phb->host);
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
572 g_free(phb);
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
573 return;
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
574 }
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
575
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
576 if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
577 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
578 phb->func(phb->data, -1, GAIM_INPUT_READ);
1844
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
579 g_free(phb->host);
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
580 g_free(phb);
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
581 return;
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
582 }
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
583
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
584 if (buf[1] == 0x02) {
1881
a02584b98823 [gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1868
diff changeset
585 unsigned int i = strlen(proxyuser), j = strlen(proxypass);
2372
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
586 buf[0] = 0x01; /* version 1 */
1844
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
587 buf[1] = i;
2372
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
588 memcpy(buf + 2, proxyuser, i);
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
589 buf[2 + i] = j;
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
590 memcpy(buf + 2 + i + 1, proxypass, j);
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
591 if (write(source, buf, 3 + i + j) < 3 + i + j) {
1844
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
592 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
593 phb->func(phb->data, -1, GAIM_INPUT_READ);
1844
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
594 g_free(phb->host);
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
595 g_free(phb);
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
596 return;
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
597 }
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
598
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
599 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_readauth, phb);
1844
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
600 } else {
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
601 s5_sendconnect(phb, source);
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
602 }
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
603 }
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
604
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
605 static void s5_canwrite(gpointer data, gint source, GaimInputCondition cond)
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
606 {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
607 unsigned char buf[512];
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
608 int i;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
609 struct PHB *phb = data;
2048
3366311426df [gaim-migrate @ 2058]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1881
diff changeset
610 unsigned int len;
3366311426df [gaim-migrate @ 2058]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1881
diff changeset
611 int error = ETIMEDOUT;
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
612 debug_printf("Connected\n");
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
613 if (phb->inpa > 0)
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
614 gaim_input_remove(phb->inpa);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
615 len = sizeof(error);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
616 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
617 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
618 phb->func(phb->data, -1, GAIM_INPUT_READ);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
619 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
620 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
621 return;
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
622 }
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
623 fcntl(source, F_SETFL, 0);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
624
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
625 i = 0;
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
626 buf[0] = 0x05; /* SOCKS version 5 */
1881
a02584b98823 [gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1868
diff changeset
627 if (proxyuser[0]) {
1844
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
628 buf[1] = 0x02; /* two methods */
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
629 buf[2] = 0x00; /* no authentication */
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
630 buf[3] = 0x02; /* username/password authentication */
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
631 i = 4;
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
632 } else {
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
633 buf[1] = 0x01;
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
634 buf[2] = 0x00;
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
635 i = 3;
9845deede1e9 [gaim-migrate @ 1854]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1843
diff changeset
636 }
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
637
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
638 if (write(source, buf, i) < i) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
639 debug_printf("unable to write\n");
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
640 close(source);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
641 phb->func(phb->data, -1, GAIM_INPUT_READ);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
642 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
643 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
644 return;
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
645 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
646
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
647 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread, phb);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
648 }
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
649
1881
a02584b98823 [gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1868
diff changeset
650 static int proxy_connect_socks5(char *host, unsigned short port, struct PHB *phb)
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
651 {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
652 int fd = -1;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
653 struct sockaddr_in sin;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
654 struct hostent *hp;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
655
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
656 debug_printf("connecting to %s:%d via %s:%d using SOCKS5\n", host, port, proxyhost, proxyport);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
657
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
658 if (!(hp = gethostbyname(proxyhost))) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
659 g_free(phb);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
660 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
661 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
662
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
663 memset(&sin, 0, sizeof(struct sockaddr_in));
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
664 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
665 sin.sin_family = hp->h_addrtype;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
666 sin.sin_port = htons(proxyport);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
667
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
668 if ((fd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
669 g_free(phb);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
670 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
671 }
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
672
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
673 phb->host = g_strdup(host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
674 phb->port = port;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
675
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
676 fcntl(fd, F_SETFL, O_NONBLOCK);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
677 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
678 if ((errno == EINPROGRESS) || (errno == EINTR)) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
679 debug_printf("Connect would have blocked\n");
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
680 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, s5_canwrite, phb);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
681 } else {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
682 close(fd);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
683 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
684 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
685 return -1;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
686 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
687 } else {
2048
3366311426df [gaim-migrate @ 2058]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1881
diff changeset
688 unsigned int len;
3366311426df [gaim-migrate @ 2058]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1881
diff changeset
689 int error = ETIMEDOUT;
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
690 debug_printf("Connect didn't block\n");
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
691 len = sizeof(error);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
692 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
693 close(fd);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
694 g_free(phb->host);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
695 g_free(phb);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
696 return -1;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
697 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
698 fcntl(fd, F_SETFL, 0);
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
699 s5_canwrite(phb, fd, GAIM_INPUT_WRITE);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
700 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
701
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
702 return fd;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
703 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
704
2090
b66aca8e8dce [gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2048
diff changeset
705 int proxy_connect(char *host, int port, GaimInputFunction func, gpointer data)
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
706 {
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
707 struct PHB *phb = g_new0(struct PHB, 1);
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
708 phb->func = func;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
709 phb->data = data;
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
710
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
711 if (!host || !port || (port == -1) || !func) {
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
712 g_free(phb);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
713 return -1;
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
714 }
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
715
2306
7973a8348d8b [gaim-migrate @ 2316]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2300
diff changeset
716 sethostent(1);
7973a8348d8b [gaim-migrate @ 2316]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2300
diff changeset
717
2372
2927c2c26fe6 [gaim-migrate @ 2385]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 2306
diff changeset
718 if ((proxytype == PROXY_NONE) || !proxyhost || !proxyhost[0] || !proxyport || (proxyport == -1))
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
719 return proxy_connect_none(host, port, phb);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
720 else if (proxytype == PROXY_HTTP)
1881
a02584b98823 [gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1868
diff changeset
721 return proxy_connect_http(host, port, phb);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
722 else if (proxytype == PROXY_SOCKS4)
1881
a02584b98823 [gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1868
diff changeset
723 return proxy_connect_socks4(host, port, phb);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
724 else if (proxytype == PROXY_SOCKS5)
1881
a02584b98823 [gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1868
diff changeset
725 return proxy_connect_socks5(host, port, phb);
1840
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
726
00aef397a1fe [gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1705
diff changeset
727 g_free(phb);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
728 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
729 }