1 const char gateway_rcs[] = "$Id: gateway.c,v 1.90 2012/10/17 18:13:26 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 SOCKS4_REQUEST_GRANTED = 90,
93 SOCKS4_REQUEST_REJECT = 91,
94 SOCKS4_REQUEST_IDENT_FAILED = 92,
95 SOCKS4_REQUEST_IDENT_CONFLICT = 93
99 SOCKS5_REQUEST_GRANTED = 0,
100 SOCKS5_REQUEST_FAILED = 1,
101 SOCKS5_REQUEST_DENIED = 2,
102 SOCKS5_REQUEST_NETWORK_UNREACHABLE = 3,
103 SOCKS5_REQUEST_HOST_UNREACHABLE = 4,
104 SOCKS5_REQUEST_CONNECTION_REFUSED = 5,
105 SOCKS5_REQUEST_TTL_EXPIRED = 6,
106 SOCKS5_REQUEST_PROTOCOL_ERROR = 7,
107 SOCKS5_REQUEST_BAD_ADDRESS_TYPE = 8
110 /* structure of a socks client operation */
112 unsigned char vn; /* socks version number */
113 unsigned char cd; /* command code */
114 unsigned char dstport[2]; /* destination port */
115 unsigned char dstip[4]; /* destination address */
116 char userid; /* first byte of userid */
117 char padding[3]; /* make sure sizeof(struct socks_op) is endian-independent. */
118 /* more bytes of the userid follow, terminated by a NULL */
121 /* structure of a socks server reply */
123 unsigned char vn; /* socks version number */
124 unsigned char cd; /* command code */
125 unsigned char dstport[2]; /* destination port */
126 unsigned char dstip[4]; /* destination address */
129 static const char socks_userid[] = "anonymous";
131 #ifdef FEATURE_CONNECTION_SHARING
133 #define MAX_REUSABLE_CONNECTIONS 100
134 static unsigned int keep_alive_timeout = DEFAULT_KEEP_ALIVE_TIMEOUT;
136 static struct reusable_connection reusable_connection[MAX_REUSABLE_CONNECTIONS];
137 static int mark_connection_unused(const struct reusable_connection *connection);
139 /*********************************************************************
141 * Function : initialize_reusable_connections
143 * Description : Initializes the reusable_connection structures.
144 * Must be called with connection_reuse_mutex locked.
150 *********************************************************************/
151 extern void initialize_reusable_connections(void)
153 unsigned int slot = 0;
155 #if !defined(HAVE_POLL) && !defined(_WIN32)
156 log_error(LOG_LEVEL_INFO,
157 "Detecting already dead connections might not work "
158 "correctly on your platform. In case of problems, "
159 "unset the keep-alive-timeout option.");
162 for (slot = 0; slot < SZ(reusable_connection); slot++)
164 mark_connection_closed(&reusable_connection[slot]);
167 log_error(LOG_LEVEL_CONNECT, "Initialized %d socket slots.", slot);
171 /*********************************************************************
173 * Function : remember_connection
175 * Description : Remembers a server connection for reuse later on.
178 * 1 : connection = The server connection to remember.
182 *********************************************************************/
183 void remember_connection(const struct reusable_connection *connection)
185 unsigned int slot = 0;
186 int free_slot_found = FALSE;
188 assert(NULL != connection);
189 assert(connection->sfd != JB_INVALID_SOCKET);
191 if (mark_connection_unused(connection))
196 privoxy_mutex_lock(&connection_reuse_mutex);
198 /* Find free socket slot. */
199 for (slot = 0; slot < SZ(reusable_connection); slot++)
201 if (reusable_connection[slot].sfd == JB_INVALID_SOCKET)
203 assert(reusable_connection[slot].in_use == 0);
204 log_error(LOG_LEVEL_CONNECT,
205 "Remembering socket %d for %s:%d in slot %d.",
206 connection->sfd, connection->host, connection->port, slot);
207 free_slot_found = TRUE;
212 if (!free_slot_found)
214 log_error(LOG_LEVEL_CONNECT,
215 "No free slots found to remember socket for %s:%d. Last slot %d.",
216 connection->host, connection->port, slot);
217 privoxy_mutex_unlock(&connection_reuse_mutex);
218 close_socket(connection->sfd);
222 assert(NULL != connection->host);
223 reusable_connection[slot].host = strdup_or_die(connection->host);
224 reusable_connection[slot].sfd = connection->sfd;
225 reusable_connection[slot].port = connection->port;
226 reusable_connection[slot].in_use = 0;
227 reusable_connection[slot].timestamp = connection->timestamp;
228 reusable_connection[slot].request_sent = connection->request_sent;
229 reusable_connection[slot].response_received = connection->response_received;
230 reusable_connection[slot].keep_alive_timeout = connection->keep_alive_timeout;
231 reusable_connection[slot].requests_sent_total = connection->requests_sent_total;
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_or_die(connection->gateway_host);
246 reusable_connection[slot].gateway_host = NULL;
248 reusable_connection[slot].gateway_port = connection->gateway_port;
250 if (NULL != connection->forward_host)
252 reusable_connection[slot].forward_host = strdup_or_die(connection->forward_host);
256 reusable_connection[slot].forward_host = NULL;
258 reusable_connection[slot].forward_port = connection->forward_port;
260 privoxy_mutex_unlock(&connection_reuse_mutex);
262 #endif /* def FEATURE_CONNECTION_SHARING */
265 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
266 /*********************************************************************
268 * Function : mark_connection_closed
270 * Description : Marks a reused connection closed.
273 * 1 : closed_connection = The connection to mark as closed.
277 *********************************************************************/
278 void mark_connection_closed(struct reusable_connection *closed_connection)
280 closed_connection->in_use = FALSE;
281 closed_connection->sfd = JB_INVALID_SOCKET;
282 freez(closed_connection->host);
283 closed_connection->port = 0;
284 closed_connection->timestamp = 0;
285 closed_connection->request_sent = 0;
286 closed_connection->response_received = 0;
287 closed_connection->keep_alive_timeout = 0;
288 closed_connection->requests_sent_total = 0;
289 closed_connection->forwarder_type = SOCKS_NONE;
290 freez(closed_connection->gateway_host);
291 closed_connection->gateway_port = 0;
292 freez(closed_connection->forward_host);
293 closed_connection->forward_port = 0;
295 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
298 #ifdef FEATURE_CONNECTION_SHARING
299 /*********************************************************************
301 * Function : forget_connection
303 * Description : Removes a previously remembered connection from
304 * the list of reusable connections.
307 * 1 : sfd = The socket belonging to the connection in question.
311 *********************************************************************/
312 void forget_connection(jb_socket sfd)
314 unsigned int slot = 0;
316 assert(sfd != JB_INVALID_SOCKET);
318 privoxy_mutex_lock(&connection_reuse_mutex);
320 for (slot = 0; slot < SZ(reusable_connection); slot++)
322 if (reusable_connection[slot].sfd == sfd)
324 assert(reusable_connection[slot].in_use);
326 log_error(LOG_LEVEL_CONNECT,
327 "Forgetting socket %d for %s:%d in slot %d.",
328 sfd, reusable_connection[slot].host,
329 reusable_connection[slot].port, slot);
330 mark_connection_closed(&reusable_connection[slot]);
335 privoxy_mutex_unlock(&connection_reuse_mutex);
338 #endif /* def FEATURE_CONNECTION_SHARING */
341 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
342 /*********************************************************************
344 * Function : connection_destination_matches
346 * Description : Determines whether a remembered connection can
347 * be reused. That is, whether the destination and
348 * the forwarding settings match.
351 * 1 : connection = The connection to check.
352 * 2 : http = The destination for the connection.
353 * 3 : fwd = The forwarder settings.
355 * Returns : TRUE for yes, FALSE otherwise.
357 *********************************************************************/
358 int connection_destination_matches(const struct reusable_connection *connection,
359 const struct http_request *http,
360 const struct forward_spec *fwd)
362 if ((connection->forwarder_type != fwd->type)
363 || (connection->gateway_port != fwd->gateway_port)
364 || (connection->forward_port != fwd->forward_port)
365 || (connection->port != http->port))
370 if (( (NULL != connection->gateway_host)
371 && (NULL != fwd->gateway_host)
372 && strcmpic(connection->gateway_host, fwd->gateway_host))
373 && (connection->gateway_host != fwd->gateway_host))
375 log_error(LOG_LEVEL_CONNECT,
376 "Gateway mismatch. Previous gateway: %s. Current gateway: %s",
377 connection->gateway_host, fwd->gateway_host);
381 if (( (NULL != connection->forward_host)
382 && (NULL != fwd->forward_host)
383 && strcmpic(connection->forward_host, fwd->forward_host))
384 && (connection->forward_host != fwd->forward_host))
386 log_error(LOG_LEVEL_CONNECT,
387 "Forwarding proxy mismatch. Previous proxy: %s. Current proxy: %s",
388 connection->forward_host, fwd->forward_host);
392 return (!strcmpic(connection->host, http->host));
395 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
398 #ifdef FEATURE_CONNECTION_SHARING
399 /*********************************************************************
401 * Function : close_unusable_connections
403 * Description : Closes remembered connections that have timed
404 * out or have been closed on the other side.
408 * Returns : Number of connections that are still alive.
410 *********************************************************************/
411 int close_unusable_connections(void)
413 unsigned int slot = 0;
414 int connections_alive = 0;
416 privoxy_mutex_lock(&connection_reuse_mutex);
418 for (slot = 0; slot < SZ(reusable_connection); slot++)
420 if (!reusable_connection[slot].in_use
421 && (JB_INVALID_SOCKET != reusable_connection[slot].sfd))
423 time_t time_open = time(NULL) - reusable_connection[slot].timestamp;
424 time_t latency = (reusable_connection[slot].response_received -
425 reusable_connection[slot].request_sent) / 2;
427 if (reusable_connection[slot].keep_alive_timeout < time_open + latency)
429 log_error(LOG_LEVEL_CONNECT,
430 "The connection to %s:%d in slot %d timed out. "
431 "Closing socket %d. Timeout is: %d. Assumed latency: %d.",
432 reusable_connection[slot].host,
433 reusable_connection[slot].port, slot,
434 reusable_connection[slot].sfd,
435 reusable_connection[slot].keep_alive_timeout,
437 close_socket(reusable_connection[slot].sfd);
438 mark_connection_closed(&reusable_connection[slot]);
440 else if (!socket_is_still_alive(reusable_connection[slot].sfd))
442 log_error(LOG_LEVEL_CONNECT,
443 "The connection to %s:%d in slot %d is no longer usable. "
444 "Closing socket %d.", reusable_connection[slot].host,
445 reusable_connection[slot].port, slot,
446 reusable_connection[slot].sfd);
447 close_socket(reusable_connection[slot].sfd);
448 mark_connection_closed(&reusable_connection[slot]);
457 privoxy_mutex_unlock(&connection_reuse_mutex);
459 return connections_alive;
464 /*********************************************************************
466 * Function : get_reusable_connection
468 * Description : Returns an open socket to a previously remembered
469 * open connection (if there is one).
472 * 1 : http = The destination for the connection.
473 * 2 : fwd = The forwarder settings.
475 * Returns : JB_INVALID_SOCKET => No reusable connection found,
476 * otherwise a usable socket.
478 *********************************************************************/
479 static jb_socket get_reusable_connection(const struct http_request *http,
480 const struct forward_spec *fwd)
482 jb_socket sfd = JB_INVALID_SOCKET;
483 unsigned int slot = 0;
485 close_unusable_connections();
487 privoxy_mutex_lock(&connection_reuse_mutex);
489 for (slot = 0; slot < SZ(reusable_connection); slot++)
491 if (!reusable_connection[slot].in_use
492 && (JB_INVALID_SOCKET != reusable_connection[slot].sfd))
494 if (connection_destination_matches(&reusable_connection[slot], http, fwd))
496 reusable_connection[slot].in_use = TRUE;
497 sfd = reusable_connection[slot].sfd;
498 log_error(LOG_LEVEL_CONNECT,
499 "Found reusable socket %d for %s:%d in slot %d. Timestamp made %d "
500 "seconds ago. Timeout: %d. Latency: %d. Requests served: %d",
501 sfd, reusable_connection[slot].host, reusable_connection[slot].port,
502 slot, time(NULL) - reusable_connection[slot].timestamp,
503 reusable_connection[slot].keep_alive_timeout,
504 (int)(reusable_connection[slot].response_received -
505 reusable_connection[slot].request_sent),
506 reusable_connection[slot].requests_sent_total);
512 privoxy_mutex_unlock(&connection_reuse_mutex);
519 /*********************************************************************
521 * Function : mark_connection_unused
523 * Description : Gives a remembered connection free for reuse.
526 * 1 : connection = The connection in question.
528 * Returns : TRUE => Socket found and marked as unused.
529 * FALSE => Socket not found.
531 *********************************************************************/
532 static int mark_connection_unused(const struct reusable_connection *connection)
534 unsigned int slot = 0;
535 int socket_found = FALSE;
537 assert(connection->sfd != JB_INVALID_SOCKET);
539 privoxy_mutex_lock(&connection_reuse_mutex);
541 for (slot = 0; slot < SZ(reusable_connection); slot++)
543 if (reusable_connection[slot].sfd == connection->sfd)
545 assert(reusable_connection[slot].in_use);
547 log_error(LOG_LEVEL_CONNECT,
548 "Marking open socket %d for %s:%d in slot %d as unused.",
549 connection->sfd, reusable_connection[slot].host,
550 reusable_connection[slot].port, slot);
551 reusable_connection[slot].in_use = 0;
552 reusable_connection[slot].timestamp = connection->timestamp;
557 privoxy_mutex_unlock(&connection_reuse_mutex);
564 /*********************************************************************
566 * Function : set_keep_alive_timeout
568 * Description : Sets the timeout after which open
569 * connections will no longer be reused.
572 * 1 : timeout = The timeout in seconds.
576 *********************************************************************/
577 void set_keep_alive_timeout(unsigned int timeout)
579 keep_alive_timeout = timeout;
581 #endif /* def FEATURE_CONNECTION_SHARING */
584 /*********************************************************************
586 * Function : forwarded_connect
588 * Description : Connect to a specified web server, possibly via
589 * a HTTP proxy and/or a SOCKS proxy.
592 * 1 : fwd = the proxies to use when connecting.
593 * 2 : http = the http request and apropos headers
594 * 3 : csp = Current client state (buffers, headers, etc...)
596 * Returns : JB_INVALID_SOCKET => failure, else it is the socket file descriptor.
598 *********************************************************************/
599 jb_socket forwarded_connect(const struct forward_spec * fwd,
600 struct http_request *http,
601 struct client_state *csp)
603 const char * dest_host;
605 jb_socket sfd = JB_INVALID_SOCKET;
607 #ifdef FEATURE_CONNECTION_SHARING
608 if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING)
609 && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED))
611 sfd = get_reusable_connection(http, fwd);
612 if (JB_INVALID_SOCKET != sfd)
617 #endif /* def FEATURE_CONNECTION_SHARING */
619 /* Figure out if we need to connect to the web server or a HTTP proxy. */
620 if (fwd->forward_host)
623 dest_host = fwd->forward_host;
624 dest_port = fwd->forward_port;
629 dest_host = http->host;
630 dest_port = http->port;
633 /* Connect, maybe using a SOCKS proxy */
637 sfd = connect_to(dest_host, dest_port, csp);
641 sfd = socks4_connect(fwd, dest_host, dest_port, csp);
644 sfd = socks5_connect(fwd, dest_host, dest_port, csp);
647 /* Should never get here */
648 log_error(LOG_LEVEL_FATAL,
649 "Internal error in forwarded_connect(). Bad proxy type: %d", fwd->type);
652 if (JB_INVALID_SOCKET != sfd)
654 log_error(LOG_LEVEL_CONNECT,
655 "Created new connection to %s:%d on socket %d.",
656 http->host, http->port, sfd);
664 /*********************************************************************
666 * Function : socks4_connect
668 * Description : Connect to the SOCKS server, and connect through
669 * it to the specified server. This handles
670 * all the SOCKS negotiation, and returns a file
671 * descriptor for a socket which can be treated as a
672 * normal (non-SOCKS) socket.
674 * Logged error messages are saved to csp->error_message
675 * and later reused by error_response() for the CGI
676 * message. strdup allocation failures are handled there.
679 * 1 : fwd = Specifies the SOCKS proxy to use.
680 * 2 : target_host = The final server to connect to.
681 * 3 : target_port = The final port to connect to.
682 * 4 : csp = Current client state (buffers, headers, etc...)
684 * Returns : JB_INVALID_SOCKET => failure, else a socket file descriptor.
686 *********************************************************************/
687 static jb_socket socks4_connect(const struct forward_spec * fwd,
688 const char * target_host,
690 struct client_state *csp)
692 unsigned long web_server_addr;
693 char buf[BUFFER_SIZE];
694 struct socks_op *c = (struct socks_op *)buf;
695 struct socks_reply *s = (struct socks_reply *)buf;
702 if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
704 /* XXX: Shouldn't the config file parser prevent this? */
705 errstr = "NULL gateway host specified.";
709 if (fwd->gateway_port <= 0)
711 errstr = "invalid gateway port specified.";
717 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
718 csp->error_message = strdup(errstr);
720 return(JB_INVALID_SOCKET);
723 /* build a socks request for connection to the web server */
725 strlcpy(&(c->userid), socks_userid, sizeof(buf) - sizeof(struct socks_op));
727 csiz = sizeof(*c) + sizeof(socks_userid) - sizeof(c->userid) - sizeof(c->padding);
732 web_server_addr = resolve_hostname_to_ip(target_host);
733 if (web_server_addr == INADDR_NONE)
735 errstr = "could not resolve target host";
736 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s %s", errstr, target_host);
741 web_server_addr = htonl(web_server_addr);
745 web_server_addr = 0x00000001;
746 n = csiz + strlen(target_host) + 1;
750 errstr = "buffer cbuf too small.";
751 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
756 strlcpy(buf + csiz, target_host, sizeof(buf) - sizeof(struct socks_op) - csiz);
758 * What we forward to the socks4a server should have the
759 * size of socks_op, plus the length of the userid plus
760 * its \0 byte (which we don't have to add because the
761 * first byte of the userid is counted twice as it's also
762 * part of sock_op) minus the padding bytes (which are part
763 * of the userid as well), plus the length of the target_host
764 * (which is stored csiz bytes after the beginning of the buffer),
765 * plus another \0 byte.
767 assert(n == sizeof(struct socks_op) + strlen(&(c->userid)) - sizeof(c->padding) + strlen(buf + csiz) + 1);
772 /* Should never get here */
773 log_error(LOG_LEVEL_FATAL,
774 "socks4_connect: SOCKS4 impossible internal error - bad SOCKS type.");
776 return(JB_INVALID_SOCKET);
781 csp->error_message = strdup(errstr);
782 return(JB_INVALID_SOCKET);
787 c->dstport[0] = (unsigned char)((target_port >> 8 ) & 0xff);
788 c->dstport[1] = (unsigned char)((target_port ) & 0xff);
789 c->dstip[0] = (unsigned char)((web_server_addr >> 24) & 0xff);
790 c->dstip[1] = (unsigned char)((web_server_addr >> 16) & 0xff);
791 c->dstip[2] = (unsigned char)((web_server_addr >> 8) & 0xff);
792 c->dstip[3] = (unsigned char)((web_server_addr ) & 0xff);
794 /* pass the request to the socks server */
795 sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
797 if (sfd == JB_INVALID_SOCKET)
799 /* The error an its reason have already been logged by connect_to() */
800 return(JB_INVALID_SOCKET);
802 else if (write_socket(sfd, (char *)c, csiz))
804 errstr = "SOCKS4 negotiation write failed.";
805 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
809 else if (!data_is_available(sfd, csp->config->socket_timeout))
811 if (socket_is_still_alive(sfd))
813 errstr = "SOCKS4 negotiation timed out";
817 errstr = "SOCKS4 negotiation got aborted by the server";
819 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
823 else if (read_socket(sfd, buf, sizeof(buf)) != sizeof(*s))
825 errstr = "SOCKS4 negotiation read failed.";
826 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
833 csp->error_message = strdup(errstr);
834 return(JB_INVALID_SOCKET);
839 case SOCKS4_REQUEST_GRANTED:
841 case SOCKS4_REQUEST_REJECT:
842 errstr = "SOCKS request rejected or failed.";
845 case SOCKS4_REQUEST_IDENT_FAILED:
846 errstr = "SOCKS request rejected because "
847 "SOCKS server cannot connect to identd on the client.";
850 case SOCKS4_REQUEST_IDENT_CONFLICT:
851 errstr = "SOCKS request rejected because "
852 "the client program and identd report "
853 "different user-ids.";
858 snprintf(buf, sizeof(buf),
859 "SOCKS request rejected for reason code %d.", s->cd);
863 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
864 csp->error_message = strdup(errstr);
867 return(JB_INVALID_SOCKET);
871 /*********************************************************************
873 * Function : translate_socks5_error
875 * Description : Translates a SOCKS errors to a string.
878 * 1 : socks_error = The error code to translate.
880 * Returns : The string translation.
882 *********************************************************************/
883 static const char *translate_socks5_error(int socks_error)
887 /* XXX: these should be more descriptive */
888 case SOCKS5_REQUEST_FAILED:
889 return "SOCKS5 request failed";
890 case SOCKS5_REQUEST_DENIED:
891 return "SOCKS5 request denied";
892 case SOCKS5_REQUEST_NETWORK_UNREACHABLE:
893 return "SOCKS5 network unreachable";
894 case SOCKS5_REQUEST_HOST_UNREACHABLE:
895 return "SOCKS5 host unreachable";
896 case SOCKS5_REQUEST_CONNECTION_REFUSED:
897 return "SOCKS5 connection refused";
898 case SOCKS5_REQUEST_TTL_EXPIRED:
899 return "SOCKS5 TTL expired";
900 case SOCKS5_REQUEST_PROTOCOL_ERROR:
901 return "SOCKS5 client protocol error";
902 case SOCKS5_REQUEST_BAD_ADDRESS_TYPE:
903 return "SOCKS5 domain names unsupported";
904 case SOCKS5_REQUEST_GRANTED:
905 return "everything's peachy";
907 return "SOCKS5 negotiation protocol error";
911 /*********************************************************************
913 * Function : socks5_connect
915 * Description : Connect to the SOCKS server, and connect through
916 * it to the specified server. This handles
917 * all the SOCKS negotiation, and returns a file
918 * descriptor for a socket which can be treated as a
919 * normal (non-SOCKS) socket.
922 * 1 : fwd = Specifies the SOCKS proxy to use.
923 * 2 : target_host = The final server to connect to.
924 * 3 : target_port = The final port to connect to.
925 * 4 : csp = Current client state (buffers, headers, etc...)
927 * Returns : JB_INVALID_SOCKET => failure, else a socket file descriptor.
929 *********************************************************************/
930 static jb_socket socks5_connect(const struct forward_spec *fwd,
931 const char *target_host,
933 struct client_state *csp)
938 size_t client_pos = 0;
942 const char *errstr = NULL;
944 assert(fwd->gateway_host);
945 if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
947 errstr = "NULL gateway host specified";
951 if (fwd->gateway_port <= 0)
954 * XXX: currently this can't happen because in
955 * case of invalid gateway ports we use the defaults.
956 * Of course we really shouldn't do that.
958 errstr = "invalid gateway port specified";
962 hostlen = strlen(target_host);
963 if (hostlen > (size_t)255)
965 errstr = "target host name is longer than 255 characters";
969 if (fwd->type != SOCKS_5)
971 /* Should never get here */
972 log_error(LOG_LEVEL_FATAL,
973 "SOCKS5 impossible internal error - bad SOCKS type");
980 assert(errstr != NULL);
981 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
982 csp->error_message = strdup(errstr);
983 return(JB_INVALID_SOCKET);
986 /* pass the request to the socks server */
987 sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
989 if (sfd == JB_INVALID_SOCKET)
991 errstr = "socks5 server unreachable";
992 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
993 /* Free the generic error message provided by connect_to() */
994 freez(csp->error_message);
995 csp->error_message = strdup(errstr);
996 return(JB_INVALID_SOCKET);
1000 cbuf[client_pos++] = '\x05'; /* Version */
1001 cbuf[client_pos++] = '\x01'; /* One authentication method supported */
1002 cbuf[client_pos++] = '\x00'; /* The no authentication authentication method */
1004 if (write_socket(sfd, cbuf, client_pos))
1006 errstr = "SOCKS5 negotiation write failed";
1007 csp->error_message = strdup(errstr);
1008 log_error(LOG_LEVEL_CONNECT, "%s", errstr);
1010 return(JB_INVALID_SOCKET);
1013 if (!data_is_available(sfd, csp->config->socket_timeout))
1015 if (socket_is_still_alive(sfd))
1017 errstr = "SOCKS5 negotiation timed out";
1021 errstr = "SOCKS5 negotiation got aborted by the server";
1026 if (!err && read_socket(sfd, sbuf, sizeof(sbuf)) != 2)
1028 errstr = "SOCKS5 negotiation read failed";
1032 if (!err && (sbuf[0] != '\x05'))
1034 errstr = "SOCKS5 negotiation protocol version error";
1038 if (!err && (sbuf[1] == '\xff'))
1040 errstr = "SOCKS5 authentication required";
1044 if (!err && (sbuf[1] != '\x00'))
1046 errstr = "SOCKS5 negotiation protocol error";
1052 assert(errstr != NULL);
1053 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1054 csp->error_message = strdup(errstr);
1057 return(JB_INVALID_SOCKET);
1061 cbuf[client_pos++] = '\x05'; /* Version */
1062 cbuf[client_pos++] = '\x01'; /* TCP connect */
1063 cbuf[client_pos++] = '\x00'; /* Reserved, must be 0x00 */
1064 cbuf[client_pos++] = '\x03'; /* Address is domain name */
1065 cbuf[client_pos++] = (char)(hostlen & 0xffu);
1066 assert(sizeof(cbuf) - client_pos > (size_t)255);
1067 /* Using strncpy because we really want the nul byte padding. */
1068 strncpy(cbuf + client_pos, target_host, sizeof(cbuf) - client_pos);
1069 client_pos += (hostlen & 0xffu);
1070 cbuf[client_pos++] = (char)((target_port >> 8) & 0xff);
1071 cbuf[client_pos++] = (char)((target_port ) & 0xff);
1073 if (write_socket(sfd, cbuf, client_pos))
1075 errstr = "SOCKS5 negotiation write failed";
1076 csp->error_message = strdup(errstr);
1077 log_error(LOG_LEVEL_CONNECT, "%s", errstr);
1080 return(JB_INVALID_SOCKET);
1083 server_size = read_socket(sfd, sbuf, sizeof(sbuf));
1084 if (server_size != sizeof(sbuf))
1086 errstr = "SOCKS5 negotiation read failed";
1090 if (sbuf[0] != '\x05')
1092 errstr = "SOCKS5 negotiation protocol version error";
1094 else if (sbuf[2] != '\x00')
1096 errstr = "SOCKS5 negotiation protocol error";
1098 else if (sbuf[1] != SOCKS5_REQUEST_GRANTED)
1100 errstr = translate_socks5_error(sbuf[1]);
1108 assert(errstr != NULL);
1109 csp->error_message = strdup(errstr);
1110 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1114 return(JB_INVALID_SOCKET);