1 const char gateway_rcs[] = "$Id: gateway.c,v 1.68 2011/02/14 16:03:03 fabiankeil 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-2009 the
11 * Privoxy team. http://www.privoxy.org/
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.
35 *********************************************************************/
41 #include <sys/types.h>
44 #include <netinet/in.h>
53 #endif /* def _WIN32 */
57 #endif /* def __BEOS__ */
61 #endif /* def __OS2__ */
66 #include "jbsockets.h"
69 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
75 #endif /* def __GLIBC__ */
76 #endif /* HAVE_POLL */
77 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
79 const char gateway_h_rcs[] = GATEWAY_H_VERSION;
81 static jb_socket socks4_connect(const struct forward_spec * fwd,
82 const char * target_host,
84 struct client_state *csp);
86 static jb_socket socks5_connect(const struct forward_spec *fwd,
87 const char *target_host,
89 struct client_state *csp);
92 #define SOCKS_REQUEST_GRANTED 90
93 #define SOCKS_REQUEST_REJECT 91
94 #define SOCKS_REQUEST_IDENT_FAILED 92
95 #define SOCKS_REQUEST_IDENT_CONFLICT 93
98 SOCKS5_REQUEST_GRANTED = 0,
99 SOCKS5_REQUEST_FAILED = 1,
100 SOCKS5_REQUEST_DENIED = 2,
101 SOCKS5_REQUEST_NETWORK_UNREACHABLE = 3,
102 SOCKS5_REQUEST_HOST_UNREACHABLE = 4,
103 SOCKS5_REQUEST_CONNECTION_REFUSED = 5,
104 SOCKS5_REQUEST_TTL_EXPIRED = 6,
105 SOCKS5_REQUEST_PROTOCOL_ERROR = 7,
106 SOCKS5_REQUEST_BAD_ADDRESS_TYPE = 8
109 /* structure of a socks client operation */
111 unsigned char vn; /* socks version number */
112 unsigned char cd; /* command code */
113 unsigned char dstport[2]; /* destination port */
114 unsigned char dstip[4]; /* destination address */
115 char userid; /* first byte of userid */
116 char padding[3]; /* make sure sizeof(struct socks_op) is endian-independent. */
117 /* more bytes of the userid follow, terminated by a NULL */
120 /* structure of a socks server reply */
122 unsigned char vn; /* socks version number */
123 unsigned char cd; /* command code */
124 unsigned char dstport[2]; /* destination port */
125 unsigned char dstip[4]; /* destination address */
128 static const char socks_userid[] = "anonymous";
130 #ifdef FEATURE_CONNECTION_SHARING
132 #define MAX_REUSABLE_CONNECTIONS 100
133 static unsigned int keep_alive_timeout = DEFAULT_KEEP_ALIVE_TIMEOUT;
135 static struct reusable_connection reusable_connection[MAX_REUSABLE_CONNECTIONS];
136 static int mark_connection_unused(const struct reusable_connection *connection);
138 /*********************************************************************
140 * Function : initialize_reusable_connections
142 * Description : Initializes the reusable_connection structures.
143 * Must be called with connection_reuse_mutex locked.
149 *********************************************************************/
150 extern void initialize_reusable_connections(void)
152 unsigned int slot = 0;
154 #if !defined(HAVE_POLL) && !defined(_WIN32)
155 log_error(LOG_LEVEL_INFO,
156 "Detecting already dead connections might not work "
157 "correctly on your platform. In case of problems, "
158 "unset the keep-alive-timeout option.");
161 for (slot = 0; slot < SZ(reusable_connection); slot++)
163 mark_connection_closed(&reusable_connection[slot]);
166 log_error(LOG_LEVEL_CONNECT, "Initialized %d socket slots.", slot);
170 /*********************************************************************
172 * Function : remember_connection
174 * Description : Remembers a server connection for reuse later on.
177 * 1 : connection = The server connection to remember.
181 *********************************************************************/
182 void remember_connection(const struct reusable_connection *connection)
184 unsigned int slot = 0;
185 int free_slot_found = FALSE;
187 assert(NULL != connection);
188 assert(connection->sfd != JB_INVALID_SOCKET);
190 if (mark_connection_unused(connection))
195 privoxy_mutex_lock(&connection_reuse_mutex);
197 /* Find free socket slot. */
198 for (slot = 0; slot < SZ(reusable_connection); slot++)
200 if (reusable_connection[slot].sfd == JB_INVALID_SOCKET)
202 assert(reusable_connection[slot].in_use == 0);
203 log_error(LOG_LEVEL_CONNECT,
204 "Remembering socket %d for %s:%d in slot %d.",
205 connection->sfd, connection->host, connection->port, slot);
206 free_slot_found = TRUE;
211 if (!free_slot_found)
213 log_error(LOG_LEVEL_CONNECT,
214 "No free slots found to remembering socket for %s:%d. Last slot %d.",
215 connection->host, connection->port, slot);
216 privoxy_mutex_unlock(&connection_reuse_mutex);
217 close_socket(connection->sfd);
221 assert(NULL != connection->host);
222 reusable_connection[slot].host = strdup(connection->host);
223 if (NULL == reusable_connection[slot].host)
225 log_error(LOG_LEVEL_FATAL, "Out of memory saving socket.");
227 reusable_connection[slot].sfd = connection->sfd;
228 reusable_connection[slot].port = connection->port;
229 reusable_connection[slot].in_use = 0;
230 reusable_connection[slot].timestamp = connection->timestamp;
231 reusable_connection->request_sent = connection->request_sent;
232 reusable_connection->response_received = connection->response_received;
233 reusable_connection[slot].keep_alive_timeout = connection->keep_alive_timeout;
235 assert(reusable_connection[slot].gateway_host == NULL);
236 assert(reusable_connection[slot].gateway_port == 0);
237 assert(reusable_connection[slot].forwarder_type == SOCKS_NONE);
238 assert(reusable_connection[slot].forward_host == NULL);
239 assert(reusable_connection[slot].forward_port == 0);
241 reusable_connection[slot].forwarder_type = connection->forwarder_type;
242 if (NULL != connection->gateway_host)
244 reusable_connection[slot].gateway_host = strdup(connection->gateway_host);
245 if (NULL == reusable_connection[slot].gateway_host)
247 log_error(LOG_LEVEL_FATAL, "Out of memory saving gateway_host.");
252 reusable_connection[slot].gateway_host = NULL;
254 reusable_connection[slot].gateway_port = connection->gateway_port;
256 if (NULL != connection->forward_host)
258 reusable_connection[slot].forward_host = strdup(connection->forward_host);
259 if (NULL == reusable_connection[slot].forward_host)
261 log_error(LOG_LEVEL_FATAL, "Out of memory saving forward_host.");
266 reusable_connection[slot].forward_host = NULL;
268 reusable_connection[slot].forward_port = connection->forward_port;
270 privoxy_mutex_unlock(&connection_reuse_mutex);
272 #endif /* def FEATURE_CONNECTION_SHARING */
275 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
276 /*********************************************************************
278 * Function : mark_connection_closed
280 * Description : Marks a reused connection closed.
283 * 1 : closed_connection = The connection to mark as closed.
287 *********************************************************************/
288 void mark_connection_closed(struct reusable_connection *closed_connection)
290 closed_connection->in_use = FALSE;
291 closed_connection->sfd = JB_INVALID_SOCKET;
292 freez(closed_connection->host);
293 closed_connection->port = 0;
294 closed_connection->timestamp = 0;
295 closed_connection->request_sent = 0;
296 closed_connection->response_received = 0;
297 closed_connection->keep_alive_timeout = 0;
298 closed_connection->forwarder_type = SOCKS_NONE;
299 freez(closed_connection->gateway_host);
300 closed_connection->gateway_port = 0;
301 freez(closed_connection->forward_host);
302 closed_connection->forward_port = 0;
304 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
307 #ifdef FEATURE_CONNECTION_SHARING
308 /*********************************************************************
310 * Function : forget_connection
312 * Description : Removes a previously remembered connection from
313 * the list of reusable connections.
316 * 1 : sfd = The socket belonging to the connection in question.
320 *********************************************************************/
321 void forget_connection(jb_socket sfd)
323 unsigned int slot = 0;
325 assert(sfd != JB_INVALID_SOCKET);
327 privoxy_mutex_lock(&connection_reuse_mutex);
329 for (slot = 0; slot < SZ(reusable_connection); slot++)
331 if (reusable_connection[slot].sfd == sfd)
333 assert(reusable_connection[slot].in_use);
335 log_error(LOG_LEVEL_CONNECT,
336 "Forgetting socket %d for %s:%d in slot %d.",
337 sfd, reusable_connection[slot].host,
338 reusable_connection[slot].port, slot);
339 mark_connection_closed(&reusable_connection[slot]);
340 privoxy_mutex_unlock(&connection_reuse_mutex);
346 log_error(LOG_LEVEL_CONNECT,
347 "Socket %d already forgotten or never remembered.", sfd);
349 privoxy_mutex_unlock(&connection_reuse_mutex);
351 #endif /* def FEATURE_CONNECTION_SHARING */
354 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
355 /*********************************************************************
357 * Function : connection_destination_matches
359 * Description : Determines whether a remembered connection can
360 * be reused. That is, whether the destination and
361 * the forwarding settings match.
364 * 1 : connection = The connection to check.
365 * 2 : http = The destination for the connection.
366 * 3 : fwd = The forwarder settings.
368 * Returns : TRUE for yes, FALSE otherwise.
370 *********************************************************************/
371 int connection_destination_matches(const struct reusable_connection *connection,
372 const struct http_request *http,
373 const struct forward_spec *fwd)
375 if ((connection->forwarder_type != fwd->type)
376 || (connection->gateway_port != fwd->gateway_port)
377 || (connection->forward_port != fwd->forward_port)
378 || (connection->port != http->port))
383 if (( (NULL != connection->gateway_host)
384 && (NULL != fwd->gateway_host)
385 && strcmpic(connection->gateway_host, fwd->gateway_host))
386 && (connection->gateway_host != fwd->gateway_host))
388 log_error(LOG_LEVEL_CONNECT,
389 "Gateway mismatch. Previous gateway: %s. Current gateway: %s",
390 connection->gateway_host, fwd->gateway_host);
394 if (( (NULL != connection->forward_host)
395 && (NULL != fwd->forward_host)
396 && strcmpic(connection->forward_host, fwd->forward_host))
397 && (connection->forward_host != fwd->forward_host))
399 log_error(LOG_LEVEL_CONNECT,
400 "Forwarding proxy mismatch. Previous proxy: %s. Current proxy: %s",
401 connection->forward_host, fwd->forward_host);
405 return (!strcmpic(connection->host, http->host));
408 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
411 #ifdef FEATURE_CONNECTION_SHARING
412 /*********************************************************************
414 * Function : close_unusable_connections
416 * Description : Closes remembered connections that have timed
417 * out or have been closed on the other side.
421 * Returns : Number of connections that are still alive.
423 *********************************************************************/
424 int close_unusable_connections(void)
426 unsigned int slot = 0;
427 int connections_alive = 0;
429 privoxy_mutex_lock(&connection_reuse_mutex);
431 for (slot = 0; slot < SZ(reusable_connection); slot++)
433 if (!reusable_connection[slot].in_use
434 && (JB_INVALID_SOCKET != reusable_connection[slot].sfd))
436 time_t time_open = time(NULL) - reusable_connection[slot].timestamp;
437 time_t latency = (reusable_connection[slot].response_received -
438 reusable_connection[slot].request_sent) / 2;
440 if (reusable_connection[slot].keep_alive_timeout < time_open + latency)
442 log_error(LOG_LEVEL_CONNECT,
443 "The connection to %s:%d in slot %d timed out. "
444 "Closing socket %d. Timeout is: %d. Assumed latency: %d.",
445 reusable_connection[slot].host,
446 reusable_connection[slot].port, slot,
447 reusable_connection[slot].sfd,
448 reusable_connection[slot].keep_alive_timeout,
450 close_socket(reusable_connection[slot].sfd);
451 mark_connection_closed(&reusable_connection[slot]);
453 else if (!socket_is_still_alive(reusable_connection[slot].sfd))
455 log_error(LOG_LEVEL_CONNECT,
456 "The connection to %s:%d in slot %d is no longer usable. "
457 "Closing socket %d.", reusable_connection[slot].host,
458 reusable_connection[slot].port, slot,
459 reusable_connection[slot].sfd);
460 close_socket(reusable_connection[slot].sfd);
461 mark_connection_closed(&reusable_connection[slot]);
470 privoxy_mutex_unlock(&connection_reuse_mutex);
472 return connections_alive;
477 /*********************************************************************
479 * Function : get_reusable_connection
481 * Description : Returns an open socket to a previously remembered
482 * open connection (if there is one).
485 * 1 : http = The destination for the connection.
486 * 2 : fwd = The forwarder settings.
488 * Returns : JB_INVALID_SOCKET => No reusable connection found,
489 * otherwise a usable socket.
491 *********************************************************************/
492 static jb_socket get_reusable_connection(const struct http_request *http,
493 const struct forward_spec *fwd)
495 jb_socket sfd = JB_INVALID_SOCKET;
496 unsigned int slot = 0;
498 close_unusable_connections();
500 privoxy_mutex_lock(&connection_reuse_mutex);
502 for (slot = 0; slot < SZ(reusable_connection); slot++)
504 if (!reusable_connection[slot].in_use
505 && (JB_INVALID_SOCKET != reusable_connection[slot].sfd))
507 if (connection_destination_matches(&reusable_connection[slot], http, fwd))
509 reusable_connection[slot].in_use = TRUE;
510 sfd = reusable_connection[slot].sfd;
511 log_error(LOG_LEVEL_CONNECT,
512 "Found reusable socket %d for %s:%d in slot %d. "
513 "Timestamp made %d seconds ago. Timeout: %d. Latency: %d.",
514 sfd, reusable_connection[slot].host, reusable_connection[slot].port,
515 slot, time(NULL) - reusable_connection[slot].timestamp,
516 reusable_connection[slot].keep_alive_timeout,
517 (int)(reusable_connection[slot].response_received -
518 reusable_connection[slot].request_sent));
524 privoxy_mutex_unlock(&connection_reuse_mutex);
531 /*********************************************************************
533 * Function : mark_connection_unused
535 * Description : Gives a remembered connection free for reuse.
538 * 1 : connection = The connection in question.
540 * Returns : TRUE => Socket found and marked as unused.
541 * FALSE => Socket not found.
543 *********************************************************************/
544 static int mark_connection_unused(const struct reusable_connection *connection)
546 unsigned int slot = 0;
547 int socket_found = FALSE;
549 assert(connection->sfd != JB_INVALID_SOCKET);
551 privoxy_mutex_lock(&connection_reuse_mutex);
553 for (slot = 0; slot < SZ(reusable_connection); slot++)
555 if (reusable_connection[slot].sfd == connection->sfd)
557 assert(reusable_connection[slot].in_use);
559 log_error(LOG_LEVEL_CONNECT,
560 "Marking open socket %d for %s:%d in slot %d as unused.",
561 connection->sfd, reusable_connection[slot].host,
562 reusable_connection[slot].port, slot);
563 reusable_connection[slot].in_use = 0;
564 reusable_connection[slot].timestamp = connection->timestamp;
569 privoxy_mutex_unlock(&connection_reuse_mutex);
576 /*********************************************************************
578 * Function : set_keep_alive_timeout
580 * Description : Sets the timeout after which open
581 * connections will no longer be reused.
584 * 1 : timeout = The timeout in seconds.
588 *********************************************************************/
589 void set_keep_alive_timeout(unsigned int timeout)
591 keep_alive_timeout = timeout;
593 #endif /* def FEATURE_CONNECTION_SHARING */
596 /*********************************************************************
598 * Function : forwarded_connect
600 * Description : Connect to a specified web server, possibly via
601 * a HTTP proxy and/or a SOCKS proxy.
604 * 1 : fwd = the proxies to use when connecting.
605 * 2 : http = the http request and apropos headers
606 * 3 : csp = Current client state (buffers, headers, etc...)
608 * Returns : JB_INVALID_SOCKET => failure, else it is the socket file descriptor.
610 *********************************************************************/
611 jb_socket forwarded_connect(const struct forward_spec * fwd,
612 struct http_request *http,
613 struct client_state *csp)
615 const char * dest_host;
617 jb_socket sfd = JB_INVALID_SOCKET;
619 #ifdef FEATURE_CONNECTION_SHARING
620 if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING)
621 && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED))
623 sfd = get_reusable_connection(http, fwd);
624 if (JB_INVALID_SOCKET != sfd)
629 #endif /* def FEATURE_CONNECTION_SHARING */
631 /* Figure out if we need to connect to the web server or a HTTP proxy. */
632 if (fwd->forward_host)
635 dest_host = fwd->forward_host;
636 dest_port = fwd->forward_port;
641 dest_host = http->host;
642 dest_port = http->port;
645 /* Connect, maybe using a SOCKS proxy */
649 sfd = connect_to(dest_host, dest_port, csp);
653 sfd = socks4_connect(fwd, dest_host, dest_port, csp);
656 sfd = socks5_connect(fwd, dest_host, dest_port, csp);
659 /* Should never get here */
660 log_error(LOG_LEVEL_FATAL,
661 "SOCKS4 impossible internal error - bad SOCKS type.");
664 if (JB_INVALID_SOCKET != sfd)
666 log_error(LOG_LEVEL_CONNECT,
667 "Created new connection to %s:%d on socket %d.",
668 http->host, http->port, sfd);
676 /*********************************************************************
678 * Function : socks4_connect
680 * Description : Connect to the SOCKS server, and connect through
681 * it to the specified server. This handles
682 * all the SOCKS negotiation, and returns a file
683 * descriptor for a socket which can be treated as a
684 * normal (non-SOCKS) socket.
686 * Logged error messages are saved to csp->error_message
687 * and later reused by error_response() for the CGI
688 * message. strdup allocation failures are handled there.
691 * 1 : fwd = Specifies the SOCKS proxy to use.
692 * 2 : target_host = The final server to connect to.
693 * 3 : target_port = The final port to connect to.
694 * 4 : csp = Current client state (buffers, headers, etc...)
696 * Returns : JB_INVALID_SOCKET => failure, else a socket file descriptor.
698 *********************************************************************/
699 static jb_socket socks4_connect(const struct forward_spec * fwd,
700 const char * target_host,
702 struct client_state *csp)
704 unsigned long web_server_addr;
705 char buf[BUFFER_SIZE];
706 struct socks_op *c = (struct socks_op *)buf;
707 struct socks_reply *s = (struct socks_reply *)buf;
714 if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
716 /* XXX: Shouldn't the config file parser prevent this? */
717 errstr = "NULL gateway host specified.";
721 if (fwd->gateway_port <= 0)
723 errstr = "invalid gateway port specified.";
729 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
730 csp->error_message = strdup(errstr);
732 return(JB_INVALID_SOCKET);
735 /* build a socks request for connection to the web server */
737 strlcpy(&(c->userid), socks_userid, sizeof(buf) - sizeof(struct socks_op));
739 csiz = sizeof(*c) + sizeof(socks_userid) - sizeof(c->userid) - sizeof(c->padding);
744 web_server_addr = resolve_hostname_to_ip(target_host);
745 if (web_server_addr == INADDR_NONE)
747 errstr = "could not resolve target host";
748 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s %s", errstr, target_host);
753 web_server_addr = htonl(web_server_addr);
757 web_server_addr = 0x00000001;
758 n = csiz + strlen(target_host) + 1;
762 errstr = "buffer cbuf too small.";
763 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
768 strlcpy(buf + csiz, target_host, sizeof(buf) - sizeof(struct socks_op) - csiz);
770 * What we forward to the socks4a server should have the
771 * size of socks_op, plus the length of the userid plus
772 * its \0 byte (which we don't have to add because the
773 * first byte of the userid is counted twice as it's also
774 * part of sock_op) minus the padding bytes (which are part
775 * of the userid as well), plus the length of the target_host
776 * (which is stored csiz bytes after the beginning of the buffer),
777 * plus another \0 byte.
779 assert(n == sizeof(struct socks_op) + strlen(&(c->userid)) - sizeof(c->padding) + strlen(buf + csiz) + 1);
784 /* Should never get here */
785 log_error(LOG_LEVEL_FATAL,
786 "socks4_connect: SOCKS4 impossible internal error - bad SOCKS type.");
788 return(JB_INVALID_SOCKET);
793 csp->error_message = strdup(errstr);
794 return(JB_INVALID_SOCKET);
799 c->dstport[0] = (unsigned char)((target_port >> 8 ) & 0xff);
800 c->dstport[1] = (unsigned char)((target_port ) & 0xff);
801 c->dstip[0] = (unsigned char)((web_server_addr >> 24 ) & 0xff);
802 c->dstip[1] = (unsigned char)((web_server_addr >> 16 ) & 0xff);
803 c->dstip[2] = (unsigned char)((web_server_addr >> 8 ) & 0xff);
804 c->dstip[3] = (unsigned char)((web_server_addr ) & 0xff);
806 /* pass the request to the socks server */
807 sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
809 if (sfd == JB_INVALID_SOCKET)
812 * XXX: connect_to should fill in the exact reason.
813 * Most likely resolving the IP of the forwarder failed.
815 errstr = "connect_to failed: see logfile for details";
818 else if (!data_is_available(sfd, csp->config->socket_timeout))
820 if (socket_is_still_alive(sfd))
822 errstr = "SOCKS4 negotiation timed out";
826 errstr = "SOCKS4 negotiation got aborted by the server";
828 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
832 else if (write_socket(sfd, (char *)c, csiz))
834 errstr = "SOCKS4 negotiation write failed.";
835 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
839 else if (read_socket(sfd, buf, sizeof(buf)) != sizeof(*s))
841 errstr = "SOCKS4 negotiation read failed.";
842 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
849 csp->error_message = strdup(errstr);
850 return(JB_INVALID_SOCKET);
855 case SOCKS_REQUEST_GRANTED:
857 case SOCKS_REQUEST_REJECT:
858 errstr = "SOCKS request rejected or failed.";
861 case SOCKS_REQUEST_IDENT_FAILED:
862 errstr = "SOCKS request rejected because "
863 "SOCKS server cannot connect to identd on the client.";
866 case SOCKS_REQUEST_IDENT_CONFLICT:
867 errstr = "SOCKS request rejected because "
868 "the client program and identd report "
869 "different user-ids.";
874 snprintf(buf, sizeof(buf),
875 "SOCKS request rejected for reason code %d.", s->cd);
879 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
880 csp->error_message = strdup(errstr);
883 return(JB_INVALID_SOCKET);
887 /*********************************************************************
889 * Function : translate_socks5_error
891 * Description : Translates a SOCKS errors to a string.
894 * 1 : socks_error = The error code to translate.
896 * Returns : The string translation.
898 *********************************************************************/
899 static const char *translate_socks5_error(int socks_error)
903 /* XXX: these should be more descriptive */
904 case SOCKS5_REQUEST_FAILED:
905 return "SOCKS5 request failed";
906 case SOCKS5_REQUEST_DENIED:
907 return "SOCKS5 request denied";
908 case SOCKS5_REQUEST_NETWORK_UNREACHABLE:
909 return "SOCKS5 network unreachable";
910 case SOCKS5_REQUEST_HOST_UNREACHABLE:
911 return "SOCKS5 host unreachable";
912 case SOCKS5_REQUEST_CONNECTION_REFUSED:
913 return "SOCKS5 connection refused";
914 case SOCKS5_REQUEST_TTL_EXPIRED:
915 return "SOCKS5 TTL expired";
916 case SOCKS5_REQUEST_PROTOCOL_ERROR:
917 return "SOCKS5 client protocol error";
918 case SOCKS5_REQUEST_BAD_ADDRESS_TYPE:
919 return "SOCKS5 domain names unsupported";
920 case SOCKS5_REQUEST_GRANTED:
921 return "everything's peachy";
923 return "SOCKS5 negotiation protocol error";
927 /*********************************************************************
929 * Function : socks5_connect
931 * Description : Connect to the SOCKS server, and connect through
932 * it to the specified server. This handles
933 * all the SOCKS negotiation, and returns a file
934 * descriptor for a socket which can be treated as a
935 * normal (non-SOCKS) socket.
938 * 1 : fwd = Specifies the SOCKS proxy to use.
939 * 2 : target_host = The final server to connect to.
940 * 3 : target_port = The final port to connect to.
941 * 4 : csp = Current client state (buffers, headers, etc...)
943 * Returns : JB_INVALID_SOCKET => failure, else a socket file descriptor.
945 *********************************************************************/
946 static jb_socket socks5_connect(const struct forward_spec *fwd,
947 const char *target_host,
949 struct client_state *csp)
954 size_t client_pos = 0;
958 const char *errstr = NULL;
960 assert(fwd->gateway_host);
961 if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
963 errstr = "NULL gateway host specified";
967 if (fwd->gateway_port <= 0)
970 * XXX: currently this can't happen because in
971 * case of invalid gateway ports we use the defaults.
972 * Of course we really shouldn't do that.
974 errstr = "invalid gateway port specified";
978 hostlen = strlen(target_host);
979 if (hostlen > (size_t)255)
981 errstr = "target host name is longer than 255 characters";
985 if (fwd->type != SOCKS_5)
987 /* Should never get here */
988 log_error(LOG_LEVEL_FATAL,
989 "SOCKS5 impossible internal error - bad SOCKS type");
996 assert(errstr != NULL);
997 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
998 csp->error_message = strdup(errstr);
999 return(JB_INVALID_SOCKET);
1002 /* pass the request to the socks server */
1003 sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
1005 if (sfd == JB_INVALID_SOCKET)
1007 errstr = "socks5 server unreachable";
1008 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1009 csp->error_message = strdup(errstr);
1010 return(JB_INVALID_SOCKET);
1014 cbuf[client_pos++] = '\x05'; /* Version */
1015 cbuf[client_pos++] = '\x01'; /* One authentication method supported */
1016 cbuf[client_pos++] = '\x00'; /* The no authentication authentication method */
1018 if (write_socket(sfd, cbuf, client_pos))
1020 errstr = "SOCKS5 negotiation write failed";
1021 csp->error_message = strdup(errstr);
1022 log_error(LOG_LEVEL_CONNECT, "%s", errstr);
1024 return(JB_INVALID_SOCKET);
1027 if (!data_is_available(sfd, csp->config->socket_timeout))
1029 if (socket_is_still_alive(sfd))
1031 errstr = "SOCKS5 negotiation timed out";
1035 errstr = "SOCKS5 negotiation got aborted by the server";
1040 if (!err && read_socket(sfd, sbuf, sizeof(sbuf)) != 2)
1042 errstr = "SOCKS5 negotiation read failed";
1046 if (!err && (sbuf[0] != '\x05'))
1048 errstr = "SOCKS5 negotiation protocol version error";
1052 if (!err && (sbuf[1] == '\xff'))
1054 errstr = "SOCKS5 authentication required";
1058 if (!err && (sbuf[1] != '\x00'))
1060 errstr = "SOCKS5 negotiation protocol error";
1066 assert(errstr != NULL);
1067 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1068 csp->error_message = strdup(errstr);
1071 return(JB_INVALID_SOCKET);
1075 cbuf[client_pos++] = '\x05'; /* Version */
1076 cbuf[client_pos++] = '\x01'; /* TCP connect */
1077 cbuf[client_pos++] = '\x00'; /* Reserved, must be 0x00 */
1078 cbuf[client_pos++] = '\x03'; /* Address is domain name */
1079 cbuf[client_pos++] = (char)(hostlen & 0xffu);
1080 assert(sizeof(cbuf) - client_pos > (size_t)255);
1081 /* Using strncpy because we really want the nul byte padding. */
1082 strncpy(cbuf + client_pos, target_host, sizeof(cbuf) - client_pos);
1083 client_pos += (hostlen & 0xffu);
1084 cbuf[client_pos++] = (char)((target_port >> 8) & 0xff);
1085 cbuf[client_pos++] = (char)((target_port ) & 0xff);
1087 if (write_socket(sfd, cbuf, client_pos))
1089 errstr = "SOCKS5 negotiation read failed";
1090 csp->error_message = strdup(errstr);
1091 log_error(LOG_LEVEL_CONNECT, "%s", errstr);
1094 return(JB_INVALID_SOCKET);
1097 server_size = read_socket(sfd, sbuf, sizeof(sbuf));
1098 if (server_size < 3)
1100 errstr = "SOCKS5 negotiation read failed";
1103 else if (server_size > 20)
1105 /* This is somewhat unexpected but doesn't realy matter. */
1106 log_error(LOG_LEVEL_CONNECT, "socks5_connect: read %d bytes "
1107 "from socks server. Would have accepted up to %d.",
1108 server_size, sizeof(sbuf));
1111 if (!err && (sbuf[0] != '\x05'))
1113 errstr = "SOCKS5 negotiation protocol version error";
1117 if (!err && (sbuf[2] != '\x00'))
1119 errstr = "SOCKS5 negotiation protocol error";
1125 if (sbuf[1] == SOCKS5_REQUEST_GRANTED)
1129 errstr = translate_socks5_error(sbuf[1]);
1132 assert(errstr != NULL);
1133 csp->error_message = strdup(errstr);
1134 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1138 return(JB_INVALID_SOCKET);