1 const char gateway_rcs[] = "$Id: gateway.c,v 1.65 2010/04/23 11:53:48 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
97 #define SOCKS5_REQUEST_GRANTED 0
98 #define SOCKS5_REQUEST_FAILED 1
99 #define SOCKS5_REQUEST_DENIED 2
100 #define SOCKS5_REQUEST_NETWORK_UNREACHABLE 3
101 #define SOCKS5_REQUEST_HOST_UNREACHABLE 4
102 #define SOCKS5_REQUEST_CONNECTION_REFUSED 5
103 #define SOCKS5_REQUEST_TTL_EXPIRED 6
104 #define SOCKS5_REQUEST_PROTOCOL_ERROR 7
105 #define SOCKS5_REQUEST_BAD_ADDRESS_TYPE 8
107 /* structure of a socks client operation */
109 unsigned char vn; /* socks version number */
110 unsigned char cd; /* command code */
111 unsigned char dstport[2]; /* destination port */
112 unsigned char dstip[4]; /* destination address */
113 char userid; /* first byte of userid */
114 char padding[3]; /* make sure sizeof(struct socks_op) is endian-independent. */
115 /* more bytes of the userid follow, terminated by a NULL */
118 /* structure of a socks server reply */
120 unsigned char vn; /* socks version number */
121 unsigned char cd; /* command code */
122 unsigned char dstport[2]; /* destination port */
123 unsigned char dstip[4]; /* destination address */
126 static const char socks_userid[] = "anonymous";
128 #ifdef FEATURE_CONNECTION_SHARING
130 #define MAX_REUSABLE_CONNECTIONS 100
131 static unsigned int keep_alive_timeout = DEFAULT_KEEP_ALIVE_TIMEOUT;
133 static struct reusable_connection reusable_connection[MAX_REUSABLE_CONNECTIONS];
134 static int mark_connection_unused(const struct reusable_connection *connection);
136 /*********************************************************************
138 * Function : initialize_reusable_connections
140 * Description : Initializes the reusable_connection structures.
141 * Must be called with connection_reuse_mutex locked.
147 *********************************************************************/
148 extern void initialize_reusable_connections(void)
150 unsigned int slot = 0;
152 #if !defined(HAVE_POLL) && !defined(_WIN32)
153 log_error(LOG_LEVEL_INFO,
154 "Detecting already dead connections might not work "
155 "correctly on your platform. In case of problems, "
156 "unset the keep-alive-timeout option.");
159 for (slot = 0; slot < SZ(reusable_connection); slot++)
161 mark_connection_closed(&reusable_connection[slot]);
164 log_error(LOG_LEVEL_CONNECT, "Initialized %d socket slots.", slot);
168 /*********************************************************************
170 * Function : remember_connection
172 * Description : Remembers a server connection for reuse later on.
175 * 1 : connection = The server connection to remember.
179 *********************************************************************/
180 void remember_connection(const struct reusable_connection *connection)
182 unsigned int slot = 0;
183 int free_slot_found = FALSE;
185 assert(NULL != connection);
186 assert(connection->sfd != JB_INVALID_SOCKET);
188 if (mark_connection_unused(connection))
193 privoxy_mutex_lock(&connection_reuse_mutex);
195 /* Find free socket slot. */
196 for (slot = 0; slot < SZ(reusable_connection); slot++)
198 if (reusable_connection[slot].sfd == JB_INVALID_SOCKET)
200 assert(reusable_connection[slot].in_use == 0);
201 log_error(LOG_LEVEL_CONNECT,
202 "Remembering socket %d for %s:%d in slot %d.",
203 connection->sfd, connection->host, connection->port, slot);
204 free_slot_found = TRUE;
209 if (!free_slot_found)
211 log_error(LOG_LEVEL_CONNECT,
212 "No free slots found to remembering socket for %s:%d. Last slot %d.",
213 connection->host, connection->port, slot);
214 privoxy_mutex_unlock(&connection_reuse_mutex);
215 close_socket(connection->sfd);
219 assert(NULL != connection->host);
220 reusable_connection[slot].host = strdup(connection->host);
221 if (NULL == reusable_connection[slot].host)
223 log_error(LOG_LEVEL_FATAL, "Out of memory saving socket.");
225 reusable_connection[slot].sfd = connection->sfd;
226 reusable_connection[slot].port = connection->port;
227 reusable_connection[slot].in_use = 0;
228 reusable_connection[slot].timestamp = connection->timestamp;
229 reusable_connection->request_sent = connection->request_sent;
230 reusable_connection->response_received = connection->response_received;
231 reusable_connection[slot].keep_alive_timeout = connection->keep_alive_timeout;
233 assert(reusable_connection[slot].gateway_host == NULL);
234 assert(reusable_connection[slot].gateway_port == 0);
235 assert(reusable_connection[slot].forwarder_type == SOCKS_NONE);
236 assert(reusable_connection[slot].forward_host == NULL);
237 assert(reusable_connection[slot].forward_port == 0);
239 reusable_connection[slot].forwarder_type = connection->forwarder_type;
240 if (NULL != connection->gateway_host)
242 reusable_connection[slot].gateway_host = strdup(connection->gateway_host);
243 if (NULL == reusable_connection[slot].gateway_host)
245 log_error(LOG_LEVEL_FATAL, "Out of memory saving gateway_host.");
250 reusable_connection[slot].gateway_host = NULL;
252 reusable_connection[slot].gateway_port = connection->gateway_port;
254 if (NULL != connection->forward_host)
256 reusable_connection[slot].forward_host = strdup(connection->forward_host);
257 if (NULL == reusable_connection[slot].forward_host)
259 log_error(LOG_LEVEL_FATAL, "Out of memory saving forward_host.");
264 reusable_connection[slot].forward_host = NULL;
266 reusable_connection[slot].forward_port = connection->forward_port;
268 privoxy_mutex_unlock(&connection_reuse_mutex);
270 #endif /* def FEATURE_CONNECTION_SHARING */
273 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
274 /*********************************************************************
276 * Function : mark_connection_closed
278 * Description : Marks a reused connection closed.
281 * 1 : closed_connection = The connection to mark as closed.
285 *********************************************************************/
286 void mark_connection_closed(struct reusable_connection *closed_connection)
288 closed_connection->in_use = FALSE;
289 closed_connection->sfd = JB_INVALID_SOCKET;
290 freez(closed_connection->host);
291 closed_connection->port = 0;
292 closed_connection->timestamp = 0;
293 closed_connection->request_sent = 0;
294 closed_connection->response_received = 0;
295 closed_connection->keep_alive_timeout = 0;
296 closed_connection->forwarder_type = SOCKS_NONE;
297 freez(closed_connection->gateway_host);
298 closed_connection->gateway_port = 0;
299 freez(closed_connection->forward_host);
300 closed_connection->forward_port = 0;
302 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
305 #ifdef FEATURE_CONNECTION_SHARING
306 /*********************************************************************
308 * Function : forget_connection
310 * Description : Removes a previously remembered connection from
311 * the list of reusable connections.
314 * 1 : sfd = The socket belonging to the connection in question.
318 *********************************************************************/
319 void forget_connection(jb_socket sfd)
321 unsigned int slot = 0;
323 assert(sfd != JB_INVALID_SOCKET);
325 privoxy_mutex_lock(&connection_reuse_mutex);
327 for (slot = 0; slot < SZ(reusable_connection); slot++)
329 if (reusable_connection[slot].sfd == sfd)
331 assert(reusable_connection[slot].in_use);
333 log_error(LOG_LEVEL_CONNECT,
334 "Forgetting socket %d for %s:%d in slot %d.",
335 sfd, reusable_connection[slot].host,
336 reusable_connection[slot].port, slot);
337 mark_connection_closed(&reusable_connection[slot]);
338 privoxy_mutex_unlock(&connection_reuse_mutex);
344 log_error(LOG_LEVEL_CONNECT,
345 "Socket %d already forgotten or never remembered.", sfd);
347 privoxy_mutex_unlock(&connection_reuse_mutex);
349 #endif /* def FEATURE_CONNECTION_SHARING */
352 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
353 /*********************************************************************
355 * Function : connection_destination_matches
357 * Description : Determines whether a remembered connection can
358 * be reused. That is, whether the destination and
359 * the forwarding settings match.
362 * 1 : connection = The connection to check.
363 * 2 : http = The destination for the connection.
364 * 3 : fwd = The forwarder settings.
366 * Returns : TRUE for yes, FALSE otherwise.
368 *********************************************************************/
369 int connection_destination_matches(const struct reusable_connection *connection,
370 const struct http_request *http,
371 const struct forward_spec *fwd)
373 if ((connection->forwarder_type != fwd->type)
374 || (connection->gateway_port != fwd->gateway_port)
375 || (connection->forward_port != fwd->forward_port)
376 || (connection->port != http->port))
381 if (( (NULL != connection->gateway_host)
382 && (NULL != fwd->gateway_host)
383 && strcmpic(connection->gateway_host, fwd->gateway_host))
384 && (connection->gateway_host != fwd->gateway_host))
386 log_error(LOG_LEVEL_CONNECT, "Gateway mismatch.");
390 if (( (NULL != connection->forward_host)
391 && (NULL != fwd->forward_host)
392 && strcmpic(connection->forward_host, fwd->forward_host))
393 && (connection->forward_host != fwd->forward_host))
395 log_error(LOG_LEVEL_CONNECT, "Forwarding proxy mismatch.");
399 return (!strcmpic(connection->host, http->host));
402 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
405 #ifdef FEATURE_CONNECTION_SHARING
406 /*********************************************************************
408 * Function : close_unusable_connections
410 * Description : Closes remembered connections that have timed
411 * out or have been closed on the other side.
415 * Returns : Number of connections that are still alive.
417 *********************************************************************/
418 int close_unusable_connections(void)
420 unsigned int slot = 0;
421 int connections_alive = 0;
423 privoxy_mutex_lock(&connection_reuse_mutex);
425 for (slot = 0; slot < SZ(reusable_connection); slot++)
427 if (!reusable_connection[slot].in_use
428 && (JB_INVALID_SOCKET != reusable_connection[slot].sfd))
430 time_t time_open = time(NULL) - reusable_connection[slot].timestamp;
431 time_t latency = (reusable_connection[slot].response_received -
432 reusable_connection[slot].request_sent) / 2;
434 if (reusable_connection[slot].keep_alive_timeout < time_open + latency)
436 log_error(LOG_LEVEL_CONNECT,
437 "The connection to %s:%d in slot %d timed out. "
438 "Closing socket %d. Timeout is: %d. Assumed latency: %d.",
439 reusable_connection[slot].host,
440 reusable_connection[slot].port, slot,
441 reusable_connection[slot].sfd,
442 reusable_connection[slot].keep_alive_timeout,
444 close_socket(reusable_connection[slot].sfd);
445 mark_connection_closed(&reusable_connection[slot]);
447 else if (!socket_is_still_alive(reusable_connection[slot].sfd))
449 log_error(LOG_LEVEL_CONNECT,
450 "The connection to %s:%d in slot %d is no longer usable. "
451 "Closing socket %d.", reusable_connection[slot].host,
452 reusable_connection[slot].port, slot,
453 reusable_connection[slot].sfd);
454 close_socket(reusable_connection[slot].sfd);
455 mark_connection_closed(&reusable_connection[slot]);
464 privoxy_mutex_unlock(&connection_reuse_mutex);
466 return connections_alive;
471 /*********************************************************************
473 * Function : get_reusable_connection
475 * Description : Returns an open socket to a previously remembered
476 * open connection (if there is one).
479 * 1 : http = The destination for the connection.
480 * 2 : fwd = The forwarder settings.
482 * Returns : JB_INVALID_SOCKET => No reusable connection found,
483 * otherwise a usable socket.
485 *********************************************************************/
486 static jb_socket get_reusable_connection(const struct http_request *http,
487 const struct forward_spec *fwd)
489 jb_socket sfd = JB_INVALID_SOCKET;
490 unsigned int slot = 0;
492 close_unusable_connections();
494 privoxy_mutex_lock(&connection_reuse_mutex);
496 for (slot = 0; slot < SZ(reusable_connection); slot++)
498 if (!reusable_connection[slot].in_use
499 && (JB_INVALID_SOCKET != reusable_connection[slot].sfd))
501 if (connection_destination_matches(&reusable_connection[slot], http, fwd))
503 reusable_connection[slot].in_use = TRUE;
504 sfd = reusable_connection[slot].sfd;
505 log_error(LOG_LEVEL_CONNECT,
506 "Found reusable socket %d for %s:%d in slot %d. "
507 "Timestamp made %d seconds ago. Timeout: %d. Latency: %d.",
508 sfd, reusable_connection[slot].host, reusable_connection[slot].port,
509 slot, time(NULL) - reusable_connection[slot].timestamp,
510 reusable_connection[slot].keep_alive_timeout,
511 (int)(reusable_connection[slot].response_received -
512 reusable_connection[slot].request_sent));
518 privoxy_mutex_unlock(&connection_reuse_mutex);
525 /*********************************************************************
527 * Function : mark_connection_unused
529 * Description : Gives a remembered connection free for reuse.
532 * 1 : connection = The connection in question.
534 * Returns : TRUE => Socket found and marked as unused.
535 * FALSE => Socket not found.
537 *********************************************************************/
538 static int mark_connection_unused(const struct reusable_connection *connection)
540 unsigned int slot = 0;
541 int socket_found = FALSE;
543 assert(connection->sfd != JB_INVALID_SOCKET);
545 privoxy_mutex_lock(&connection_reuse_mutex);
547 for (slot = 0; slot < SZ(reusable_connection); slot++)
549 if (reusable_connection[slot].sfd == connection->sfd)
551 assert(reusable_connection[slot].in_use);
553 log_error(LOG_LEVEL_CONNECT,
554 "Marking open socket %d for %s:%d in slot %d as unused.",
555 connection->sfd, reusable_connection[slot].host,
556 reusable_connection[slot].port, slot);
557 reusable_connection[slot].in_use = 0;
558 reusable_connection[slot].timestamp = connection->timestamp;
563 privoxy_mutex_unlock(&connection_reuse_mutex);
570 /*********************************************************************
572 * Function : set_keep_alive_timeout
574 * Description : Sets the timeout after which open
575 * connections will no longer be reused.
578 * 1 : timeout = The timeout in seconds.
582 *********************************************************************/
583 void set_keep_alive_timeout(unsigned int timeout)
585 keep_alive_timeout = timeout;
587 #endif /* def FEATURE_CONNECTION_SHARING */
590 /*********************************************************************
592 * Function : forwarded_connect
594 * Description : Connect to a specified web server, possibly via
595 * a HTTP proxy and/or a SOCKS proxy.
598 * 1 : fwd = the proxies to use when connecting.
599 * 2 : http = the http request and apropos headers
600 * 3 : csp = Current client state (buffers, headers, etc...)
602 * Returns : JB_INVALID_SOCKET => failure, else it is the socket file descriptor.
604 *********************************************************************/
605 jb_socket forwarded_connect(const struct forward_spec * fwd,
606 struct http_request *http,
607 struct client_state *csp)
609 const char * dest_host;
611 jb_socket sfd = JB_INVALID_SOCKET;
613 #ifdef FEATURE_CONNECTION_SHARING
614 if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING)
615 && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED))
617 sfd = get_reusable_connection(http, fwd);
618 if (JB_INVALID_SOCKET != sfd)
623 #endif /* def FEATURE_CONNECTION_SHARING */
625 /* Figure out if we need to connect to the web server or a HTTP proxy. */
626 if (fwd->forward_host)
629 dest_host = fwd->forward_host;
630 dest_port = fwd->forward_port;
635 dest_host = http->host;
636 dest_port = http->port;
639 /* Connect, maybe using a SOCKS proxy */
643 sfd = connect_to(dest_host, dest_port, csp);
647 sfd = socks4_connect(fwd, dest_host, dest_port, csp);
650 sfd = socks5_connect(fwd, dest_host, dest_port, csp);
653 /* Should never get here */
654 log_error(LOG_LEVEL_FATAL,
655 "SOCKS4 impossible internal error - bad SOCKS type.");
658 if (JB_INVALID_SOCKET != sfd)
660 log_error(LOG_LEVEL_CONNECT,
661 "Created new connection to %s:%d on socket %d.",
662 http->host, http->port, sfd);
670 /*********************************************************************
672 * Function : socks4_connect
674 * Description : Connect to the SOCKS server, and connect through
675 * it to the specified server. This handles
676 * all the SOCKS negotiation, and returns a file
677 * descriptor for a socket which can be treated as a
678 * normal (non-SOCKS) socket.
680 * Logged error messages are saved to csp->error_message
681 * and later reused by error_response() for the CGI
682 * message. strdup allocation failures are handled there.
685 * 1 : fwd = Specifies the SOCKS proxy to use.
686 * 2 : target_host = The final server to connect to.
687 * 3 : target_port = The final port to connect to.
688 * 4 : csp = Current client state (buffers, headers, etc...)
690 * Returns : JB_INVALID_SOCKET => failure, else a socket file descriptor.
692 *********************************************************************/
693 static jb_socket socks4_connect(const struct forward_spec * fwd,
694 const char * target_host,
696 struct client_state *csp)
698 unsigned long web_server_addr;
699 char buf[BUFFER_SIZE];
700 struct socks_op *c = (struct socks_op *)buf;
701 struct socks_reply *s = (struct socks_reply *)buf;
708 if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
710 /* XXX: Shouldn't the config file parser prevent this? */
711 errstr = "NULL gateway host specified.";
715 if (fwd->gateway_port <= 0)
717 errstr = "invalid gateway port specified.";
723 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
724 csp->error_message = strdup(errstr);
726 return(JB_INVALID_SOCKET);
729 /* build a socks request for connection to the web server */
731 strlcpy(&(c->userid), socks_userid, sizeof(buf) - sizeof(struct socks_op));
733 csiz = sizeof(*c) + sizeof(socks_userid) - sizeof(c->userid) - sizeof(c->padding);
738 web_server_addr = resolve_hostname_to_ip(target_host);
739 if (web_server_addr == INADDR_NONE)
741 errstr = "could not resolve target host";
742 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s %s", errstr, target_host);
747 web_server_addr = htonl(web_server_addr);
751 web_server_addr = 0x00000001;
752 n = csiz + strlen(target_host) + 1;
756 errstr = "buffer cbuf too small.";
757 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
762 strlcpy(buf + csiz, target_host, sizeof(buf) - sizeof(struct socks_op) - csiz);
764 * What we forward to the socks4a server should have the
765 * size of socks_op, plus the length of the userid plus
766 * its \0 byte (which we don't have to add because the
767 * first byte of the userid is counted twice as it's also
768 * part of sock_op) minus the padding bytes (which are part
769 * of the userid as well), plus the length of the target_host
770 * (which is stored csiz bytes after the beginning of the buffer),
771 * plus another \0 byte.
773 assert(n == sizeof(struct socks_op) + strlen(&(c->userid)) - sizeof(c->padding) + strlen(buf + csiz) + 1);
778 /* Should never get here */
779 log_error(LOG_LEVEL_FATAL,
780 "socks4_connect: SOCKS4 impossible internal error - bad SOCKS type.");
782 return(JB_INVALID_SOCKET);
787 csp->error_message = strdup(errstr);
788 return(JB_INVALID_SOCKET);
793 c->dstport[0] = (unsigned char)((target_port >> 8 ) & 0xff);
794 c->dstport[1] = (unsigned char)((target_port ) & 0xff);
795 c->dstip[0] = (unsigned char)((web_server_addr >> 24 ) & 0xff);
796 c->dstip[1] = (unsigned char)((web_server_addr >> 16 ) & 0xff);
797 c->dstip[2] = (unsigned char)((web_server_addr >> 8 ) & 0xff);
798 c->dstip[3] = (unsigned char)((web_server_addr ) & 0xff);
800 /* pass the request to the socks server */
801 sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
803 if (sfd == JB_INVALID_SOCKET)
806 * XXX: connect_to should fill in the exact reason.
807 * Most likely resolving the IP of the forwarder failed.
809 errstr = "connect_to failed: see logfile for details";
812 else if (write_socket(sfd, (char *)c, csiz))
814 errstr = "SOCKS4 negotiation write failed.";
815 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
819 else if (read_socket(sfd, buf, sizeof(buf)) != sizeof(*s))
821 errstr = "SOCKS4 negotiation read failed.";
822 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
829 csp->error_message = strdup(errstr);
830 return(JB_INVALID_SOCKET);
835 case SOCKS_REQUEST_GRANTED:
837 case SOCKS_REQUEST_REJECT:
838 errstr = "SOCKS request rejected or failed.";
841 case SOCKS_REQUEST_IDENT_FAILED:
842 errstr = "SOCKS request rejected because "
843 "SOCKS server cannot connect to identd on the client.";
846 case SOCKS_REQUEST_IDENT_CONFLICT:
847 errstr = "SOCKS request rejected because "
848 "the client program and identd report "
849 "different user-ids.";
854 snprintf(buf, sizeof(buf),
855 "SOCKS request rejected for reason code %d.", s->cd);
859 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
860 csp->error_message = strdup(errstr);
863 return(JB_INVALID_SOCKET);
867 /*********************************************************************
869 * Function : translate_socks5_error
871 * Description : Translates a SOCKS errors to a string.
874 * 1 : socks_error = The error code to translate.
876 * Returns : The string translation.
878 *********************************************************************/
879 static const char *translate_socks5_error(int socks_error)
883 /* XXX: these should be more descriptive */
884 case SOCKS5_REQUEST_FAILED:
885 return "SOCKS5 request failed";
886 case SOCKS5_REQUEST_DENIED:
887 return "SOCKS5 request denied";
888 case SOCKS5_REQUEST_NETWORK_UNREACHABLE:
889 return "SOCKS5 network unreachable";
890 case SOCKS5_REQUEST_HOST_UNREACHABLE:
891 return "SOCKS5 host unreachable";
892 case SOCKS5_REQUEST_CONNECTION_REFUSED:
893 return "SOCKS5 connection refused";
894 case SOCKS5_REQUEST_TTL_EXPIRED:
895 return "SOCKS5 TTL expired";
896 case SOCKS5_REQUEST_PROTOCOL_ERROR:
897 return "SOCKS5 client protocol error";
898 case SOCKS5_REQUEST_BAD_ADDRESS_TYPE:
899 return "SOCKS5 domain names unsupported";
900 case SOCKS5_REQUEST_GRANTED:
901 return "everything's peachy";
903 return "SOCKS5 negotiation protocol error";
907 /*********************************************************************
909 * Function : socks5_connect
911 * Description : Connect to the SOCKS server, and connect through
912 * it to the specified server. This handles
913 * all the SOCKS negotiation, and returns a file
914 * descriptor for a socket which can be treated as a
915 * normal (non-SOCKS) socket.
918 * 1 : fwd = Specifies the SOCKS proxy to use.
919 * 2 : target_host = The final server to connect to.
920 * 3 : target_port = The final port to connect to.
921 * 4 : csp = Current client state (buffers, headers, etc...)
923 * Returns : JB_INVALID_SOCKET => failure, else a socket file descriptor.
925 *********************************************************************/
926 static jb_socket socks5_connect(const struct forward_spec *fwd,
927 const char *target_host,
929 struct client_state *csp)
934 size_t client_pos = 0;
938 const char *errstr = NULL;
940 assert(fwd->gateway_host);
941 if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
943 errstr = "NULL gateway host specified";
947 if (fwd->gateway_port <= 0)
950 * XXX: currently this can't happen because in
951 * case of invalid gateway ports we use the defaults.
952 * Of course we really shouldn't do that.
954 errstr = "invalid gateway port specified";
958 hostlen = strlen(target_host);
959 if (hostlen > (size_t)255)
961 errstr = "target host name is longer than 255 characters";
965 if (fwd->type != SOCKS_5)
967 /* Should never get here */
968 log_error(LOG_LEVEL_FATAL,
969 "SOCKS5 impossible internal error - bad SOCKS type");
976 assert(errstr != NULL);
977 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
978 csp->error_message = strdup(errstr);
979 return(JB_INVALID_SOCKET);
982 /* pass the request to the socks server */
983 sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
985 if (sfd == JB_INVALID_SOCKET)
987 errstr = "socks5 server unreachable";
988 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
989 csp->error_message = strdup(errstr);
990 return(JB_INVALID_SOCKET);
994 cbuf[client_pos++] = '\x05'; /* Version */
995 cbuf[client_pos++] = '\x01'; /* One authentication method supported */
996 cbuf[client_pos++] = '\x00'; /* The no authentication authentication method */
998 if (write_socket(sfd, cbuf, client_pos))
1000 errstr = "SOCKS5 negotiation write failed";
1001 csp->error_message = strdup(errstr);
1002 log_error(LOG_LEVEL_CONNECT, "%s", errstr);
1004 return(JB_INVALID_SOCKET);
1007 if (!data_is_available(sfd, csp->config->socket_timeout))
1009 if (socket_is_still_alive(sfd))
1011 errstr = "SOCKS5 negotiation timed out";
1015 errstr = "SOCKS5 negotiation got aborted by the server";
1020 if (!err && read_socket(sfd, sbuf, sizeof(sbuf)) != 2)
1022 errstr = "SOCKS5 negotiation read failed";
1026 if (!err && (sbuf[0] != '\x05'))
1028 errstr = "SOCKS5 negotiation protocol version error";
1032 if (!err && (sbuf[1] == '\xff'))
1034 errstr = "SOCKS5 authentication required";
1038 if (!err && (sbuf[1] != '\x00'))
1040 errstr = "SOCKS5 negotiation protocol error";
1046 assert(errstr != NULL);
1047 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1048 csp->error_message = strdup(errstr);
1051 return(JB_INVALID_SOCKET);
1055 cbuf[client_pos++] = '\x05'; /* Version */
1056 cbuf[client_pos++] = '\x01'; /* TCP connect */
1057 cbuf[client_pos++] = '\x00'; /* Reserved, must be 0x00 */
1058 cbuf[client_pos++] = '\x03'; /* Address is domain name */
1059 cbuf[client_pos++] = (char)(hostlen & 0xffu);
1060 assert(sizeof(cbuf) - client_pos > (size_t)255);
1061 /* Using strncpy because we really want the nul byte padding. */
1062 strncpy(cbuf + client_pos, target_host, sizeof(cbuf) - client_pos);
1063 client_pos += (hostlen & 0xffu);
1064 cbuf[client_pos++] = (char)((target_port >> 8) & 0xff);
1065 cbuf[client_pos++] = (char)((target_port ) & 0xff);
1067 if (write_socket(sfd, cbuf, client_pos))
1069 errstr = "SOCKS5 negotiation read failed";
1070 csp->error_message = strdup(errstr);
1071 log_error(LOG_LEVEL_CONNECT, "%s", errstr);
1074 return(JB_INVALID_SOCKET);
1077 server_size = read_socket(sfd, sbuf, sizeof(sbuf));
1078 if (server_size < 3)
1080 errstr = "SOCKS5 negotiation read failed";
1083 else if (server_size > 20)
1085 /* This is somewhat unexpected but doesn't realy matter. */
1086 log_error(LOG_LEVEL_CONNECT, "socks5_connect: read %d bytes "
1087 "from socks server. Would have accepted up to %d.",
1088 server_size, sizeof(sbuf));
1091 if (!err && (sbuf[0] != '\x05'))
1093 errstr = "SOCKS5 negotiation protocol version error";
1097 if (!err && (sbuf[2] != '\x00'))
1099 errstr = "SOCKS5 negotiation protocol error";
1105 if (sbuf[1] == SOCKS5_REQUEST_GRANTED)
1109 errstr = translate_socks5_error(sbuf[1]);
1112 assert(errstr != NULL);
1113 csp->error_message = strdup(errstr);
1114 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1118 return(JB_INVALID_SOCKET);