1 const char gateway_rcs[] = "$Id: gateway.c,v 1.55 2009/07/05 12:01:45 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 SourceForge
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_KEEP_ALIVE
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 connection for reuse later on.
175 * 1 : csp = Current client state (buffers, headers, etc...)
176 * 2 : fwd = The forwarder settings used.
180 *********************************************************************/
181 void remember_connection(const struct client_state *csp, const struct forward_spec *fwd)
183 unsigned int slot = 0;
184 int free_slot_found = FALSE;
185 const struct reusable_connection *connection = &csp->server_connection;
186 const struct http_request *http = csp->http;
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, http->host, http->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 http->host, http->port, slot);
216 privoxy_mutex_unlock(&connection_reuse_mutex);
217 close_socket(connection->sfd);
221 assert(NULL != http->host);
222 reusable_connection[slot].host = strdup(http->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 = http->port;
229 reusable_connection[slot].in_use = 0;
230 reusable_connection[slot].timestamp = connection->timestamp;
231 reusable_connection[slot].keep_alive_timeout = connection->keep_alive_timeout;
234 assert(reusable_connection[slot].gateway_host == NULL);
235 assert(reusable_connection[slot].gateway_port == 0);
236 assert(reusable_connection[slot].forwarder_type == SOCKS_NONE);
237 assert(reusable_connection[slot].forward_host == NULL);
238 assert(reusable_connection[slot].forward_port == 0);
240 reusable_connection[slot].forwarder_type = fwd->type;
241 if (NULL != fwd->gateway_host)
243 reusable_connection[slot].gateway_host = strdup(fwd->gateway_host);
244 if (NULL == reusable_connection[slot].gateway_host)
246 log_error(LOG_LEVEL_FATAL, "Out of memory saving gateway_host.");
251 reusable_connection[slot].gateway_host = NULL;
253 reusable_connection[slot].gateway_port = fwd->gateway_port;
255 if (NULL != fwd->forward_host)
257 reusable_connection[slot].forward_host = strdup(fwd->forward_host);
258 if (NULL == reusable_connection[slot].forward_host)
260 log_error(LOG_LEVEL_FATAL, "Out of memory saving forward_host.");
265 reusable_connection[slot].forward_host = NULL;
267 reusable_connection[slot].forward_port = fwd->forward_port;
269 privoxy_mutex_unlock(&connection_reuse_mutex);
273 /*********************************************************************
275 * Function : mark_connection_closed
277 * Description : Marks a reused connection closed.
280 * 1 : closed_connection = The connection to mark as closed.
284 *********************************************************************/
285 void mark_connection_closed(struct reusable_connection *closed_connection)
287 closed_connection->in_use = FALSE;
288 closed_connection->sfd = JB_INVALID_SOCKET;
289 freez(closed_connection->host);
290 closed_connection->port = 0;
291 closed_connection->timestamp = 0;
292 closed_connection->keep_alive_timeout = 0;
293 closed_connection->forwarder_type = SOCKS_NONE;
294 freez(closed_connection->gateway_host);
295 closed_connection->gateway_port = 0;
296 freez(closed_connection->forward_host);
297 closed_connection->forward_port = 0;
301 /*********************************************************************
303 * Function : forget_connection
305 * Description : Removes a previously remembered connection from
306 * the list of reusable connections.
309 * 1 : sfd = The socket belonging to the connection in question.
313 *********************************************************************/
314 void forget_connection(jb_socket sfd)
316 unsigned int slot = 0;
318 assert(sfd != JB_INVALID_SOCKET);
320 privoxy_mutex_lock(&connection_reuse_mutex);
322 for (slot = 0; slot < SZ(reusable_connection); slot++)
324 if (reusable_connection[slot].sfd == sfd)
326 assert(reusable_connection[slot].in_use);
328 log_error(LOG_LEVEL_CONNECT,
329 "Forgetting socket %d for %s:%d in slot %d.",
330 sfd, reusable_connection[slot].host,
331 reusable_connection[slot].port, slot);
332 mark_connection_closed(&reusable_connection[slot]);
333 privoxy_mutex_unlock(&connection_reuse_mutex);
339 log_error(LOG_LEVEL_CONNECT,
340 "Socket %d already forgotten or never remembered.", sfd);
342 privoxy_mutex_unlock(&connection_reuse_mutex);
346 /*********************************************************************
348 * Function : connection_destination_matches
350 * Description : Determines whether a remembered connection can
351 * be reused. That is, whether the destination and
352 * the forwarding settings match.
355 * 1 : connection = The connection to check.
356 * 2 : http = The destination for the connection.
357 * 3 : fwd = The forwarder settings.
359 * Returns : TRUE for yes, FALSE otherwise.
361 *********************************************************************/
362 int connection_destination_matches(const struct reusable_connection *connection,
363 const struct http_request *http,
364 const struct forward_spec *fwd)
366 if ((connection->forwarder_type != fwd->type)
367 || (connection->gateway_port != fwd->gateway_port)
368 || (connection->forward_port != fwd->forward_port)
369 || (connection->port != http->port))
374 if (( (NULL != connection->gateway_host)
375 && (NULL != fwd->gateway_host)
376 && strcmpic(connection->gateway_host, fwd->gateway_host))
377 && (connection->gateway_host != fwd->gateway_host))
379 log_error(LOG_LEVEL_CONNECT, "Gateway mismatch.");
383 if (( (NULL != connection->forward_host)
384 && (NULL != fwd->forward_host)
385 && strcmpic(connection->forward_host, fwd->forward_host))
386 && (connection->forward_host != fwd->forward_host))
388 log_error(LOG_LEVEL_CONNECT, "Forwarding proxy mismatch.");
392 return (!strcmpic(connection->host, http->host));
397 /*********************************************************************
399 * Function : close_unusable_connections
401 * Description : Closes remembered connections that have timed
402 * out or have been closed on the other side.
406 * Returns : Number of connections that are still alive.
408 *********************************************************************/
409 int close_unusable_connections(void)
411 unsigned int slot = 0;
412 int connections_alive = 0;
414 privoxy_mutex_lock(&connection_reuse_mutex);
416 for (slot = 0; slot < SZ(reusable_connection); slot++)
418 if (!reusable_connection[slot].in_use
419 && (JB_INVALID_SOCKET != reusable_connection[slot].sfd))
421 time_t time_open = time(NULL) - reusable_connection[slot].timestamp;
423 if (reusable_connection[slot].keep_alive_timeout < time_open)
425 log_error(LOG_LEVEL_CONNECT,
426 "The connection to %s:%d in slot %d timed out. "
427 "Closing socket %d. Timeout is: %d.",
428 reusable_connection[slot].host,
429 reusable_connection[slot].port, slot,
430 reusable_connection[slot].sfd,
431 reusable_connection[slot].keep_alive_timeout);
432 close_socket(reusable_connection[slot].sfd);
433 mark_connection_closed(&reusable_connection[slot]);
435 else if (!socket_is_still_usable(reusable_connection[slot].sfd))
437 log_error(LOG_LEVEL_CONNECT,
438 "The connection to %s:%d in slot %d is no longer usable. "
439 "Closing socket %d.", reusable_connection[slot].host,
440 reusable_connection[slot].port, slot,
441 reusable_connection[slot].sfd);
442 close_socket(reusable_connection[slot].sfd);
443 mark_connection_closed(&reusable_connection[slot]);
452 privoxy_mutex_unlock(&connection_reuse_mutex);
454 return connections_alive;
459 /*********************************************************************
461 * Function : get_reusable_connection
463 * Description : Returns an open socket to a previously remembered
464 * open connection (if there is one).
467 * 1 : http = The destination for the connection.
468 * 2 : fwd = The forwarder settings.
470 * Returns : JB_INVALID_SOCKET => No reusable connection found,
471 * otherwise a usable socket.
473 *********************************************************************/
474 static jb_socket get_reusable_connection(const struct http_request *http,
475 const struct forward_spec *fwd)
477 jb_socket sfd = JB_INVALID_SOCKET;
478 unsigned int slot = 0;
480 close_unusable_connections();
482 privoxy_mutex_lock(&connection_reuse_mutex);
484 for (slot = 0; slot < SZ(reusable_connection); slot++)
486 if (!reusable_connection[slot].in_use
487 && (JB_INVALID_SOCKET != reusable_connection[slot].sfd))
489 if (connection_destination_matches(&reusable_connection[slot], http, fwd))
491 reusable_connection[slot].in_use = TRUE;
492 sfd = reusable_connection[slot].sfd;
493 log_error(LOG_LEVEL_CONNECT,
494 "Found reusable socket %d for %s:%d in slot %d.",
495 sfd, reusable_connection[slot].host, reusable_connection[slot].port, slot);
501 privoxy_mutex_unlock(&connection_reuse_mutex);
508 /*********************************************************************
510 * Function : mark_connection_unused
512 * Description : Gives a remembered connection free for reuse.
515 * 1 : connection = The connection in question.
517 * Returns : TRUE => Socket found and marked as unused.
518 * FALSE => Socket not found.
520 *********************************************************************/
521 static int mark_connection_unused(const struct reusable_connection *connection)
523 unsigned int slot = 0;
524 int socket_found = FALSE;
526 assert(connection->sfd != JB_INVALID_SOCKET);
528 privoxy_mutex_lock(&connection_reuse_mutex);
530 for (slot = 0; slot < SZ(reusable_connection); slot++)
532 if (reusable_connection[slot].sfd == connection->sfd)
534 assert(reusable_connection[slot].in_use);
536 log_error(LOG_LEVEL_CONNECT,
537 "Marking open socket %d for %s:%d in slot %d as unused.",
538 connection->sfd, reusable_connection[slot].host,
539 reusable_connection[slot].port, slot);
540 reusable_connection[slot].in_use = 0;
541 reusable_connection[slot].timestamp = connection->timestamp;
546 privoxy_mutex_unlock(&connection_reuse_mutex);
553 /*********************************************************************
555 * Function : set_keep_alive_timeout
557 * Description : Sets the timeout after which open
558 * connections will no longer be reused.
561 * 1 : timeout = The timeout in seconds.
565 *********************************************************************/
566 void set_keep_alive_timeout(unsigned int timeout)
568 keep_alive_timeout = timeout;
570 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
573 /*********************************************************************
575 * Function : forwarded_connect
577 * Description : Connect to a specified web server, possibly via
578 * a HTTP proxy and/or a SOCKS proxy.
581 * 1 : fwd = the proxies to use when connecting.
582 * 2 : http = the http request and apropos headers
583 * 3 : csp = Current client state (buffers, headers, etc...)
585 * Returns : JB_INVALID_SOCKET => failure, else it is the socket file descriptor.
587 *********************************************************************/
588 jb_socket forwarded_connect(const struct forward_spec * fwd,
589 struct http_request *http,
590 struct client_state *csp)
592 const char * dest_host;
594 jb_socket sfd = JB_INVALID_SOCKET;
596 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
597 if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING)
598 && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED))
600 sfd = get_reusable_connection(http, fwd);
601 if (JB_INVALID_SOCKET != sfd)
606 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
608 /* Figure out if we need to connect to the web server or a HTTP proxy. */
609 if (fwd->forward_host)
612 dest_host = fwd->forward_host;
613 dest_port = fwd->forward_port;
618 dest_host = http->host;
619 dest_port = http->port;
622 /* Connect, maybe using a SOCKS proxy */
626 sfd = connect_to(dest_host, dest_port, csp);
630 sfd = socks4_connect(fwd, dest_host, dest_port, csp);
633 sfd = socks5_connect(fwd, dest_host, dest_port, csp);
636 /* Should never get here */
637 log_error(LOG_LEVEL_FATAL,
638 "SOCKS4 impossible internal error - bad SOCKS type.");
641 if (JB_INVALID_SOCKET != sfd)
643 log_error(LOG_LEVEL_CONNECT,
644 "Created new connection to %s:%d on socket %d.",
645 http->host, http->port, sfd);
653 /*********************************************************************
655 * Function : socks4_connect
657 * Description : Connect to the SOCKS server, and connect through
658 * it to the specified server. This handles
659 * all the SOCKS negotiation, and returns a file
660 * descriptor for a socket which can be treated as a
661 * normal (non-SOCKS) socket.
663 * Logged error messages are saved to csp->error_message
664 * and later reused by error_response() for the CGI
665 * message. strdup allocation failures are handled there.
668 * 1 : fwd = Specifies the SOCKS proxy to use.
669 * 2 : target_host = The final server to connect to.
670 * 3 : target_port = The final port to connect to.
671 * 4 : csp = Current client state (buffers, headers, etc...)
673 * Returns : JB_INVALID_SOCKET => failure, else a socket file descriptor.
675 *********************************************************************/
676 static jb_socket socks4_connect(const struct forward_spec * fwd,
677 const char * target_host,
679 struct client_state *csp)
681 unsigned int web_server_addr;
682 char buf[BUFFER_SIZE];
683 struct socks_op *c = (struct socks_op *)buf;
684 struct socks_reply *s = (struct socks_reply *)buf;
691 if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
693 /* XXX: Shouldn't the config file parser prevent this? */
694 errstr = "NULL gateway host specified.";
698 if (fwd->gateway_port <= 0)
700 errstr = "invalid gateway port specified.";
706 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
707 csp->error_message = strdup(errstr);
709 return(JB_INVALID_SOCKET);
712 /* build a socks request for connection to the web server */
714 strlcpy(&(c->userid), socks_userid, sizeof(buf) - sizeof(struct socks_op));
716 csiz = sizeof(*c) + sizeof(socks_userid) - sizeof(c->userid) - sizeof(c->padding);
721 web_server_addr = resolve_hostname_to_ip(target_host);
722 if (web_server_addr == INADDR_NONE)
724 errstr = "could not resolve target host";
725 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s %s", errstr, target_host);
730 web_server_addr = htonl(web_server_addr);
734 web_server_addr = 0x00000001;
735 n = csiz + strlen(target_host) + 1;
739 errstr = "buffer cbuf too small.";
740 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
745 strlcpy(buf + csiz, target_host, sizeof(buf) - sizeof(struct socks_op) - csiz);
747 * What we forward to the socks4a server should have the
748 * size of socks_op, plus the length of the userid plus
749 * its \0 byte (which we don't have to add because the
750 * first byte of the userid is counted twice as it's also
751 * part of sock_op) minus the padding bytes (which are part
752 * of the userid as well), plus the length of the target_host
753 * (which is stored csiz bytes after the beginning of the buffer),
754 * plus another \0 byte.
756 assert(n == sizeof(struct socks_op) + strlen(&(c->userid)) - sizeof(c->padding) + strlen(buf + csiz) + 1);
761 /* Should never get here */
762 log_error(LOG_LEVEL_FATAL,
763 "socks4_connect: SOCKS4 impossible internal error - bad SOCKS type.");
765 return(JB_INVALID_SOCKET);
770 csp->error_message = strdup(errstr);
771 return(JB_INVALID_SOCKET);
776 c->dstport[0] = (unsigned char)((target_port >> 8 ) & 0xff);
777 c->dstport[1] = (unsigned char)((target_port ) & 0xff);
778 c->dstip[0] = (unsigned char)((web_server_addr >> 24 ) & 0xff);
779 c->dstip[1] = (unsigned char)((web_server_addr >> 16 ) & 0xff);
780 c->dstip[2] = (unsigned char)((web_server_addr >> 8 ) & 0xff);
781 c->dstip[3] = (unsigned char)((web_server_addr ) & 0xff);
783 /* pass the request to the socks server */
784 sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
786 if (sfd == JB_INVALID_SOCKET)
789 * XXX: connect_to should fill in the exact reason.
790 * Most likely resolving the IP of the forwarder failed.
792 errstr = "connect_to failed: see logfile for details";
795 else if (write_socket(sfd, (char *)c, csiz))
797 errstr = "SOCKS4 negotiation write failed.";
798 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
802 else if (read_socket(sfd, buf, sizeof(buf)) != sizeof(*s))
804 errstr = "SOCKS4 negotiation read failed.";
805 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
812 csp->error_message = strdup(errstr);
813 return(JB_INVALID_SOCKET);
818 case SOCKS_REQUEST_GRANTED:
820 case SOCKS_REQUEST_REJECT:
821 errstr = "SOCKS request rejected or failed.";
824 case SOCKS_REQUEST_IDENT_FAILED:
825 errstr = "SOCKS request rejected because "
826 "SOCKS server cannot connect to identd on the client.";
829 case SOCKS_REQUEST_IDENT_CONFLICT:
830 errstr = "SOCKS request rejected because "
831 "the client program and identd report "
832 "different user-ids.";
837 snprintf(buf, sizeof(buf),
838 "SOCKS request rejected for reason code %d.", s->cd);
842 log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
843 csp->error_message = strdup(errstr);
846 return(JB_INVALID_SOCKET);
850 /*********************************************************************
852 * Function : translate_socks5_error
854 * Description : Translates a SOCKS errors to a string.
857 * 1 : socks_error = The error code to translate.
859 * Returns : The string translation.
861 *********************************************************************/
862 static const char *translate_socks5_error(int socks_error)
866 /* XXX: these should be more descriptive */
867 case SOCKS5_REQUEST_FAILED:
868 return "SOCKS5 request failed";
869 case SOCKS5_REQUEST_DENIED:
870 return "SOCKS5 request denied";
871 case SOCKS5_REQUEST_NETWORK_UNREACHABLE:
872 return "SOCKS5 network unreachable";
873 case SOCKS5_REQUEST_HOST_UNREACHABLE:
874 return "SOCKS5 host unreachable";
875 case SOCKS5_REQUEST_CONNECTION_REFUSED:
876 return "SOCKS5 connection refused";
877 case SOCKS5_REQUEST_TTL_EXPIRED:
878 return "SOCKS5 TTL expired";
879 case SOCKS5_REQUEST_PROTOCOL_ERROR:
880 return "SOCKS5 client protocol error";
881 case SOCKS5_REQUEST_BAD_ADDRESS_TYPE:
882 return "SOCKS5 domain names unsupported";
883 case SOCKS5_REQUEST_GRANTED:
884 return "everything's peachy";
886 return "SOCKS5 negotiation protocol error";
890 /*********************************************************************
892 * Function : socks5_connect
894 * Description : Connect to the SOCKS server, and connect through
895 * it to the specified server. This handles
896 * all the SOCKS negotiation, and returns a file
897 * descriptor for a socket which can be treated as a
898 * normal (non-SOCKS) socket.
901 * 1 : fwd = Specifies the SOCKS proxy to use.
902 * 2 : target_host = The final server to connect to.
903 * 3 : target_port = The final port to connect to.
904 * 4 : csp = Current client state (buffers, headers, etc...)
906 * Returns : JB_INVALID_SOCKET => failure, else a socket file descriptor.
908 *********************************************************************/
909 static jb_socket socks5_connect(const struct forward_spec *fwd,
910 const char *target_host,
912 struct client_state *csp)
917 size_t client_pos = 0;
921 const char *errstr = NULL;
923 assert(fwd->gateway_host);
924 if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
926 errstr = "NULL gateway host specified";
930 if (fwd->gateway_port <= 0)
933 * XXX: currently this can't happen because in
934 * case of invalid gateway ports we use the defaults.
935 * Of course we really shouldn't do that.
937 errstr = "invalid gateway port specified";
941 hostlen = strlen(target_host);
942 if (hostlen > (size_t)255)
944 errstr = "target host name is longer than 255 characters";
948 if (fwd->type != SOCKS_5)
950 /* Should never get here */
951 log_error(LOG_LEVEL_FATAL,
952 "SOCKS5 impossible internal error - bad SOCKS type");
959 assert(errstr != NULL);
960 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
961 csp->error_message = strdup(errstr);
962 return(JB_INVALID_SOCKET);
965 /* pass the request to the socks server */
966 sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
968 if (sfd == JB_INVALID_SOCKET)
970 errstr = "socks5 server unreachable";
971 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
972 csp->error_message = strdup(errstr);
973 return(JB_INVALID_SOCKET);
977 cbuf[client_pos++] = '\x05'; /* Version */
978 cbuf[client_pos++] = '\x01'; /* One authentication method supported */
979 cbuf[client_pos++] = '\x00'; /* The no authentication authentication method */
981 if (write_socket(sfd, cbuf, client_pos))
983 errstr = "SOCKS5 negotiation write failed";
984 csp->error_message = strdup(errstr);
985 log_error(LOG_LEVEL_CONNECT, "%s", errstr);
987 return(JB_INVALID_SOCKET);
990 if (read_socket(sfd, sbuf, sizeof(sbuf)) != 2)
992 errstr = "SOCKS5 negotiation read failed";
996 if (!err && (sbuf[0] != '\x05'))
998 errstr = "SOCKS5 negotiation protocol version error";
1002 if (!err && (sbuf[1] == '\xff'))
1004 errstr = "SOCKS5 authentication required";
1008 if (!err && (sbuf[1] != '\x00'))
1010 errstr = "SOCKS5 negotiation protocol error";
1016 assert(errstr != NULL);
1017 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1018 csp->error_message = strdup(errstr);
1021 return(JB_INVALID_SOCKET);
1025 cbuf[client_pos++] = '\x05'; /* Version */
1026 cbuf[client_pos++] = '\x01'; /* TCP connect */
1027 cbuf[client_pos++] = '\x00'; /* Reserved, must be 0x00 */
1028 cbuf[client_pos++] = '\x03'; /* Address is domain name */
1029 cbuf[client_pos++] = (char)(hostlen & 0xffu);
1030 assert(sizeof(cbuf) - client_pos > (size_t)255);
1031 /* Using strncpy because we really want the nul byte padding. */
1032 strncpy(cbuf + client_pos, target_host, sizeof(cbuf) - client_pos);
1033 client_pos += (hostlen & 0xffu);
1034 cbuf[client_pos++] = (char)((target_port >> 8) & 0xff);
1035 cbuf[client_pos++] = (char)((target_port ) & 0xff);
1037 if (write_socket(sfd, cbuf, client_pos))
1039 errstr = "SOCKS5 negotiation read failed";
1040 csp->error_message = strdup(errstr);
1041 log_error(LOG_LEVEL_CONNECT, "%s", errstr);
1044 return(JB_INVALID_SOCKET);
1047 server_size = read_socket(sfd, sbuf, sizeof(sbuf));
1048 if (server_size < 3)
1050 errstr = "SOCKS5 negotiation read failed";
1053 else if (server_size > 20)
1055 /* This is somewhat unexpected but doesn't realy matter. */
1056 log_error(LOG_LEVEL_CONNECT, "socks5_connect: read %d bytes "
1057 "from socks server. Would have accepted up to %d.",
1058 server_size, sizeof(sbuf));
1061 if (!err && (sbuf[0] != '\x05'))
1063 errstr = "SOCKS5 negotiation protocol version error";
1067 if (!err && (sbuf[2] != '\x00'))
1069 errstr = "SOCKS5 negotiation protocol error";
1075 if (sbuf[1] == SOCKS5_REQUEST_GRANTED)
1079 errstr = translate_socks5_error(sbuf[1]);
1082 assert(errstr != NULL);
1083 csp->error_message = strdup(errstr);
1084 log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1088 return(JB_INVALID_SOCKET);