1 const char gateway_rcs[] = "$Id: gateway.c,v 1.1 2001/05/13 21:57:06 administrator Exp $";
2 /*********************************************************************
4 * File : $Source: /home/administrator/cvs/ijb/gateway.c,v $
6 * Purpose : Contains functions to connect to a server, possibly
7 * using a "gateway" (i.e. HTTP proxy and/or SOCKS4
8 * proxy). Also contains the list of gateway types.
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.
38 *********************************************************************/
44 #include <sys/types.h>
49 #endif /* def _WIN32 */
53 #include "jbsockets.h"
56 const char gateway_h_rcs[] = GATEWAY_H_VERSION;
58 #define SOCKS_4 40 /* original SOCKS 4 protocol */
59 #define SOCKS_4A 41 /* as modified for hosts w/o external DNS */
61 const struct gateway gateways[] = {
62 /* type function gw type/host/port, fw host/port */
63 { "direct", direct_connect, 0, NULL, 0, NULL, 0 },
64 { ".", direct_connect, 0, NULL, 0, NULL, 0 },
65 { "socks", socks4_connect, SOCKS_4, NULL, 1080, NULL, 0 },
66 { "socks4", socks4_connect, SOCKS_4, NULL, 1080, NULL, 0 },
67 { "socks4a", socks4_connect, SOCKS_4A, NULL, 1080, NULL, 0 },
68 { NULL, NULL, 0, NULL, 0, NULL, 0 }
71 const struct gateway *gw_default = gateways; /* direct */
74 #define SOCKS_REQUEST_GRANTED 90
75 #define SOCKS_REQUEST_REJECT 91
76 #define SOCKS_REQUEST_IDENT_FAILED 92
77 #define SOCKS_REQUEST_IDENT_CONFLICT 93
79 /* structure of a socks client operation */
81 unsigned char vn; /* socks version number */
82 unsigned char cd; /* command code */
83 unsigned char dstport[2]; /* destination port */
84 unsigned char dstip[4]; /* destination address */
85 unsigned char userid; /* first byte of userid */
86 /* more bytes of the userid follow, terminated by a NULL */
89 /* structure of a socks server reply */
91 unsigned char vn; /* socks version number */
92 unsigned char cd; /* command code */
93 unsigned char dstport[2]; /* destination port */
94 unsigned char dstip[4]; /* destination address */
97 static const char socks_userid[] = "anonymous";
100 /*********************************************************************
102 * Function : direct_connect
104 * Description : Direct how we connect to the web. This can be:
105 * directly : no forwarding, or
106 * indirectly : through another proxy such as squid.
109 * 1 : gw = pointer to a gateway structure (such as gw_default)
110 * 2 : http = the http request and apropos headers
111 * 3 : csp = Current client state (buffers, headers, etc...)
113 * Returns : -1 => failure, else it is the socket file descriptor.
115 *********************************************************************/
116 int direct_connect(const struct gateway *gw, struct http_request *http, struct client_state *csp)
118 if (gw->forward_host)
120 return(connect_to(gw->forward_host, gw->forward_port, csp));
124 return(connect_to(http->host, http->port, csp));
130 /*********************************************************************
132 * Function : socks4_connect
134 * Description : Connect to the SOCKS server, and connect through
135 * it to the web server or web proxy. This handles
136 * all the SOCKS negotiation, and returns a file
137 * descriptor for a socket which can be treated as a
138 * normal (non-SOCKS) socket.
141 * 1 : gw = pointer to a gateway structure (such as gw_default)
142 * 2 : http = the http request and apropos headers
143 * 3 : csp = Current client state (buffers, headers, etc...)
145 * Returns : -1 => failure, else a socket file descriptor.
147 *********************************************************************/
148 int socks4_connect(const struct gateway *gw, struct http_request *http, struct client_state *csp)
151 unsigned char cbuf[BUFSIZ];
152 unsigned char sbuf[BUFSIZ];
153 struct socks_op *c = (struct socks_op *)cbuf;
154 struct socks_reply *s = (struct socks_reply *)sbuf;
155 int n, csiz, sfd, target_port;
157 char *errstr, *target_host;
159 if ((gw->gateway_host == NULL) || (*gw->gateway_host == '\0'))
161 log_error(LOG_LEVEL_CONNECT, "socks4_connect: NULL gateway host specified");
165 if (gw->gateway_port <= 0)
167 log_error(LOG_LEVEL_CONNECT, "socks4_connect: invalid gateway port specified");
177 if (gw->forward_host)
179 target_host = gw->forward_host;
180 target_port = gw->forward_port;
184 target_host = http->host;
185 target_port = http->port;
188 /* build a socks request for connection to the web server */
190 strcpy((char *)&(c->userid), socks_userid);
192 csiz = sizeof(*c) + sizeof(socks_userid) - 1;
197 web_server_addr = htonl(resolve_hostname_to_ip(target_host));
200 web_server_addr = 0x00000001;
201 n = csiz + strlen(target_host) + 1;
202 if (n > sizeof(cbuf))
207 strcpy(((char *)cbuf) + csiz, http->host);
211 /* Should never get here */
212 log_error(LOG_LEVEL_ERROR, "SOCKS4 impossible internal error - bad SOCKS type.");
219 c->dstport[0] = (target_port >> 8 ) & 0xff;
220 c->dstport[1] = (target_port ) & 0xff;
221 c->dstip[0] = (web_server_addr >> 24 ) & 0xff;
222 c->dstip[1] = (web_server_addr >> 16 ) & 0xff;
223 c->dstip[2] = (web_server_addr >> 8 ) & 0xff;
224 c->dstip[3] = (web_server_addr ) & 0xff;
226 /* pass the request to the socks server */
227 sfd = connect_to(gw->gateway_host, gw->gateway_port, csp);
234 if ((n = write_socket(sfd, (char *)c, csiz)) != csiz)
236 log_error(LOG_LEVEL_CONNECT, "SOCKS4 negotiation write failed...");
241 if ((n = read_socket(sfd, sbuf, sizeof(sbuf))) != sizeof(*s))
243 log_error(LOG_LEVEL_CONNECT, "SOCKS4 negotiation read failed...");
250 case SOCKS_REQUEST_GRANTED:
253 case SOCKS_REQUEST_REJECT:
254 errstr = "SOCKS request rejected or failed";
257 case SOCKS_REQUEST_IDENT_FAILED:
258 errstr = "SOCKS request rejected because "
259 "SOCKS server cannot connect to identd on the client";
262 case SOCKS_REQUEST_IDENT_CONFLICT:
263 errstr = "SOCKS request rejected because "
264 "the client program and identd report "
265 "different user-ids";
269 errstr = (char *) cbuf;
272 "SOCKS request rejected for reason code %d\n", s->cd);
275 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s ...", errstr);