1 const char gateway_rcs[] = "$Id: gateway.c,v 1.1.1.1 2001/05/15 13:58:54 oes 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 * IJBSWA 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.1.1.1 2001/05/15 13:58:54 oes
38 * Initial import of version 2.9.3 source tree
41 *********************************************************************/
47 #include <sys/types.h>
52 #endif /* def _WIN32 */
56 #include "jbsockets.h"
59 const char gateway_h_rcs[] = GATEWAY_H_VERSION;
61 static int socks4_connect(const struct forward_spec * fwd,
62 const char * target_host,
64 struct client_state *csp);
67 #define SOCKS_REQUEST_GRANTED 90
68 #define SOCKS_REQUEST_REJECT 91
69 #define SOCKS_REQUEST_IDENT_FAILED 92
70 #define SOCKS_REQUEST_IDENT_CONFLICT 93
72 /* structure of a socks client operation */
74 unsigned char vn; /* socks version number */
75 unsigned char cd; /* command code */
76 unsigned char dstport[2]; /* destination port */
77 unsigned char dstip[4]; /* destination address */
78 unsigned char userid; /* first byte of userid */
79 /* more bytes of the userid follow, terminated by a NULL */
82 /* structure of a socks server reply */
84 unsigned char vn; /* socks version number */
85 unsigned char cd; /* command code */
86 unsigned char dstport[2]; /* destination port */
87 unsigned char dstip[4]; /* destination address */
90 static const char socks_userid[] = "anonymous";
93 /*********************************************************************
95 * Function : forwarded_connect
97 * Description : Connect to a specified web server, possibly via
98 * a HTTP proxy and/or a SOCKS proxy.
101 * 1 : gw = pointer to a gateway structure (such as gw_default)
102 * 2 : http = the http request and apropos headers
103 * 3 : csp = Current client state (buffers, headers, etc...)
105 * Returns : -1 => failure, else it is the socket file descriptor.
107 *********************************************************************/
108 int forwarded_connect(const struct forward_spec * fwd,
109 struct http_request *http,
110 struct client_state *csp)
112 const char * dest_host;
115 /* Figure out if we need to connect to the web server or a HTTP proxy. */
116 if (fwd->forward_host)
119 dest_host = fwd->forward_host;
120 dest_port = fwd->forward_port;
125 dest_host = http->host;
126 dest_port = http->port;
129 /* Connect, maybe using a SOCKS proxy */
133 return (connect_to(dest_host, dest_port, csp));
137 return (socks4_connect(fwd, dest_host, dest_port, csp));
140 /* Should never get here */
141 log_error(LOG_LEVEL_FATAL, "SOCKS4 impossible internal error - bad SOCKS type.");
148 /*********************************************************************
150 * Function : socks4_connect
152 * Description : Connect to the SOCKS server, and connect through
153 * it to the specified server. This handles
154 * all the SOCKS negotiation, and returns a file
155 * descriptor for a socket which can be treated as a
156 * normal (non-SOCKS) socket.
159 * 1 : gw = pointer to a gateway structure (such as gw_default)
160 * 2 : http = the http request and apropos headers
161 * 3 : csp = Current client state (buffers, headers, etc...)
163 * Returns : -1 => failure, else a socket file descriptor.
165 *********************************************************************/
166 static int socks4_connect(const struct forward_spec * fwd,
167 const char * target_host,
169 struct client_state *csp)
172 unsigned char cbuf[BUFSIZ];
173 unsigned char sbuf[BUFSIZ];
174 struct socks_op *c = (struct socks_op *)cbuf;
175 struct socks_reply *s = (struct socks_reply *)sbuf;
182 if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
184 log_error(LOG_LEVEL_CONNECT, "socks4_connect: NULL gateway host specified");
188 if (fwd->gateway_port <= 0)
190 log_error(LOG_LEVEL_CONNECT, "socks4_connect: invalid gateway port specified");
200 /* build a socks request for connection to the web server */
202 strcpy((char *)&(c->userid), socks_userid);
204 csiz = sizeof(*c) + sizeof(socks_userid) - 1;
209 web_server_addr = htonl(resolve_hostname_to_ip(target_host));
212 web_server_addr = 0x00000001;
213 n = csiz + strlen(target_host) + 1;
214 if (n > sizeof(cbuf))
219 strcpy(((char *)cbuf) + csiz, target_host);
223 /* Should never get here */
224 log_error(LOG_LEVEL_FATAL, "SOCKS4 impossible internal error - bad SOCKS type.");
231 c->dstport[0] = (target_port >> 8 ) & 0xff;
232 c->dstport[1] = (target_port ) & 0xff;
233 c->dstip[0] = (web_server_addr >> 24 ) & 0xff;
234 c->dstip[1] = (web_server_addr >> 16 ) & 0xff;
235 c->dstip[2] = (web_server_addr >> 8 ) & 0xff;
236 c->dstip[3] = (web_server_addr ) & 0xff;
238 /* pass the request to the socks server */
239 sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
246 if ((n = write_socket(sfd, (char *)c, csiz)) != csiz)
248 log_error(LOG_LEVEL_CONNECT, "SOCKS4 negotiation write failed...");
253 if ((n = read_socket(sfd, sbuf, sizeof(sbuf))) != sizeof(*s))
255 log_error(LOG_LEVEL_CONNECT, "SOCKS4 negotiation read failed...");
262 case SOCKS_REQUEST_GRANTED:
265 case SOCKS_REQUEST_REJECT:
266 errstr = "SOCKS request rejected or failed";
269 case SOCKS_REQUEST_IDENT_FAILED:
270 errstr = "SOCKS request rejected because "
271 "SOCKS server cannot connect to identd on the client";
274 case SOCKS_REQUEST_IDENT_CONFLICT:
275 errstr = "SOCKS request rejected because "
276 "the client program and identd report "
277 "different user-ids";
281 errstr = (char *) cbuf;
284 "SOCKS request rejected for reason code %d\n", s->cd);
287 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s ...", errstr);