-const char gateway_rcs[] = "$Id: gateway.c,v 1.70 2011/02/19 13:55:57 fabiankeil Exp $";
+const char gateway_rcs[] = "$Id: gateway.c,v 1.86 2012/03/09 17:55:50 fabiankeil Exp $";
/*********************************************************************
*
* File : $Source: /cvsroot/ijbswa/current/gateway.c,v $
#include "miscutil.h"
#ifdef FEATURE_CONNECTION_KEEP_ALIVE
#ifdef HAVE_POLL
-#ifdef __GLIBC__
+#ifdef __GLIBC__
#include <sys/poll.h>
#else
#include <poll.h>
sfd, reusable_connection[slot].host,
reusable_connection[slot].port, slot);
mark_connection_closed(&reusable_connection[slot]);
- privoxy_mutex_unlock(&connection_reuse_mutex);
-
- return;
+ break;
}
}
- log_error(LOG_LEVEL_CONNECT,
- "Socket %d already forgotten or never remembered.", sfd);
-
privoxy_mutex_unlock(&connection_reuse_mutex);
+
}
#endif /* def FEATURE_CONNECTION_SHARING */
default:
/* Should never get here */
log_error(LOG_LEVEL_FATAL,
- "SOCKS4 impossible internal error - bad SOCKS type.");
+ "Internal error in forwarded_connect(). Bad proxy type: %d", fwd->type);
}
if (JB_INVALID_SOCKET != sfd)
if (err)
{
log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
- csp->error_message = strdup(errstr);
+ csp->error_message = strdup(errstr);
errno = EINVAL;
return(JB_INVALID_SOCKET);
}
c->vn = 4;
c->cd = 1;
- c->dstport[0] = (unsigned char)((target_port >> 8 ) & 0xff);
- c->dstport[1] = (unsigned char)((target_port ) & 0xff);
- c->dstip[0] = (unsigned char)((web_server_addr >> 24 ) & 0xff);
- c->dstip[1] = (unsigned char)((web_server_addr >> 16 ) & 0xff);
- c->dstip[2] = (unsigned char)((web_server_addr >> 8 ) & 0xff);
- c->dstip[3] = (unsigned char)((web_server_addr ) & 0xff);
+ c->dstport[0] = (unsigned char)((target_port >> 8 ) & 0xff);
+ c->dstport[1] = (unsigned char)((target_port ) & 0xff);
+ c->dstip[0] = (unsigned char)((web_server_addr >> 24) & 0xff);
+ c->dstip[1] = (unsigned char)((web_server_addr >> 16) & 0xff);
+ c->dstip[2] = (unsigned char)((web_server_addr >> 8) & 0xff);
+ c->dstip[3] = (unsigned char)((web_server_addr ) & 0xff);
/* pass the request to the socks server */
sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
if (sfd == JB_INVALID_SOCKET)
{
- /*
- * XXX: connect_to should fill in the exact reason.
- * Most likely resolving the IP of the forwarder failed.
- */
- errstr = "connect_to failed: see logfile for details";
+ /* The error an its reason have already been logged by connect_to() */
+ return(JB_INVALID_SOCKET);
+ }
+ else if (write_socket(sfd, (char *)c, csiz))
+ {
+ errstr = "SOCKS4 negotiation write failed.";
+ log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
err = 1;
+ close_socket(sfd);
}
else if (!data_is_available(sfd, csp->config->socket_timeout))
{
err = 1;
close_socket(sfd);
}
- else if (write_socket(sfd, (char *)c, csiz))
- {
- errstr = "SOCKS4 negotiation write failed.";
- log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
- err = 1;
- close_socket(sfd);
- }
else if (read_socket(sfd, buf, sizeof(buf)) != sizeof(*s))
{
errstr = "SOCKS4 negotiation read failed.";
if (err)
{
- csp->error_message = strdup(errstr);
+ csp->error_message = strdup(errstr);
return(JB_INVALID_SOCKET);
}
{
int err = 0;
char cbuf[300];
- char sbuf[30];
+ char sbuf[10];
size_t client_pos = 0;
int server_size = 0;
size_t hostlen = 0;
{
errstr = "socks5 server unreachable";
log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
+ /* Free the generic error message provided by connect_to() */
+ freez(csp->error_message);
csp->error_message = strdup(errstr);
return(JB_INVALID_SOCKET);
}
if (write_socket(sfd, cbuf, client_pos))
{
- errstr = "SOCKS5 negotiation read failed";
+ errstr = "SOCKS5 negotiation write failed";
csp->error_message = strdup(errstr);
log_error(LOG_LEVEL_CONNECT, "%s", errstr);
close_socket(sfd);
}
server_size = read_socket(sfd, sbuf, sizeof(sbuf));
- if (server_size < 3)
+ if (server_size != sizeof(sbuf))
{
errstr = "SOCKS5 negotiation read failed";
- err = 1;
- }
- else if (server_size > 20)
- {
- /* This is somewhat unexpected but doesn't realy matter. */
- log_error(LOG_LEVEL_CONNECT, "socks5_connect: read %d bytes "
- "from socks server. Would have accepted up to %d.",
- server_size, sizeof(sbuf));
- }
-
- if (!err && (sbuf[0] != '\x05'))
- {
- errstr = "SOCKS5 negotiation protocol version error";
- err = 1;
}
-
- if (!err && (sbuf[2] != '\x00'))
- {
- errstr = "SOCKS5 negotiation protocol error";
- err = 1;
- }
-
- if (!err)
+ else
{
- if (sbuf[1] == SOCKS5_REQUEST_GRANTED)
+ if (sbuf[0] != '\x05')
+ {
+ errstr = "SOCKS5 negotiation protocol version error";
+ }
+ else if (sbuf[2] != '\x00')
+ {
+ errstr = "SOCKS5 negotiation protocol error";
+ }
+ else if (sbuf[1] != SOCKS5_REQUEST_GRANTED)
+ {
+ errstr = translate_socks5_error(sbuf[1]);
+ }
+ else
{
return(sfd);
}
- errstr = translate_socks5_error(sbuf[1]);
}
assert(errstr != NULL);