14192
|
1 /**
|
|
2 * The QQ2003C protocol plugin
|
|
3 *
|
|
4 * for gaim
|
|
5 *
|
|
6 * Copyright (C) 2004 Puzzlebird
|
|
7 * Henry Ou <henry@linux.net>
|
|
8 *
|
|
9 * This program is free software; you can redistribute it and/or modify
|
|
10 * it under the terms of the GNU General Public License as published by
|
|
11 * the Free Software Foundation; either version 2 of the License, or
|
|
12 * (at your option) any later version.
|
|
13 *
|
|
14 * This program is distributed in the hope that it will be useful,
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 * GNU General Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU General Public License
|
|
20 * along with this program; if not, write to the Free Software
|
|
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
22 */
|
|
23
|
|
24 #include "debug.h"
|
|
25
|
|
26 #include "udp_proxy_s5.h"
|
|
27
|
|
28 static void _qq_s5_canread_again(gpointer data, gint source, GaimInputCondition cond)
|
|
29 {
|
|
30 unsigned char buf[512];
|
|
31 struct PHB *phb = data;
|
|
32 struct sockaddr_in sin;
|
|
33 int len, error;
|
14195
|
34 socklen_t errlen;
|
14192
|
35
|
|
36 gaim_input_remove(phb->inpa);
|
|
37 gaim_debug(GAIM_DEBUG_INFO, "socks5 proxy", "Able to read again.\n");
|
|
38
|
|
39 len = read(source, buf, 10);
|
|
40 if (len < 10) {
|
|
41 gaim_debug(GAIM_DEBUG_WARNING, "socks5 proxy", "or not...\n");
|
|
42 close(source);
|
|
43
|
|
44 if (phb->account == NULL || gaim_account_get_connection(phb->account) != NULL) {
|
|
45
|
|
46 phb->func(phb->data, source, NULL);
|
|
47 }
|
|
48
|
|
49 g_free(phb->host);
|
|
50 g_free(phb);
|
|
51 return;
|
|
52 }
|
|
53 if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
|
|
54 if ((buf[0] == 0x05) && (buf[1] < 0x09))
|
|
55 gaim_debug(GAIM_DEBUG_ERROR, "socks5 proxy", "socks5 error: %x\n", buf[1]);
|
|
56 else
|
|
57 gaim_debug(GAIM_DEBUG_ERROR, "socks5 proxy", "Bad data.\n");
|
|
58 close(source);
|
|
59
|
|
60 if (phb->account == NULL || gaim_account_get_connection(phb->account) != NULL) {
|
|
61
|
14195
|
62 phb->func(phb->data, -1, _("Unable to connect"));
|
14192
|
63 }
|
|
64
|
|
65 g_free(phb->host);
|
|
66 g_free(phb);
|
|
67 return;
|
|
68 }
|
|
69
|
|
70 sin.sin_family = AF_INET;
|
|
71 memcpy(&sin.sin_addr.s_addr, buf + 4, 4);
|
|
72 memcpy(&sin.sin_port, buf + 8, 2);
|
|
73
|
|
74 if (connect(phb->udpsock, (struct sockaddr *) &sin, sizeof(struct sockaddr_in)) < 0) {
|
|
75 gaim_debug(GAIM_DEBUG_INFO, "s5_canread_again", "connect failed: %s\n", strerror(errno));
|
|
76 close(phb->udpsock);
|
|
77 close(source);
|
|
78 g_free(phb->host);
|
|
79 g_free(phb);
|
|
80 return;
|
|
81 }
|
|
82
|
|
83 error = ETIMEDOUT;
|
|
84 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Connect didn't block\n");
|
14195
|
85 errlen = sizeof(error);
|
|
86 if (getsockopt(phb->udpsock, SOL_SOCKET, SO_ERROR, &error, &errlen) < 0) {
|
14192
|
87 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "getsockopt failed.\n");
|
|
88 close(phb->udpsock);
|
|
89 return;
|
|
90 }
|
|
91 fcntl(phb->udpsock, F_SETFL, 0);
|
|
92
|
|
93 if (phb->account == NULL || gaim_account_get_connection(phb->account) != NULL) {
|
|
94 phb->func(phb->data, phb->udpsock, NULL);
|
|
95 }
|
|
96
|
|
97 g_free(phb->host);
|
|
98 g_free(phb);
|
|
99 }
|
|
100
|
|
101 static void _qq_s5_sendconnect(gpointer data, gint source)
|
|
102 {
|
|
103 unsigned char buf[512];
|
|
104 struct PHB *phb = data;
|
|
105 struct sockaddr_in sin, ctlsin;
|
14195
|
106 int port;
|
|
107 socklen_t ctllen;
|
14192
|
108
|
|
109 gaim_debug(GAIM_DEBUG_INFO, "s5_sendconnect", "remote host is %s:%d\n", phb->host, phb->port);
|
|
110
|
|
111 buf[0] = 0x05;
|
|
112 buf[1] = 0x03; /* udp relay */
|
|
113 buf[2] = 0x00; /* reserved */
|
|
114 buf[3] = 0x01; /* address type -- ipv4 */
|
|
115 memset(buf + 4, 0, 0x04);
|
|
116
|
|
117 ctllen = sizeof(ctlsin);
|
|
118 if (getsockname(source, (struct sockaddr *) &ctlsin, &ctllen) < 0) {
|
|
119 gaim_debug(GAIM_DEBUG_INFO, "QQ", "getsockname: %s\n", strerror(errno));
|
|
120 close(source);
|
|
121 g_free(phb->host);
|
|
122 g_free(phb);
|
|
123 return;
|
|
124 }
|
|
125
|
|
126 phb->udpsock = socket(PF_INET, SOCK_DGRAM, 0);
|
|
127 gaim_debug(GAIM_DEBUG_INFO, "s5_sendconnect", "UDP socket=%d\n", phb->udpsock);
|
|
128 if (phb->udpsock < 0) {
|
|
129 close(source);
|
|
130 g_free(phb->host);
|
|
131 g_free(phb);
|
|
132 return;
|
|
133 }
|
|
134
|
|
135 fcntl(phb->udpsock, F_SETFL, O_NONBLOCK);
|
|
136
|
|
137 port = ntohs(ctlsin.sin_port) + 1;
|
|
138 while (1) {
|
14195
|
139 inet_aton("0.0.0.0", &(sin.sin_addr));
|
|
140 sin.sin_family = AF_INET;
|
|
141 sin.sin_port = htons(port);
|
14192
|
142 if (bind(phb->udpsock, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
|
|
143 port++;
|
|
144 if (port > 65500) {
|
|
145 close(source);
|
|
146 g_free(phb->host);
|
|
147 g_free(phb);
|
|
148 return;
|
|
149 }
|
|
150 } else
|
|
151 break;
|
|
152 }
|
|
153
|
|
154 memset(buf + 4, 0, 0x04);
|
|
155 memcpy(buf + 8, &(sin.sin_port), 0x02);
|
|
156
|
|
157 if (write(source, buf, 10) < 10) {
|
|
158 close(source);
|
|
159 gaim_debug(GAIM_DEBUG_INFO, "s5_sendconnect", "packet too small\n");
|
|
160
|
|
161 if (phb->account == NULL || gaim_account_get_connection(phb->account) != NULL) {
|
14195
|
162 phb->func(phb->data, -1, _("Unable to connect"));
|
14192
|
163 }
|
|
164
|
|
165 g_free(phb->host);
|
|
166 g_free(phb);
|
|
167 return;
|
|
168 }
|
|
169
|
|
170 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, _qq_s5_canread_again, phb);
|
|
171 }
|
|
172
|
|
173 static void _qq_s5_readauth(gpointer data, gint source, GaimInputCondition cond)
|
|
174 {
|
|
175 unsigned char buf[512];
|
|
176 struct PHB *phb = data;
|
|
177
|
|
178 gaim_input_remove(phb->inpa);
|
|
179 gaim_debug(GAIM_DEBUG_INFO, "socks5 proxy", "Got auth response.\n");
|
|
180
|
|
181 if (read(source, buf, 2) < 2) {
|
|
182 close(source);
|
|
183
|
|
184 if (phb->account == NULL || gaim_account_get_connection(phb->account) != NULL) {
|
|
185
|
14195
|
186 phb->func(phb->data, -1, _("Unable to connect"));
|
14192
|
187 }
|
|
188
|
|
189 g_free(phb->host);
|
|
190 g_free(phb);
|
|
191 return;
|
|
192 }
|
|
193
|
|
194 if ((buf[0] != 0x01) || (buf[1] != 0x00)) {
|
|
195 close(source);
|
|
196
|
|
197 if (phb->account == NULL || gaim_account_get_connection(phb->account) != NULL) {
|
|
198
|
14195
|
199 phb->func(phb->data, -1, _("Unable to connect"));
|
14192
|
200 }
|
|
201
|
|
202 g_free(phb->host);
|
|
203 g_free(phb);
|
|
204 return;
|
|
205 }
|
|
206
|
|
207 _qq_s5_sendconnect(phb, source);
|
|
208 }
|
|
209
|
|
210 static void _qq_s5_canread(gpointer data, gint source, GaimInputCondition cond)
|
|
211 {
|
|
212 unsigned char buf[512];
|
|
213 struct PHB *phb;
|
|
214 int ret;
|
|
215
|
|
216 phb = data;
|
|
217
|
|
218 gaim_input_remove(phb->inpa);
|
|
219 gaim_debug(GAIM_DEBUG_INFO, "socks5 proxy", "Able to read.\n");
|
|
220
|
|
221 ret = read(source, buf, 2);
|
|
222 if (ret < 2) {
|
|
223 gaim_debug(GAIM_DEBUG_INFO, "s5_canread", "packet smaller than 2 octet\n");
|
|
224 close(source);
|
|
225
|
|
226 if (phb->account == NULL || gaim_account_get_connection(phb->account) != NULL) {
|
|
227
|
14346
|
228 phb->func(phb->data, -1, _("Unable to connect"));
|
14192
|
229 }
|
|
230
|
|
231 g_free(phb->host);
|
|
232 g_free(phb);
|
|
233 return;
|
|
234 }
|
|
235
|
|
236 if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
|
|
237 gaim_debug(GAIM_DEBUG_INFO, "s5_canread", "unsupport\n");
|
|
238 close(source);
|
|
239
|
|
240 if (phb->account == NULL || gaim_account_get_connection(phb->account) != NULL) {
|
|
241
|
14195
|
242 phb->func(phb->data, -1, _("Unable to connect"));
|
14192
|
243 }
|
|
244
|
|
245 g_free(phb->host);
|
|
246 g_free(phb);
|
|
247 return;
|
|
248 }
|
|
249
|
|
250 if (buf[1] == 0x02) {
|
|
251 unsigned int i, j;
|
|
252
|
|
253 i = strlen(gaim_proxy_info_get_username(phb->gpi));
|
|
254 j = strlen(gaim_proxy_info_get_password(phb->gpi));
|
|
255
|
|
256 buf[0] = 0x01; /* version 1 */
|
|
257 buf[1] = i;
|
|
258 memcpy(buf + 2, gaim_proxy_info_get_username(phb->gpi), i);
|
|
259 buf[2 + i] = j;
|
|
260 memcpy(buf + 2 + i + 1, gaim_proxy_info_get_password(phb->gpi), j);
|
|
261
|
|
262 if (write(source, buf, 3 + i + j) < 3 + i + j) {
|
|
263 close(source);
|
|
264
|
|
265 if (phb->account == NULL || gaim_account_get_connection(phb->account) != NULL) {
|
|
266
|
14195
|
267 phb->func(phb->data, -1, _("Unable to connect"));
|
14192
|
268 }
|
|
269
|
|
270 g_free(phb->host);
|
|
271 g_free(phb);
|
|
272 return;
|
|
273 }
|
|
274
|
|
275 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, _qq_s5_readauth, phb);
|
|
276 } else {
|
|
277 gaim_debug(GAIM_DEBUG_INFO, "s5_canread", "calling s5_sendconnect\n");
|
|
278 _qq_s5_sendconnect(phb, source);
|
|
279 }
|
|
280 }
|
|
281
|
|
282 static void _qq_s5_canwrite(gpointer data, gint source, GaimInputCondition cond)
|
|
283 {
|
|
284 unsigned char buf[512];
|
|
285 int i;
|
|
286 struct PHB *phb = data;
|
|
287 unsigned int len;
|
|
288 int error = ETIMEDOUT;
|
|
289
|
|
290 gaim_debug(GAIM_DEBUG_INFO, "socks5 proxy", "Connected.\n");
|
|
291
|
|
292 if (phb->inpa > 0)
|
|
293 gaim_input_remove(phb->inpa);
|
|
294
|
|
295 len = sizeof(error);
|
|
296 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
|
|
297 gaim_debug(GAIM_DEBUG_INFO, "getsockopt", "%s\n", strerror(errno));
|
|
298 close(source);
|
|
299 if (phb->account == NULL || gaim_account_get_connection(phb->account) != NULL) {
|
|
300
|
14195
|
301 phb->func(phb->data, -1, _("Unable to connect"));
|
14192
|
302 }
|
|
303
|
|
304 g_free(phb->host);
|
|
305 g_free(phb);
|
|
306 return;
|
|
307 }
|
|
308 fcntl(source, F_SETFL, 0);
|
|
309
|
|
310 i = 0;
|
|
311 buf[0] = 0x05; /* SOCKS version 5 */
|
|
312
|
|
313 if (gaim_proxy_info_get_username(phb->gpi) != NULL) {
|
|
314 buf[1] = 0x02; /* two methods */
|
|
315 buf[2] = 0x00; /* no authentication */
|
|
316 buf[3] = 0x02; /* username/password authentication */
|
|
317 i = 4;
|
|
318 } else {
|
|
319 buf[1] = 0x01;
|
|
320 buf[2] = 0x00;
|
|
321 i = 3;
|
|
322 }
|
|
323
|
|
324 if (write(source, buf, i) < i) {
|
|
325 gaim_debug(GAIM_DEBUG_INFO, "write", "%s\n", strerror(errno));
|
|
326 gaim_debug(GAIM_DEBUG_ERROR, "socks5 proxy", "Unable to write\n");
|
|
327 close(source);
|
|
328
|
|
329 if (phb->account == NULL || gaim_account_get_connection(phb->account) != NULL) {
|
|
330
|
14195
|
331 phb->func(phb->data, -1, _("Unable to connect"));
|
14192
|
332 }
|
|
333
|
|
334 g_free(phb->host);
|
|
335 g_free(phb);
|
|
336 return;
|
|
337 }
|
|
338
|
|
339 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, _qq_s5_canread, phb);
|
|
340 }
|
|
341
|
|
342 gint qq_proxy_socks5(struct PHB *phb, struct sockaddr *addr, socklen_t addrlen)
|
|
343 {
|
|
344 gint fd;
|
|
345 gaim_debug(GAIM_DEBUG_INFO, "QQ",
|
|
346 "Connecting to %s:%d via %s:%d using SOCKS5\n",
|
|
347 phb->host, phb->port, gaim_proxy_info_get_host(phb->gpi), gaim_proxy_info_get_port(phb->gpi));
|
|
348
|
|
349 if ((fd = socket(addr->sa_family, SOCK_STREAM, 0)) < 0)
|
|
350 return -1;
|
|
351
|
|
352 gaim_debug(GAIM_DEBUG_INFO, "QQ", "proxy_sock5 return fd=%d\n", fd);
|
|
353
|
|
354 fcntl(fd, F_SETFL, O_NONBLOCK);
|
|
355 if (connect(fd, addr, addrlen) < 0) {
|
|
356 if ((errno == EINPROGRESS) || (errno == EINTR)) {
|
|
357 gaim_debug(GAIM_DEBUG_WARNING, "QQ", "Connect in asynchronous mode.\n");
|
|
358 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, _qq_s5_canwrite, phb);
|
|
359 } else {
|
|
360 close(fd);
|
|
361 return -1;
|
|
362 }
|
|
363 } else {
|
|
364 gaim_debug(GAIM_DEBUG_MISC, "QQ", "Connect in blocking mode.\n");
|
|
365 fcntl(fd, F_SETFL, 0);
|
|
366 _qq_s5_canwrite(phb, fd, GAIM_INPUT_WRITE);
|
|
367 }
|
|
368
|
|
369 return fd;
|
|
370 }
|