Mercurial > pidgin
annotate src/proxy.c @ 568:c53958bfaf00
[gaim-migrate @ 578]
:-)
committer: Tailor Script <tailor@pidgin.im>
author | Rob Flynn <gaim@robflynn.com> |
---|---|
date | Tue, 01 Aug 2000 17:04:16 +0000 |
parents | b402a23f35df |
children | 5cb19972df52 |
rev | line source |
---|---|
1 | 1 /* |
2 * gaim | |
3 * | |
4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
5 * | |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or | |
9 * (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 * | |
20 */ | |
21 | |
22 /* this is a little piece of code to handle proxy connection */ | |
23 /* it is intended to : 1st handle http proxy, using the CONNECT command | |
24 , 2nd provide an easy way to add socks support */ | |
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 | 29 #include <stdio.h> |
30 #include <stdlib.h> | |
31 #include <string.h> | |
32 #include <sys/types.h> | |
33 #include <sys/socket.h> | |
34 #include <netdb.h> | |
35 #include <netinet/in.h> | |
36 #include <gtk/gtk.h> | |
37 #include "gaim.h" | |
38 #include "proxy.h" | |
39 | |
40 | |
210
ec0686b3b03f
[gaim-migrate @ 220]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
79
diff
changeset
|
41 /* static int proxy_inited=0; */ |
1 | 42 int proxy_type = 0; |
43 char proxy_host[256]; | |
44 int proxy_port = 3128; | |
45 char *proxy_realhost = NULL; | |
46 | |
47 /* this code is borrowed from cvs 1.10 */ | |
48 static int | |
49 proxy_recv_line (int sock, char **resultp) | |
50 { | |
51 int c; | |
52 char *result; | |
53 size_t input_index = 0; | |
54 size_t result_size = 80; | |
55 | |
79 | 56 result = g_malloc (result_size); |
1 | 57 |
58 while (1) | |
59 { | |
60 char ch; | |
61 if (recv (sock, &ch, 1, 0) < 0) | |
62 fprintf (stderr, "recv() error from proxy server\n"); | |
63 c = ch; | |
64 | |
65 if (c == EOF) | |
66 { | |
79 | 67 g_free (result); |
1 | 68 |
69 /* It's end of file. */ | |
70 fprintf(stderr, "end of file from server\n"); | |
71 } | |
72 | |
73 if (c == '\012') | |
74 break; | |
75 | |
76 result[input_index++] = c; | |
77 while (input_index + 1 >= result_size) | |
78 { | |
79 result_size *= 2; | |
79 | 80 result = (char *) g_realloc (result, result_size); |
1 | 81 } |
82 } | |
83 | |
84 if (resultp) | |
85 *resultp = result; | |
86 | |
87 /* Terminate it just for kicks, but we *can* deal with embedded NULs. */ | |
88 result[input_index] = '\0'; | |
89 | |
90 if (resultp == NULL) | |
79 | 91 g_free (result); |
1 | 92 return input_index; |
93 } | |
94 | |
95 | |
96 struct hostent *proxy_gethostbyname(char *host) | |
97 { | |
98 | |
99 if (proxy_type == PROXY_NONE) | |
100 return (gethostbyname(host)); | |
101 | |
102 if (proxy_realhost != NULL) | |
103 g_free(proxy_realhost); | |
104 | |
105 /* we keep the real host name for the Connect command */ | |
106 proxy_realhost = (char *) strdup(host); | |
107 | |
108 return (gethostbyname(proxy_host)); | |
109 | |
110 } | |
111 | |
112 | |
113 int proxy_connect(int sockfd, struct sockaddr *serv_addr, int | |
114 addrlen ) | |
115 { | |
116 struct sockaddr_in name; | |
117 int ret; | |
118 | |
119 switch (proxy_type) { | |
120 case PROXY_NONE: | |
121 /* normal use */ | |
122 return (connect(sockfd,serv_addr,addrlen)); | |
123 break; | |
124 case PROXY_HTTP: /* Http proxy */ | |
125 /* do the tunneling */ | |
126 /* step one : connect to proxy */ | |
127 { | |
128 struct hostent *hostinfo; | |
129 unsigned short shortport = proxy_port; | |
130 | |
131 memset (&name, 0, sizeof (name)); | |
132 name.sin_family = AF_INET; | |
133 name.sin_port = htons (shortport); | |
134 hostinfo = gethostbyname (proxy_host); | |
135 if (hostinfo == NULL) { | |
136 fprintf (stderr, "Unknown host %s.\n", proxy_host); | |
137 return (-1); | |
138 } | |
139 name.sin_addr = *(struct in_addr *) hostinfo->h_addr; | |
140 } | |
141 sprintf(debug_buff,"Trying to connect ...\n"); | |
142 debug_print(debug_buff); | |
143 if ((ret = connect(sockfd,(struct sockaddr *)&name,sizeof(name)))<0) | |
144 return(ret); | |
145 | |
146 /* step two : do proxy tunneling init */ | |
147 { | |
148 char cmd[80]; | |
149 char *inputline; | |
150 unsigned short realport=ntohs(((struct sockaddr_in *)serv_addr)->sin_port); | |
151 sprintf(cmd,"CONNECT %s:%d HTTP/1.1\n\r\n\r",proxy_realhost,realport); | |
152 sprintf(debug_buff,"<%s>\n",cmd); | |
153 debug_print(debug_buff); | |
154 if (send(sockfd,cmd,strlen(cmd),0)<0) | |
155 return(-1); | |
156 if (proxy_recv_line(sockfd,&inputline) < 0) { | |
157 return(-1); | |
158 } | |
159 sprintf(debug_buff,"<%s>\n",inputline); | |
160 debug_print(debug_buff); | |
161 if (memcmp("HTTP/1.0 200 Connection established",inputline,35)) | |
162 if (memcmp("HTTP/1.1 200 Connection established",inputline,35)) { | |
163 free(inputline); | |
164 return(-1); | |
165 } | |
166 | |
167 while (strlen(inputline)>1) { | |
168 free(inputline); | |
169 if (proxy_recv_line(sockfd,&inputline) < 0) { | |
170 return(-1); | |
171 } | |
172 sprintf(debug_buff,"<%s>\n",inputline); | |
173 debug_print(debug_buff); | |
174 } | |
175 free(inputline); | |
176 } | |
177 | |
178 return ret; | |
179 break; | |
338
9d258a0aa560
[gaim-migrate @ 348]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
180 case PROXY_SOCKS4: /* Socks v4 proxy (? I'm not a proxy hacker) */ |
234
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
181 /* this is going to be a cross between the HTTP proxy code |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
182 * above and the TiK proxy code, translated from tcl->C */ |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
183 { |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
184 struct hostent *hostinfo; |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
185 unsigned short shortport = proxy_port; |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
186 |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
187 memset(&name, 0, sizeof (name)); |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
188 name.sin_family = AF_INET; |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
189 name.sin_port = htons (shortport); |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
190 hostinfo = gethostbyname (proxy_host); |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
191 if (hostinfo == NULL) { |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
192 fprintf (stderr, "Unknown host %s.\n", proxy_host); |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
193 return (-1); |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
194 } |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
195 name.sin_addr = *(struct in_addr *) hostinfo->h_addr; |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
196 } |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
197 sprintf(debug_buff,"Trying to connect ...\n"); |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
198 debug_print(debug_buff); |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
199 if ((ret = connect(sockfd,(struct sockaddr *)&name,sizeof(name)))<0) |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
200 return(ret); |
253
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
201 |
234
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
202 /* here's where it's no longer http proxy and is now tik */ |
253
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
203 { |
234
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
204 char cmd[80]; |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
205 char *inputline; |
253
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
206 unsigned short realport=((struct sockaddr_in *)serv_addr)->sin_port; |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
207 unsigned long realhost=((struct sockaddr_in *)serv_addr)->sin_addr.s_addr; |
234
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
208 |
253
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
209 cmd[0] = 4; cmd[0] = 1; |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
210 memcpy(cmd + 2, &realport, 2); |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
211 memcpy(cmd + 4, &realhost, 4); |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
212 cmd[8] = 0; |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
213 if (send(sockfd,cmd,8,0)<0) |
234
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
214 return(-1); |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
215 if (proxy_recv_line(sockfd,&inputline) < 0) { |
985635758c33
[gaim-migrate @ 244]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
210
diff
changeset
|
216 return(-1); |
253
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
217 } |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
218 if (inputline[1] != 90) { |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
219 sprintf(debug_buff, "request failed: %d\n", |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
220 inputline[1]); |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
221 debug_print(debug_buff); |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
222 if (inputline[1] == 91) |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
223 do_error_dialog("The SOCKS proxy denied" |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
224 "your request.", |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
225 "Gaim - Proxy Error"); |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
226 else |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
227 do_error_dialog("Unknown SOCKS error.", |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
228 "Gaim - Proxy Error"); |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
229 return -1; |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
230 } |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
231 } |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
234
diff
changeset
|
232 return ret; |
1 | 233 break; |
338
9d258a0aa560
[gaim-migrate @ 348]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
234 case PROXY_SOCKS5: |
9d258a0aa560
[gaim-migrate @ 348]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
235 return -1; |
9d258a0aa560
[gaim-migrate @ 348]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
236 break; |
1 | 237 default: |
238 fprintf(stderr,"Unknown proxy type : %d.\n",proxy_type); | |
239 break; | |
240 } | |
241 return(-1); | |
242 } | |
243 |