1 const char gateway_rcs[] = "$Id: gateway.c,v 1.94 2015/06/18 15:26:40 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"
72 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
78 #endif /* def __GLIBC__ */
79 #endif /* HAVE_POLL */
80 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
82 const char gateway_h_rcs[] = GATEWAY_H_VERSION;
84 static jb_socket socks4_connect(const struct forward_spec * fwd,
85 const char * target_host,
87 struct client_state *csp);
89 static jb_socket socks5_connect(const struct forward_spec *fwd,
90 const char *target_host,
92 struct client_state *csp);
95 SOCKS4_REQUEST_GRANTED = 90,
96 SOCKS4_REQUEST_REJECT = 91,
97 SOCKS4_REQUEST_IDENT_FAILED = 92,
98 SOCKS4_REQUEST_IDENT_CONFLICT = 93
102 SOCKS5_REQUEST_GRANTED = 0,
103 SOCKS5_REQUEST_FAILED = 1,
104 SOCKS5_REQUEST_DENIED = 2,
105 SOCKS5_REQUEST_NETWORK_UNREACHABLE = 3,
106 SOCKS5_REQUEST_HOST_UNREACHABLE = 4,
107 SOCKS5_REQUEST_CONNECTION_REFUSED = 5,
108 SOCKS5_REQUEST_TTL_EXPIRED = 6,
109 SOCKS5_REQUEST_PROTOCOL_ERROR = 7,
110 SOCKS5_REQUEST_BAD_ADDRESS_TYPE = 8
113 /* structure of a socks client operation */
115 unsigned char vn; /* socks version number */
116 unsigned char cd; /* command code */
117 unsigned char dstport[2]; /* destination port */
118 unsigned char dstip[4]; /* destination address */
119 char userid; /* first byte of userid */
120 char padding[3]; /* make sure sizeof(struct socks_op) is endian-independent. */
121 /* more bytes of the userid follow, terminated by a NULL */
124 /* structure of a socks server reply */
126 unsigned char vn; /* socks version number */
127 unsigned char cd; /* command code */
128 unsigned char dstport[2]; /* destination port */
129 unsigned char dstip[4]; /* destination address */
132 static const char socks_userid[] = "anonymous";
134 #ifdef FEATURE_CONNECTION_SHARING
136 #define MAX_REUSABLE_CONNECTIONS 100
137 static unsigned int keep_alive_timeout = DEFAULT_KEEP_ALIVE_TIMEOUT;
139 static struct reusable_connection reusable_connection[MAX_REUSABLE_CONNECTIONS];
140 static int mark_connection_unused(const struct reusable_connection *connection);
142 /*********************************************************************
144 * Function : initialize_reusable_connections
146 * Description : Initializes the reusable_connection structures.
147 * Must be called with connection_reuse_mutex locked.
153 *********************************************************************/
154 extern void initialize_reusable_connections(void)
156 unsigned int slot = 0;
158 #if !defined(HAVE_POLL) && !defined(_WIN32)
159 log_error(LOG_LEVEL_INFO,
160 "Detecting already dead connections might not work "
161 "correctly on your platform. In case of problems, "
162 "unset the keep-alive-timeout option.");
165 for (slot = 0; slot < SZ(reusable_connection); slot++)
167 mark_connection_closed(&reusable_connection[slot]);
170 log_error(LOG_LEVEL_CONNECT, "Initialized %d socket slots.", slot);
174 /*********************************************************************
176 * Function : remember_connection
178 * Description : Remembers a server connection for reuse later on.
181 * 1 : connection = The server connection to remember.
185 *********************************************************************/
186 void remember_connection(const struct reusable_connection *connection)
188 unsigned int slot = 0;
189 int free_slot_found = FALSE;
191 assert(NULL != connection);
192 assert(connection->sfd != JB_INVALID_SOCKET);
194 if (mark_connection_unused(connection))
199 privoxy_mutex_lock(&connection_reuse_mutex);
201 /* Find free socket slot. */
202 for (slot = 0; slot < SZ(reusable_connection); slot++)
204 if (reusable_connection[slot].sfd == JB_INVALID_SOCKET)
206 assert(reusable_connection[slot].in_use == 0);
207 log_error(LOG_LEVEL_CONNECT,
208 "Remembering socket %d for %s:%d in slot %d.",
209 connection->sfd, connection->host, connection->port, slot);
210 free_slot_found = TRUE;
215 if (!free_slot_found)
217 log_error(LOG_LEVEL_CONNECT,
218 "No free slots found to remember socket for %s:%d. Last slot %d.",
219 connection->host, connection->port, slot);
220 privoxy_mutex_unlock(&connection_reuse_mutex);
221 close_socket(connection->sfd);
225 assert(NULL != connection->host);
226 reusable_connection[slot].host = strdup_or_die(connection->host);
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[slot].request_sent = connection->request_sent;
232 reusable_connection[slot].response_received = connection->response_received;
233 reusable_connection[slot].keep_alive_timeout = connection->keep_alive_timeout;
234 reusable_connection[slot].requests_sent_total = connection->requests_sent_total;
236 assert(reusable_connection[slot].gateway_host == NULL);
237 assert(reusable_connection[slot].gateway_port == 0);
238 assert(reusable_connection[slot].forwarder_type == SOCKS_NONE);
239 assert(reusable_connection[slot].forward_host == NULL);
240 assert(reusable_connection[slot].forward_port == 0);
242 reusable_connection[slot].forwarder_type = connection->forwarder_type;
243 if (NULL != connection->gateway_host)
245 reusable_connection[slot].gateway_host = strdup_or_die(connection->gateway_host);
249 reusable_connection[slot].gateway_host = NULL;
251 reusable_connection[slot].gateway_port = connection->gateway_port;
253 if (NULL != connection->forward_host)
255 reusable_connection[slot].forward_host = strdup_or_die(connection->forward_host);
259 reusable_connection[slot].forward_host = NULL;
261 reusable_connection[slot].forward_port = connection->forward_port;
263 privoxy_mutex_unlock(&connection_reuse_mutex);
265 #endif /* def FEATURE_CONNECTION_SHARING */
268 /*********************************************************************
270 * Function : mark_connection_closed
272 * Description : Marks a reused connection closed.
275 * 1 : closed_connection = The connection to mark as closed.
279 *********************************************************************/
280 void mark_connection_closed(struct reusable_connection *closed_connection)
282 closed_connection->in_use = FALSE;
283 closed_connection->sfd = JB_INVALID_SOCKET;
284 freez(closed_connection->host);
285 closed_connection->port = 0;
286 closed_connection->timestamp = 0;
287 closed_connection->request_sent = 0;
288 closed_connection->response_received = 0;
289 closed_connection->keep_alive_timeout = 0;
290 closed_connection->requests_sent_total = 0;
291 closed_connection->forwarder_type = SOCKS_NONE;
292 freez(closed_connection->gateway_host);
293 closed_connection->gateway_port = 0;
294 freez(closed_connection->forward_host);
295 closed_connection->forward_port = 0;
299 #ifdef FEATURE_CONNECTION_SHARING
300 /*********************************************************************
302 * Function : forget_connection
304 * Description : Removes a previously remembered connection from
305 * the list of reusable connections.
308 * 1 : sfd = The socket belonging to the connection in question.
312 *********************************************************************/
313 void forget_connection(jb_socket sfd)
315 unsigned int slot = 0;
317 assert(sfd != JB_INVALID_SOCKET);
319 privoxy_mutex_lock(&connection_reuse_mutex);
321 for (slot = 0; slot < SZ(reusable_connection); slot++)
323 if (reusable_connection[slot].sfd == sfd)
325 assert(reusable_connection[slot].in_use);
327 log_error(LOG_LEVEL_CONNECT,
328 "Forgetting socket %d for %s:%d in slot %d.",
329 sfd, reusable_connection[slot].host,
330 reusable_connection[slot].port, slot);
331 mark_connection_closed(&reusable_connection[slot]);
336 privoxy_mutex_unlock(&connection_reuse_mutex);
339 #endif /* def FEATURE_CONNECTION_SHARING */
342 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
343 /*********************************************************************
345 * Function : connection_destination_matches
347 * Description : Determines whether a remembered connection can
348 * be reused. That is, whether the destination and
349 * the forwarding settings match.
352 * 1 : connection = The connection to check.
353 * 2 : http = The destination for the connection.
354 * 3 : fwd = The forwarder settings.
356 * Returns : TRUE for yes, FALSE otherwise.
358 *********************************************************************/
359 int connection_destination_matches(const struct reusable_connection *connection,
360 const struct http_request *http,
361 const struct forward_spec *fwd)
363 if ((connection->forwarder_type != fwd->type)
364 || (connection->gateway_port != fwd->gateway_port)
365 || (connection->forward_port != fwd->forward_port)
366 || (connection->port != http->port))
371 if (( (NULL != connection->gateway_host)
372 && (NULL != fwd->gateway_host)
373 && strcmpic(connection->gateway_host, fwd->gateway_host))
374 && (connection->gateway_host != fwd->gateway_host))
376 log_error(LOG_LEVEL_CONNECT,
377 "Gateway mismatch. Previous gateway: %s. Current gateway: %s",
378 connection->gateway_host, fwd->gateway_host);
382 if (( (NULL != connection->forward_host)
383 && (NULL != fwd->forward_host)
384 && strcmpic(connection->forward_host, fwd->forward_host))
385 && (connection->forward_host != fwd->forward_host))
387 log_error(LOG_LEVEL_CONNECT,
388 "Forwarding proxy mismatch. Previous proxy: %s. Current proxy: %s",
389 connection->forward_host, fwd->forward_host);
393 return (!strcmpic(connection->host, http->host));
396 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
399 #ifdef FEATURE_CONNECTION_SHARING
400 /*********************************************************************
402 * Function : close_unusable_connections
404 * Description : Closes remembered connections that have timed
405 * out or have been closed on the other side.
409 * Returns : Number of connections that are still alive.
411 *********************************************************************/
412 int close_unusable_connections(void)
414 unsigned int slot = 0;
415 int connections_alive = 0;
417 privoxy_mutex_lock(&connection_reuse_mutex);
419 for (slot = 0; slot < SZ(reusable_connection); slot++)
421 if (!reusable_connection[slot].in_use
422 && (JB_INVALID_SOCKET != reusable_connection[slot].sfd))
424 time_t time_open = time(NULL) - reusable_connection[slot].timestamp;
425 time_t latency = (reusable_connection[slot].response_received -
426 reusable_connection[slot].request_sent) / 2;
428 if (reusable_connection[slot].keep_alive_timeout < time_open + latency)
430 log_error(LOG_LEVEL_CONNECT,
431 "The connection to %s:%d in slot %d timed out. "
432 "Closing socket %d. Timeout is: %d. Assumed latency: %d.",
433 reusable_connection[slot].host,
434 reusable_connection[slot].port, slot,
435 reusable_connection[slot].sfd,
436 reusable_connection[slot].keep_alive_timeout,
438 close_socket(reusable_connection[slot].sfd);
439 mark_connection_closed(&reusable_connection[slot]);
441 else if (!socket_is_still_alive(reusable_connection[slot].sfd))
443 log_error(LOG_LEVEL_CONNECT,
444 "The connection to %s:%d in slot %d is no longer usable. "
445 "Closing socket %d.", reusable_connection[slot].host,
446 reusable_connection[slot].port, slot,
447 reusable_connection[slot].sfd);
448 close_socket(reusable_connection[slot].sfd);
449 mark_connection_closed(&reusable_connection[slot]);
458 privoxy_mutex_unlock(&connection_reuse_mutex);
460 return connections_alive;
465 /*********************************************************************
467 * Function : get_reusable_connection
469 * Description : Returns an open socket to a previously remembered
470 * open connection (if there is one).
473 * 1 : http = The destination for the connection.
474 * 2 : fwd = The forwarder settings.
476 * Returns : JB_INVALID_SOCKET => No reusable connection found,
477 * otherwise a usable socket.
479 *********************************************************************/
480 static jb_socket get_reusable_connection(const struct http_request *http,
481 const struct forward_spec *fwd)
483 jb_socket sfd = JB_INVALID_SOCKET;
484 unsigned int slot = 0;
486 close_unusable_connections();
488 privoxy_mutex_lock(&connection_reuse_mutex);
490 for (slot = 0; slot < SZ(reusable_connection); slot++)
492 if (!reusable_connection[slot].in_use
493 && (JB_INVALID_SOCKET != reusable_connection[slot].sfd))
495 if (connection_destination_matches(&reusable_connection[slot], http, fwd))
497 reusable_connection[slot].in_use = TRUE;
498 sfd = reusable_connection[slot].sfd;
499 log_error(LOG_LEVEL_CONNECT,
500 "Found reusable socket %d for %s:%d in slot %d. Timestamp made %d "
501 "seconds ago. Timeout: %d. Latency: %d. Requests served: %d",
502 sfd, reusable_connection[slot].host, reusable_connection[slot].port,
503 slot, time(NULL) - reusable_connection[slot].timestamp,
504 reusable_connection[slot].keep_alive_timeout,
505 (int)(reusable_connection[slot].response_received -
506 reusable_connection[slot].request_sent),
507 reusable_connection[slot].requests_sent_total);
513 privoxy_mutex_unlock(&connection_reuse_mutex);
520 /*********************************************************************
522 * Function : mark_connection_unused
524 * Description : Gives a remembered connection free for reuse.
527 * 1 : connection = The connection in question.
529 * Returns : TRUE => Socket found and marked as unused.
530 * FALSE => Socket not found.
532 *********************************************************************/
533 static int mark_connection_unused(const struct reusable_connection *connection)
535 unsigned int slot = 0;
536 int socket_found = FALSE;
538 assert(connection->sfd != JB_INVALID_SOCKET);
540 privoxy_mutex_lock(&connection_reuse_mutex);
542 for (slot = 0; slot < SZ(reusable_connection); slot++)
544 if (reusable_connection[slot].sfd == connection->sfd)
546 assert(reusable_connection[slot].in_use);
548 log_error(LOG_LEVEL_CONNECT,
549 "Marking open socket %d for %s:%d in slot %d as unused.",
550 connection->sfd, reusable_connection[slot].host,
551 reusable_connection[slot].port, slot);
552 reusable_connection[slot].in_use = 0;
553 reusable_connection[slot].timestamp = connection->timestamp;
558 privoxy_mutex_unlock(&connection_reuse_mutex);
565 /*********************************************************************
567 * Function : set_keep_alive_timeout
569 * Description : Sets the timeout after which open
570 * connections will no longer be reused.
573 * 1 : timeout = The timeout in seconds.
577 *********************************************************************/
578 void set_keep_alive_timeout(unsigned int timeout)
580 keep_alive_timeout = timeout;
582 #endif /* def FEATURE_CONNECTION_SHARING */
585 /*********************************************************************
587 * Function : forwarded_connect
589 * Description : Connect to a specified web server, possibly via
590 * a HTTP proxy and/or a SOCKS proxy.
593 * 1 : fwd = the proxies to use when connecting.
594 * 2 : http = the http request and apropos headers
595 * 3 : csp = Current client state (buffers, headers, etc...)
597 * Returns : JB_INVALID_SOCKET => failure, else it is the socket file descriptor.
599 *********************************************************************/
600 jb_socket forwarded_connect(const struct forward_spec * fwd,
601 struct http_request *http,
602 struct client_state *csp)
604 const char * dest_host;
606 jb_socket sfd = JB_INVALID_SOCKET;
608 #ifdef FEATURE_CONNECTION_SHARING
609 if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING)
610 && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED))
612 sfd = get_reusable_connection(http, fwd);
613 if (JB_INVALID_SOCKET != sfd)
618 #endif /* def FEATURE_CONNECTION_SHARING */
620 /* Figure out if we need to connect to the web server or a HTTP proxy. */
621 if (fwd->forward_host)
624 dest_host = fwd->forward_host;
625 dest_port = fwd->forward_port;
630 dest_host = http->host;
631 dest_port = http->port;
634 /* Connect, maybe using a SOCKS proxy */
638 sfd = connect_to(dest_host, dest_port, csp);
642 sfd = socks4_connect(fwd, dest_host, dest_port, csp);
646 sfd = socks5_connect(fwd, dest_host, dest_port, csp);
649 /* Should never get here */
650 log_error(LOG_LEVEL_FATAL,
651 "Internal error in forwarded_connect(). Bad proxy type: %d", fwd->type);
654 if (JB_INVALID_SOCKET != sfd)
656 log_error(LOG_LEVEL_CONNECT,
657 "Created new connection to %s:%d on socket %d.",
658 http->host, http->port, sfd);
666 /*********************************************************************
668 * Function : socks4_connect
670 * Description : Connect to the SOCKS server, and connect through
671 * it to the specified server. This handles
672 * all the SOCKS negotiation, and returns a file
673 * descriptor for a socket which can be treated as a
674 * normal (non-SOCKS) socket.
676 * Logged error messages are saved to csp->error_message
677 * and later reused by error_response() for the CGI
678 * message. strdup allocation failures are handled there.
681 * 1 : fwd = Specifies the SOCKS proxy to use.
682 * 2 : target_host = The final server to connect to.
683 * 3 : target_port = The final port to connect to.
684 * 4 : csp = Current client state (buffers, headers, etc...)
686 * Returns : JB_INVALID_SOCKET => failure, else a socket file descriptor.
688 *********************************************************************/
689 static jb_socket socks4_connect(const struct forward_spec * fwd,
690 const char * target_host,
692 struct client_state *csp)
694 unsigned long web_server_addr;
695 char buf[BUFFER_SIZE];
696 struct socks_op *c = (struct socks_op *)buf;
697 struct socks_reply *s = (struct socks_reply *)buf;
704 if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
706 /* XXX: Shouldn't the config file parser prevent this? */
707 errstr = "NULL gateway host specified.";
711 if (fwd->gateway_port <= 0)
713 errstr = "invalid gateway port specified.";
719 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
720 csp->error_message = strdup(errstr);
722 return(JB_INVALID_SOCKET);
725 /* build a socks request for connection to the web server */
727 strlcpy(&(c->userid), socks_userid, sizeof(buf) - sizeof(struct socks_op));
729 csiz = sizeof(*c) + sizeof(socks_userid) - sizeof(c->userid) - sizeof(c->padding);
734 web_server_addr = resolve_hostname_to_ip(target_host);
735 if (web_server_addr == INADDR_NONE)
737 errstr = "could not resolve target host";
738 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s %s", errstr, target_host);
743 web_server_addr = htonl(web_server_addr);
747 web_server_addr = 0x00000001;
748 n = csiz + strlen(target_host) + 1;
752 errstr = "buffer cbuf too small.";
753 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
758 strlcpy(buf + csiz, target_host, sizeof(buf) - sizeof(struct socks_op) - csiz);
760 * What we forward to the socks4a server should have the
761 * size of socks_op, plus the length of the userid plus
762 * its \0 byte (which we don't have to add because the
763 * first byte of the userid is counted twice as it's also
764 * part of sock_op) minus the padding bytes (which are part
765 * of the userid as well), plus the length of the target_host
766 * (which is stored csiz bytes after the beginning of the buffer),
767 * plus another \0 byte.
769 assert(n == sizeof(struct socks_op) + strlen(&(c->userid)) - sizeof(c->padding) + strlen(buf + csiz) + 1);
774 /* Should never get here */
775 log_error(LOG_LEVEL_FATAL,
776 "socks4_connect: SOCKS4 impossible internal error - bad SOCKS type.");
778 return(JB_INVALID_SOCKET);
783 csp->error_message = strdup(errstr);
784 return(JB_INVALID_SOCKET);
789 c->dstport[0] = (unsigned char)((target_port >> 8 ) & 0xff);
790 c->dstport[1] = (unsigned char)((target_port ) & 0xff);
791 c->dstip[0] = (unsigned char)((web_server_addr >> 24) & 0xff);
792 c->dstip[1] = (unsigned char)((web_server_addr >> 16) & 0xff);
793 c->dstip[2] = (unsigned char)((web_server_addr >> 8) & 0xff);
794 c->dstip[3] = (unsigned char)((web_server_addr ) & 0xff);
796 /* pass the request to the socks server */
797 sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
799 if (sfd == JB_INVALID_SOCKET)
801 /* The error an its reason have already been logged by connect_to() */
802 return(JB_INVALID_SOCKET);
804 else if (write_socket(sfd, (char *)c, csiz))
806 errstr = "SOCKS4 negotiation write failed.";
807 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
811 else if (!data_is_available(sfd, csp->config->socket_timeout))
813 if (socket_is_still_alive(sfd))
815 errstr = "SOCKS4 negotiation timed out";
819 errstr = "SOCKS4 negotiation got aborted by the server";
821 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
825 else if (read_socket(sfd, buf, sizeof(buf)) != sizeof(*s))
827 errstr = "SOCKS4 negotiation read failed.";
828 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
835 csp->error_message = strdup(errstr);
836 return(JB_INVALID_SOCKET);
841 case SOCKS4_REQUEST_GRANTED:
843 case SOCKS4_REQUEST_REJECT:
844 errstr = "SOCKS request rejected or failed.";
847 case SOCKS4_REQUEST_IDENT_FAILED:
848 errstr = "SOCKS request rejected because "
849 "SOCKS server cannot connect to identd on the client.";
852 case SOCKS4_REQUEST_IDENT_CONFLICT:
853 errstr = "SOCKS request rejected because "
854 "the client program and identd report "
855 "different user-ids.";
860 snprintf(buf, sizeof(buf),
861 "SOCKS request rejected for reason code %d.", s->cd);
865 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
866 csp->error_message = strdup(errstr);
869 return(JB_INVALID_SOCKET);
873 /*********************************************************************
875 * Function : translate_socks5_error
877 * Description : Translates a SOCKS errors to a string.
880 * 1 : socks_error = The error code to translate.
882 * Returns : The string translation.
884 *********************************************************************/
885 static const char *translate_socks5_error(int socks_error)
889 /* XXX: these should be more descriptive */
890 case SOCKS5_REQUEST_FAILED:
891 return "SOCKS5 request failed";
892 case SOCKS5_REQUEST_DENIED:
893 return "SOCKS5 request denied";
894 case SOCKS5_REQUEST_NETWORK_UNREACHABLE:
895 return "SOCKS5 network unreachable";
896 case SOCKS5_REQUEST_HOST_UNREACHABLE:
897 return "SOCKS5 destination host unreachable";
898 case SOCKS5_REQUEST_CONNECTION_REFUSED:
899 return "SOCKS5 connection refused";
900 case SOCKS5_REQUEST_TTL_EXPIRED:
901 return "SOCKS5 TTL expired";
902 case SOCKS5_REQUEST_PROTOCOL_ERROR:
903 return "SOCKS5 client protocol error";
904 case SOCKS5_REQUEST_BAD_ADDRESS_TYPE:
905 return "SOCKS5 domain names unsupported";
906 case SOCKS5_REQUEST_GRANTED:
907 return "everything's peachy";
909 return "SOCKS5 negotiation protocol error";
913 /*********************************************************************
915 * Function : socks5_connect
917 * Description : Connect to the SOCKS server, and connect through
918 * it to the specified server. This handles
919 * all the SOCKS negotiation, and returns a file
920 * descriptor for a socket which can be treated as a
921 * normal (non-SOCKS) socket.
924 * 1 : fwd = Specifies the SOCKS proxy to use.
925 * 2 : target_host = The final server to connect to.
926 * 3 : target_port = The final port to connect to.
927 * 4 : csp = Current client state (buffers, headers, etc...)
929 * Returns : JB_INVALID_SOCKET => failure, else a socket file descriptor.
931 *********************************************************************/
932 static jb_socket socks5_connect(const struct forward_spec *fwd,
933 const char *target_host,
935 struct client_state *csp)
940 size_t client_pos = 0;
944 const char *errstr = NULL;
946 assert(fwd->gateway_host);
947 if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
949 errstr = "NULL gateway host specified";
953 if (fwd->gateway_port <= 0)
956 * XXX: currently this can't happen because in
957 * case of invalid gateway ports we use the defaults.
958 * Of course we really shouldn't do that.
960 errstr = "invalid gateway port specified";
964 hostlen = strlen(target_host);
965 if (hostlen > (size_t)255)
967 errstr = "target host name is longer than 255 characters";
971 if ((fwd->type != SOCKS_5) && (fwd->type != SOCKS_5T))
973 /* Should never get here */
974 log_error(LOG_LEVEL_FATAL,
975 "SOCKS5 impossible internal error - bad SOCKS type");
982 assert(errstr != NULL);
983 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
984 csp->error_message = strdup(errstr);
985 return(JB_INVALID_SOCKET);
988 /* pass the request to the socks server */
989 sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
991 if (sfd == JB_INVALID_SOCKET)
993 errstr = "socks5 server unreachable";
994 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
995 /* Free the generic error message provided by connect_to() */
996 freez(csp->error_message);
997 csp->error_message = strdup(errstr);
998 return(JB_INVALID_SOCKET);
1002 cbuf[client_pos++] = '\x05'; /* Version */
1003 cbuf[client_pos++] = '\x01'; /* One authentication method supported */
1004 cbuf[client_pos++] = '\x00'; /* The no authentication authentication method */
1006 if (write_socket(sfd, cbuf, client_pos))
1008 errstr = "SOCKS5 negotiation write failed";
1009 csp->error_message = strdup(errstr);
1010 log_error(LOG_LEVEL_CONNECT, "%s", errstr);
1012 return(JB_INVALID_SOCKET);
1015 if (!data_is_available(sfd, csp->config->socket_timeout))
1017 if (socket_is_still_alive(sfd))
1019 errstr = "SOCKS5 negotiation timed out";
1023 errstr = "SOCKS5 negotiation got aborted by the server";
1028 if (!err && read_socket(sfd, sbuf, sizeof(sbuf)) != 2)
1030 errstr = "SOCKS5 negotiation read failed";
1034 if (!err && (sbuf[0] != '\x05'))
1036 errstr = "SOCKS5 negotiation protocol version error";
1040 if (!err && (sbuf[1] == '\xff'))
1042 errstr = "SOCKS5 authentication required";
1046 if (!err && (sbuf[1] != '\x00'))
1048 errstr = "SOCKS5 negotiation protocol error";
1054 assert(errstr != NULL);
1055 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1056 csp->error_message = strdup(errstr);
1059 return(JB_INVALID_SOCKET);
1063 cbuf[client_pos++] = '\x05'; /* Version */
1064 cbuf[client_pos++] = '\x01'; /* TCP connect */
1065 cbuf[client_pos++] = '\x00'; /* Reserved, must be 0x00 */
1066 cbuf[client_pos++] = '\x03'; /* Address is domain name */
1067 cbuf[client_pos++] = (char)(hostlen & 0xffu);
1068 assert(sizeof(cbuf) - client_pos > (size_t)255);
1069 /* Using strncpy because we really want the nul byte padding. */
1070 strncpy(cbuf + client_pos, target_host, sizeof(cbuf) - client_pos);
1071 client_pos += (hostlen & 0xffu);
1072 cbuf[client_pos++] = (char)((target_port >> 8) & 0xff);
1073 cbuf[client_pos++] = (char)((target_port ) & 0xff);
1075 if (write_socket(sfd, cbuf, client_pos))
1077 errstr = "SOCKS5 negotiation write failed";
1078 csp->error_message = strdup(errstr);
1079 log_error(LOG_LEVEL_CONNECT, "%s", errstr);
1082 return(JB_INVALID_SOCKET);
1086 * Optimistically send the HTTP request with the initial
1087 * SOCKS request if the user enabled the use of Tor extensions,
1088 * the CONNECT method isn't being used (in which case the client
1089 * doesn't send data until it gets our 200 response) and the
1090 * client request has actually been completely read already.
1092 if ((fwd->type == SOCKS_5T) && (csp->http->ssl == 0)
1093 && (csp->flags & CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ))
1095 char *client_headers = list_to_text(csp->headers);
1096 size_t header_length;
1098 if (client_headers == NULL)
1100 log_error(LOG_LEVEL_FATAL, "Out of memory rebuilding client headers");
1102 list_remove_all(csp->headers);
1103 header_length= strlen(client_headers);
1105 log_error(LOG_LEVEL_CONNECT,
1106 "Optimistically sending %d bytes of client headers intended for %s",
1107 header_length, csp->http->hostport);
1109 if (write_socket(sfd, client_headers, header_length))
1111 log_error(LOG_LEVEL_CONNECT,
1112 "optimistically writing header to: %s failed: %E", csp->http->hostport);
1113 freez(client_headers);
1114 return(JB_INVALID_SOCKET);
1116 freez(client_headers);
1117 if (csp->expected_client_content_length != 0)
1119 unsigned long long buffered_request_bytes =
1120 (unsigned long long)(csp->client_iob->eod - csp->client_iob->cur);
1121 log_error(LOG_LEVEL_CONNECT,
1122 "Optimistically sending %d bytes of client body. Expected %d",
1123 csp->expected_client_content_length, buffered_request_bytes);
1124 assert(csp->expected_client_content_length == buffered_request_bytes);
1125 if (write_socket(sfd, csp->client_iob->cur, buffered_request_bytes))
1127 log_error(LOG_LEVEL_CONNECT,
1128 "optimistically writing %d bytes of client body to: %s failed: %E",
1129 buffered_request_bytes, csp->http->hostport);
1130 return(JB_INVALID_SOCKET);
1132 clear_iob(csp->client_iob);
1136 server_size = read_socket(sfd, sbuf, sizeof(sbuf));
1137 if (server_size != sizeof(sbuf))
1139 errstr = "SOCKS5 negotiation read failed";
1143 if (sbuf[0] != '\x05')
1145 errstr = "SOCKS5 negotiation protocol version error";
1147 else if (sbuf[2] != '\x00')
1149 errstr = "SOCKS5 negotiation protocol error";
1151 else if (sbuf[1] != SOCKS5_REQUEST_GRANTED)
1153 errstr = translate_socks5_error(sbuf[1]);
1161 assert(errstr != NULL);
1162 csp->error_message = strdup(errstr);
1163 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1167 return(JB_INVALID_SOCKET);