annotate src/proxy.c @ 1768:9aae465eeb6e

[gaim-migrate @ 1778] Start of the queueing stuff committer: Tailor Script <tailor@pidgin.im>
author Rob Flynn <gaim@robflynn.com>
date Sat, 28 Apr 2001 02:01:28 +0000
parents b4d454e5ee31
children 00aef397a1fe
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
b402a23f35df [gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 338
diff changeset
27 #include "../config.h"
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>
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
37 #include <gtk/gtk.h>
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
38 #include "gaim.h"
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
39 #include "proxy.h"
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
40
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
41 static int proxy_connect_none(char *host, unsigned short port)
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
42 {
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
43 struct sockaddr_in sin;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
44 struct hostent *hp;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
45 int fd = -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
46
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
47 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
48
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
49 if (!(hp = gethostbyname(host)))
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
50 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
51
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
52 memset(&sin, 0, sizeof(struct sockaddr_in));
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
53 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
54 sin.sin_family = hp->h_addrtype;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
55 sin.sin_port = htons(port);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
56
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
57 if ((fd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0)
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
58 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
59
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
60 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
61 close(fd);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
62 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
63 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
64
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
65 return fd;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
66 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
67
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
68 #define HTTP_GOODSTRING "HTTP/1.0 200 Connection established"
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
69 #define HTTP_GOODSTRING2 "HTTP/1.1 200 Connection established"
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
70
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
71 static int proxy_connect_http(char *host, unsigned short port, char *proxyhost, unsigned short proxyport)
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
72 {
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
73 struct hostent *hp;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
74 struct sockaddr_in sin;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
75 int fd = -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
76 char cmd[384];
1695
c0ab844d4790 [gaim-migrate @ 1705]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1451
diff changeset
77 char inputline[8192];
c0ab844d4790 [gaim-migrate @ 1705]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1451
diff changeset
78 int nlc = 0;
c0ab844d4790 [gaim-migrate @ 1705]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1451
diff changeset
79 int pos = 0;
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
80
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
81 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
82
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
83 if (!(hp = gethostbyname(proxyhost)))
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
84 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
85
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
86 memset(&sin, 0, sizeof(struct sockaddr_in));
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
87 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
88 sin.sin_family = hp->h_addrtype;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
89 sin.sin_port = htons(proxyport);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
90
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
91 if ((fd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0)
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
92 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
93
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
94 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
95 close(fd);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
96 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
97 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
98
1705
b4d454e5ee31 [gaim-migrate @ 1715]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1695
diff changeset
99 snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\r\n\r\n", host, port);
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
100
1695
c0ab844d4790 [gaim-migrate @ 1705]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1451
diff changeset
101 if (send(fd, cmd, strlen(cmd), 0) < 0) {
c0ab844d4790 [gaim-migrate @ 1705]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1451
diff changeset
102 close(fd);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
103 return -1;
1695
c0ab844d4790 [gaim-migrate @ 1705]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1451
diff changeset
104 }
c0ab844d4790 [gaim-migrate @ 1705]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1451
diff changeset
105 while ((nlc != 2) && (read(fd, &inputline[pos++], 1) == 1)) {
c0ab844d4790 [gaim-migrate @ 1705]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1451
diff changeset
106 if (inputline[pos-1] == '\n')
c0ab844d4790 [gaim-migrate @ 1705]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1451
diff changeset
107 nlc++;
c0ab844d4790 [gaim-migrate @ 1705]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1451
diff changeset
108 else if (inputline[pos-1] != '\r')
c0ab844d4790 [gaim-migrate @ 1705]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1451
diff changeset
109 nlc = 0;
c0ab844d4790 [gaim-migrate @ 1705]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1451
diff changeset
110 }
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
111
1705
b4d454e5ee31 [gaim-migrate @ 1715]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1695
diff changeset
112 debug_printf("Proxy says: %s\n", inputline);
b4d454e5ee31 [gaim-migrate @ 1715]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1695
diff changeset
113
1372
153eb28ea454 [gaim-migrate @ 1382]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1252
diff changeset
114 if ((memcmp(HTTP_GOODSTRING, inputline, strlen(HTTP_GOODSTRING)) == 0) ||
153eb28ea454 [gaim-migrate @ 1382]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1252
diff changeset
115 (memcmp(HTTP_GOODSTRING2, inputline, strlen(HTTP_GOODSTRING2)) == 0)) {
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
116 return fd;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
117 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
118
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
119 close(fd);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
120 return -1;
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
121 }
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
122
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
123 static int proxy_connect_socks4(char *host, unsigned short port,
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
124 char *proxyhost, unsigned short proxyport)
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
125 {
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
126 struct sockaddr_in sin;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
127 unsigned char packet[12];
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
128 struct hostent *hp;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
129 int fd = -1;
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
130
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
131 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
132
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
133 if (!(hp = gethostbyname(proxyhost)))
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
134 return -1;
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
135
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
136 memset(&sin, 0, sizeof(struct sockaddr_in));
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
137 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
138 sin.sin_family = hp->h_addrtype;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
139 sin.sin_port = htons(proxyport);
664
5cb19972df52 [gaim-migrate @ 674]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 349
diff changeset
140
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
141 if ((fd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0)
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
142 return -1;
253
5b28ef2b550e [gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 234
diff changeset
143
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
144 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
145 close(fd);
664
5cb19972df52 [gaim-migrate @ 674]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 349
diff changeset
146 return -1;
5cb19972df52 [gaim-migrate @ 674]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 349
diff changeset
147 }
670
9e51c1a4abba [gaim-migrate @ 680]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 664
diff changeset
148
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
149 /* XXX does socks4 not support host name lookups by the proxy? */
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
150 if (!(hp = gethostbyname(host)))
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
151 return -1;
670
9e51c1a4abba [gaim-migrate @ 680]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 664
diff changeset
152
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
153 packet[0] = 4;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
154 packet[1] = 1;
1451
f6cc5cb7faf6 [gaim-migrate @ 1461]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1372
diff changeset
155 packet[2] = port >> 8;
f6cc5cb7faf6 [gaim-migrate @ 1461]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1372
diff changeset
156 packet[3] = port & 0xff;
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
157 packet[4] = (unsigned char)(hp->h_addr_list[0])[0];
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
158 packet[5] = (unsigned char)(hp->h_addr_list[0])[1];
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
159 packet[6] = (unsigned char)(hp->h_addr_list[0])[2];
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
160 packet[7] = (unsigned char)(hp->h_addr_list[0])[3];
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
161 packet[8] = 0;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
162 if (write(fd, packet, 9) == 9) {
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
163 memset(packet, 0, sizeof(packet));
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
164 if (read(fd, packet, 9) >= 4 && packet[1] == 90)
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
165 return fd;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
166 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
167 close(fd);
670
9e51c1a4abba [gaim-migrate @ 680]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 664
diff changeset
168
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
169 return -1;
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
170 }
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
171
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
172 static int proxy_connect_socks5(char *host, unsigned short port,
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
173 char *proxyhost, unsigned short proxyport)
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
174 {
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
175 int i, fd = -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
176 unsigned char buf[512];
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
177 struct sockaddr_in sin;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
178 struct hostent *hp;
1451
f6cc5cb7faf6 [gaim-migrate @ 1461]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1372
diff changeset
179 int hlen = strlen(host);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
180
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
181 debug_printf("connecting to %s:%d via %s:%d using SOCKS5\n", host, port, proxyhost, proxyport);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
182
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
183 if (!(hp = gethostbyname(proxyhost)))
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
184 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
185
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
186 memset(&sin, 0, sizeof(struct sockaddr_in));
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
187 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
188 sin.sin_family = hp->h_addrtype;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
189 sin.sin_port = htons(proxyport);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
190
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
191 if ((fd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0)
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
192 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
193
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
194 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
195 close(fd);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
196 return -1;
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 i = 0;
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
200 buf[0] = 0x05; /* SOCKS version 5 */
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
201 buf[1] = 0x01;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
202 buf[2] = 0x00;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
203 i = 3;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
204
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
205 if (write(fd, buf, i) < i) {
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
206 close(fd);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
207 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
208 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
209
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
210 if (read(fd, buf, 2) < 2) {
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
211 close(fd);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
212 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
213 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
214
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
215 if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
216 close(fd);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
217 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
218 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
219
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
220 buf[0] = 0x05;
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
221 buf[1] = 0x01; /* CONNECT */
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
222 buf[2] = 0x00; /* reserved */
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
223 buf[3] = 0x03; /* address type -- host name */
1451
f6cc5cb7faf6 [gaim-migrate @ 1461]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1372
diff changeset
224 buf[4] = hlen;
f6cc5cb7faf6 [gaim-migrate @ 1461]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1372
diff changeset
225 memcpy(buf + 5, host, hlen);
f6cc5cb7faf6 [gaim-migrate @ 1461]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1372
diff changeset
226 buf[5 + strlen(host)] = port >> 8;
f6cc5cb7faf6 [gaim-migrate @ 1461]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1372
diff changeset
227 buf[5 + strlen(host) + 1] = port & 0xff;
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
228
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
229 if (write(fd, buf, (5 + strlen(host) + 2)) < (5 + strlen(host) + 2)) {
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
230 close(fd);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
231 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
232 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
233 if (read(fd, buf, 10) < 10) {
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
234 close(fd);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
235 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
236 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
237 if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
238 close(fd);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
239 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
240 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
241
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
242 return fd;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
243 }
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
244
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
245 int proxy_connect(char *host, int port, char *proxyhost, int proxyport, int proxytype)
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
246 {
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
247 if (!host || !port || (port == -1))
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
248 return -1;
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
249 else if ((proxytype == PROXY_NONE) ||
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
250 !proxyhost || !proxyhost[0] ||
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1087
diff changeset
251 !proxyport || (proxyport == -1)) return proxy_connect_none(host, port);
1087
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
252 else if (proxytype == PROXY_HTTP)
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
253 return proxy_connect_http(host, port, proxyhost, proxyport);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
254 else if (proxytype == PROXY_SOCKS4)
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
255 return proxy_connect_socks4(host, port, proxyhost, proxyport);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
256 else if (proxytype == PROXY_SOCKS5)
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
257 return proxy_connect_socks5(host, port, proxyhost, proxyport);
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
258 return -1;
56c7ceb986a8 [gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1057
diff changeset
259 }