1 const char gateway_rcs[] = "$Id: gateway.c,v 1.13 2002/03/13 00:29:59 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/gateway.c,v $
6 * Purpose : Contains functions to connect to a server, possibly
7 * using a "forwarder" (i.e. HTTP proxy and/or a SOCKS4
10 * Copyright : Written by and Copyright (C) 2001 the SourceForge
11 * Privoxy team. http://ijbswa.sourceforge.net
13 * Based on the Internet Junkbuster originally written
14 * by and Copyright (C) 1997 Anonymous Coders and
15 * Junkbusters Corporation. http://www.junkbusters.com
17 * This program is free software; you can redistribute it
18 * and/or modify it under the terms of the GNU General
19 * Public License as published by the Free Software
20 * Foundation; either version 2 of the License, or (at
21 * your option) any later version.
23 * This program is distributed in the hope that it will
24 * be useful, but WITHOUT ANY WARRANTY; without even the
25 * implied warranty of MERCHANTABILITY or FITNESS FOR A
26 * PARTICULAR PURPOSE. See the GNU General Public
27 * License for more details.
29 * The GNU General Public License should be included with
30 * this file. If not, you can view it at
31 * http://www.gnu.org/copyleft/gpl.html
32 * or write to the Free Software Foundation, Inc., 59
33 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 * Revision 1.13 2002/03/13 00:29:59 jongfoster
40 * Revision 1.12 2002/03/09 20:03:52 jongfoster
41 * - Making various functions return int rather than size_t.
42 * (Undoing a recent change). Since size_t is unsigned on
43 * Windows, functions like read_socket that return -1 on
44 * error cannot return a size_t.
46 * THIS WAS A MAJOR BUG - it caused frequent, unpredictable
47 * crashes, and also frequently caused JB to jump to 100%
48 * CPU and stay there. (Because it thought it had just
49 * read ((unsigned)-1) == 4Gb of data...)
51 * - The signature of write_socket has changed, it now simply
52 * returns success=0/failure=nonzero.
54 * - Trying to get rid of a few warnings --with-debug on
55 * Windows, I've introduced a new type "jb_socket". This is
56 * used for the socket file descriptors. On Windows, this
57 * is SOCKET (a typedef for unsigned). Everywhere else, it's
58 * an int. The error value can't be -1 any more, so it's
59 * now JB_INVALID_SOCKET (which is -1 on UNIX, and in
60 * Windows it maps to the #define INVALID_SOCKET.)
62 * - The signature of bind_port has changed.
64 * Revision 1.11 2002/03/08 17:46:04 jongfoster
65 * Fixing int/size_t warnings
67 * Revision 1.10 2002/03/07 03:50:19 oes
68 * - Improved handling of failed DNS lookups
69 * - Fixed compiler warnings
71 * Revision 1.9 2001/10/25 03:40:48 david__schmidt
72 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
73 * threads to call select() simultaneously. So, it's time to do a real, live,
74 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
75 * (native). Both versions will work, but using __OS2__ offers multi-threading.
77 * Revision 1.8 2001/09/13 20:10:12 jongfoster
78 * Fixing missing #include under Windows
80 * Revision 1.7 2001/09/12 17:58:26 steudten
82 * add #include <string.h>
84 * Revision 1.6 2001/09/10 10:41:16 oes
87 * Revision 1.5 2001/07/29 18:47:57 jongfoster
88 * Adding missing #include project.h
90 * Revision 1.4 2001/07/24 12:47:06 oes
91 * Applied BeOS support update by Eugenia
93 * Revision 1.3 2001/06/09 10:55:28 jongfoster
94 * Changing BUFSIZ ==> BUFFER_SIZE
96 * Revision 1.2 2001/06/07 23:11:38 jongfoster
97 * Removing gateways[] list - no longer used.
98 * Replacing function pointer in struct gateway with a directly
99 * called function forwarded_connect(), which can do the common
100 * task of deciding whether to connect to the web server or HTTP
102 * Replacing struct gateway with struct forward_spec
103 * Fixing bug with SOCKS4A and HTTP proxy server in combination.
104 * It was a bug which led to the connection being made to the web
105 * server rather than the HTTP proxy, and also a buffer overrun.
107 * Revision 1.1.1.1 2001/05/15 13:58:54 oes
108 * Initial import of version 2.9.3 source tree
111 *********************************************************************/
117 #include <sys/types.h>
120 #include <netinet/in.h>
127 #include <winsock2.h>
128 #endif /* def _WIN32 */
132 #endif /* def __BEOS__ */
136 #endif /* def __OS2__ */
141 #include "jbsockets.h"
144 const char gateway_h_rcs[] = GATEWAY_H_VERSION;
146 static jb_socket socks4_connect(const struct forward_spec * fwd,
147 const char * target_host,
149 struct client_state *csp);
152 #define SOCKS_REQUEST_GRANTED 90
153 #define SOCKS_REQUEST_REJECT 91
154 #define SOCKS_REQUEST_IDENT_FAILED 92
155 #define SOCKS_REQUEST_IDENT_CONFLICT 93
157 /* structure of a socks client operation */
159 unsigned char vn; /* socks version number */
160 unsigned char cd; /* command code */
161 unsigned char dstport[2]; /* destination port */
162 unsigned char dstip[4]; /* destination address */
163 unsigned char userid; /* first byte of userid */
164 /* more bytes of the userid follow, terminated by a NULL */
167 /* structure of a socks server reply */
169 unsigned char vn; /* socks version number */
170 unsigned char cd; /* command code */
171 unsigned char dstport[2]; /* destination port */
172 unsigned char dstip[4]; /* destination address */
175 static const char socks_userid[] = "anonymous";
178 /*********************************************************************
180 * Function : forwarded_connect
182 * Description : Connect to a specified web server, possibly via
183 * a HTTP proxy and/or a SOCKS proxy.
186 * 1 : gw = pointer to a gateway structure (such as gw_default)
187 * 2 : http = the http request and apropos headers
188 * 3 : csp = Current client state (buffers, headers, etc...)
190 * Returns : -1 => failure, else it is the socket file descriptor.
192 *********************************************************************/
193 jb_socket forwarded_connect(const struct forward_spec * fwd,
194 struct http_request *http,
195 struct client_state *csp)
197 const char * dest_host;
200 /* Figure out if we need to connect to the web server or a HTTP proxy. */
201 if (fwd->forward_host)
204 dest_host = fwd->forward_host;
205 dest_port = fwd->forward_port;
210 dest_host = http->host;
211 dest_port = http->port;
214 /* Connect, maybe using a SOCKS proxy */
218 return (connect_to(dest_host, dest_port, csp));
222 return (socks4_connect(fwd, dest_host, dest_port, csp));
225 /* Should never get here */
226 log_error(LOG_LEVEL_FATAL, "SOCKS4 impossible internal error - bad SOCKS type.");
228 return(JB_INVALID_SOCKET);
233 /*********************************************************************
235 * Function : socks4_connect
237 * Description : Connect to the SOCKS server, and connect through
238 * it to the specified server. This handles
239 * all the SOCKS negotiation, and returns a file
240 * descriptor for a socket which can be treated as a
241 * normal (non-SOCKS) socket.
244 * 1 : gw = pointer to a gateway structure (such as gw_default)
245 * 2 : http = the http request and apropos headers
246 * 3 : csp = Current client state (buffers, headers, etc...)
248 * Returns : -1 => failure, else a socket file descriptor.
250 *********************************************************************/
251 static jb_socket socks4_connect(const struct forward_spec * fwd,
252 const char * target_host,
254 struct client_state *csp)
257 char cbuf[BUFFER_SIZE];
258 char sbuf[BUFFER_SIZE];
259 struct socks_op *c = (struct socks_op *)cbuf;
260 struct socks_reply *s = (struct socks_reply *)sbuf;
267 if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
269 log_error(LOG_LEVEL_CONNECT, "socks4_connect: NULL gateway host specified");
273 if (fwd->gateway_port <= 0)
275 log_error(LOG_LEVEL_CONNECT, "socks4_connect: invalid gateway port specified");
282 return(JB_INVALID_SOCKET);
285 /* build a socks request for connection to the web server */
287 strcpy((char *)&(c->userid), socks_userid);
289 csiz = sizeof(*c) + sizeof(socks_userid) - 1;
294 web_server_addr = htonl(resolve_hostname_to_ip(target_host));
295 if (web_server_addr == INADDR_NONE)
297 log_error(LOG_LEVEL_CONNECT, "socks4_connect: could not resolve target host %s", target_host);
298 return(JB_INVALID_SOCKET);
302 web_server_addr = 0x00000001;
303 n = csiz + strlen(target_host) + 1;
304 if (n > sizeof(cbuf))
307 return(JB_INVALID_SOCKET);
309 strcpy(cbuf + csiz, target_host);
313 /* Should never get here */
314 log_error(LOG_LEVEL_FATAL, "SOCKS4 impossible internal error - bad SOCKS type.");
316 return(JB_INVALID_SOCKET);
321 c->dstport[0] = (target_port >> 8 ) & 0xff;
322 c->dstport[1] = (target_port ) & 0xff;
323 c->dstip[0] = (web_server_addr >> 24 ) & 0xff;
324 c->dstip[1] = (web_server_addr >> 16 ) & 0xff;
325 c->dstip[2] = (web_server_addr >> 8 ) & 0xff;
326 c->dstip[3] = (web_server_addr ) & 0xff;
328 /* pass the request to the socks server */
329 sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
331 if (sfd == JB_INVALID_SOCKET)
333 return(JB_INVALID_SOCKET);
336 if (write_socket(sfd, (char *)c, csiz))
338 log_error(LOG_LEVEL_CONNECT, "SOCKS4 negotiation write failed...");
340 return(JB_INVALID_SOCKET);
343 if (read_socket(sfd, sbuf, sizeof(sbuf)) != sizeof(*s))
345 log_error(LOG_LEVEL_CONNECT, "SOCKS4 negotiation read failed...");
347 return(JB_INVALID_SOCKET);
352 case SOCKS_REQUEST_GRANTED:
355 case SOCKS_REQUEST_REJECT:
356 errstr = "SOCKS request rejected or failed";
359 case SOCKS_REQUEST_IDENT_FAILED:
360 errstr = "SOCKS request rejected because "
361 "SOCKS server cannot connect to identd on the client";
364 case SOCKS_REQUEST_IDENT_CONFLICT:
365 errstr = "SOCKS request rejected because "
366 "the client program and identd report "
367 "different user-ids";
374 "SOCKS request rejected for reason code %d\n", s->cd);
377 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s ...", errstr);
380 return(JB_INVALID_SOCKET);