- Update copyright range.
[privoxy.git] / gateway.c
1 const char gateway_rcs[] = "$Id: gateway.c,v 1.43 2008/11/13 09:15:51 fabiankeil Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/gateway.c,v $
5  *
6  * Purpose     :  Contains functions to connect to a server, possibly
7  *                using a "forwarder" (i.e. HTTP proxy and/or a SOCKS4
8  *                proxy).
9  *
10  * Copyright   :  Written by and Copyright (C) 2001-2008 the SourceForge
11  *                Privoxy team. http://www.privoxy.org/
12  *
13  *                Based on the Internet Junkbuster originally written
14  *                by and Copyright (C) 1997 Anonymous Coders and
15  *                Junkbusters Corporation.  http://www.junkbusters.com
16  *
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.
22  *
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.
28  *
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.
34  *
35  * Revisions   :
36  *    $Log: gateway.c,v $
37  *    Revision 1.43  2008/11/13 09:15:51  fabiankeil
38  *    Make keep_alive_timeout static.
39  *
40  *    Revision 1.42  2008/11/13 09:08:42  fabiankeil
41  *    Add new config option: keep-alive-timeout.
42  *
43  *    Revision 1.41  2008/11/08 15:29:58  fabiankeil
44  *    Unify two debug messages.
45  *
46  *    Revision 1.40  2008/11/08 15:14:05  fabiankeil
47  *    Fix duplicated debugging check.
48  *
49  *    Revision 1.39  2008/10/25 11:33:01  fabiankeil
50  *    Remove already out-commented line left over from debugging.
51  *
52  *    Revision 1.38  2008/10/24 17:33:00  fabiankeil
53  *    - Tone the "keep-alive support is experimental" warning
54  *      down a bit as hackish 0-chunk detection has been
55  *      implemented recently.
56  *    - Only show the "ndef HAVE_POLL" warning once on start-up.
57  *
58  *    Revision 1.37  2008/10/23 17:40:53  fabiankeil
59  *    Fix forget_connection() and mark_connection_unused(),
60  *    which would both under certain circumstances access
61  *    reusable_connection[MAX_REUSABLE_CONNECTIONS]. Oops.
62  *
63  *    Revision 1.36  2008/10/18 19:49:15  fabiankeil
64  *    - Factor close_unusable_connections() out of
65  *      get_reusable_connection() to make sure we really check
66  *      all the remembered connections, not just the ones before
67  *      the next reusable one.
68  *    - Plug two file descriptor leaks. Internally marking
69  *      connections as closed doesn't cut it.
70  *
71  *    Revision 1.35  2008/10/17 17:12:01  fabiankeil
72  *    In socket_is_still_usable(), use select()
73  *    and FD_ISSET() if poll() isn't available.
74  *
75  *    Revision 1.34  2008/10/17 17:07:13  fabiankeil
76  *    Add preliminary timeout support.
77  *
78  *    Revision 1.33  2008/10/16 16:34:21  fabiankeil
79  *    Fix two gcc44 warnings.
80  *
81  *    Revision 1.32  2008/10/16 16:27:22  fabiankeil
82  *    Fix compiler warning.
83  *
84  *    Revision 1.31  2008/10/16 07:31:11  fabiankeil
85  *    - Factor socket_is_still_usable() out of get_reusable_connection().
86  *    - If poll() isn't available, show a warning and assume the socket
87  *      is still usable.
88  *
89  *    Revision 1.30  2008/10/13 17:31:03  fabiankeil
90  *    If a remembered connection is no longer usable and
91  *    has been marked closed, don't bother checking if the
92  *    destination matches.
93  *
94  *    Revision 1.29  2008/10/11 16:59:41  fabiankeil
95  *    Add missing dots for two log messages.
96  *
97  *    Revision 1.28  2008/10/09 18:21:41  fabiankeil
98  *    Flush work-in-progress changes to keep outgoing connections
99  *    alive where possible. Incomplete and mostly #ifdef'd out.
100  *
101  *    Revision 1.27  2008/09/27 15:05:51  fabiankeil
102  *    Return only once in forwarded_connect().
103  *
104  *    Revision 1.26  2008/08/18 17:42:06  fabiankeil
105  *    Fix typo in macro name.
106  *
107  *    Revision 1.25  2008/02/07 18:09:46  fabiankeil
108  *    In socks5_connect:
109  *    - make the buffers quite a bit smaller.
110  *    - properly report "socks5 server unreachable" failures.
111  *    - let strncpy() use the whole buffer. Using a length of 0xffu wasn't actually
112  *      wrong, but requires too much thinking as it doesn't depend on the buffer size.
113  *    - log a message if the socks5 server sends more data than expected.
114  *    - add some assertions and comments.
115  *
116  *    Revision 1.24  2008/02/04 14:56:29  fabiankeil
117  *    - Fix a compiler warning.
118  *    - Stop assuming that htonl(INADDR_NONE) equals INADDR_NONE.
119  *
120  *    Revision 1.23  2008/02/04 13:11:35  fabiankeil
121  *    Remember the cause of the SOCKS5 error for the CGI message.
122  *
123  *    Revision 1.22  2008/02/03 13:46:15  fabiankeil
124  *    Add SOCKS5 support. Patch #1862863 by Eric M. Hopper with minor changes.
125  *
126  *    Revision 1.21  2007/07/28 12:30:03  fabiankeil
127  *    Modified patch from Song Weijia (#1762559) to
128  *    fix socks requests on big-endian platforms.
129  *
130  *    Revision 1.20  2007/05/14 10:23:48  fabiankeil
131  *    - Use strlcpy() instead of strcpy().
132  *    - Use the same buffer for socks requests and socks responses.
133  *    - Fix bogus warning about web_server_addr being used uninitialized.
134  *
135  *    Revision 1.19  2007/01/25 14:09:45  fabiankeil
136  *    - Save errors in socks4_connect() to csp->error_message.
137  *    - Silence some gcc43 warnings, hopefully the right way.
138  *
139  *    Revision 1.18  2006/07/18 14:48:46  david__schmidt
140  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
141  *    with what was really the latest development (the v_3_0_branch branch)
142  *
143  *    Revision 1.16  2002/05/12 21:36:29  jongfoster
144  *    Correcting function comments
145  *
146  *    Revision 1.15  2002/03/26 22:29:54  swa
147  *    we have a new homepage!
148  *
149  *    Revision 1.14  2002/03/24 13:25:43  swa
150  *    name change related issues
151  *
152  *    Revision 1.13  2002/03/13 00:29:59  jongfoster
153  *    Killing warnings
154  *
155  *    Revision 1.12  2002/03/09 20:03:52  jongfoster
156  *    - Making various functions return int rather than size_t.
157  *      (Undoing a recent change).  Since size_t is unsigned on
158  *      Windows, functions like read_socket that return -1 on
159  *      error cannot return a size_t.
160  *
161  *      THIS WAS A MAJOR BUG - it caused frequent, unpredictable
162  *      crashes, and also frequently caused JB to jump to 100%
163  *      CPU and stay there.  (Because it thought it had just
164  *      read ((unsigned)-1) == 4Gb of data...)
165  *
166  *    - The signature of write_socket has changed, it now simply
167  *      returns success=0/failure=nonzero.
168  *
169  *    - Trying to get rid of a few warnings --with-debug on
170  *      Windows, I've introduced a new type "jb_socket".  This is
171  *      used for the socket file descriptors.  On Windows, this
172  *      is SOCKET (a typedef for unsigned).  Everywhere else, it's
173  *      an int.  The error value can't be -1 any more, so it's
174  *      now JB_INVALID_SOCKET (which is -1 on UNIX, and in
175  *      Windows it maps to the #define INVALID_SOCKET.)
176  *
177  *    - The signature of bind_port has changed.
178  *
179  *    Revision 1.11  2002/03/08 17:46:04  jongfoster
180  *    Fixing int/size_t warnings
181  *
182  *    Revision 1.10  2002/03/07 03:50:19  oes
183  *     - Improved handling of failed DNS lookups
184  *     - Fixed compiler warnings
185  *
186  *    Revision 1.9  2001/10/25 03:40:48  david__schmidt
187  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
188  *    threads to call select() simultaneously.  So, it's time to do a real, live,
189  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
190  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
191  *
192  *    Revision 1.8  2001/09/13 20:10:12  jongfoster
193  *    Fixing missing #include under Windows
194  *
195  *    Revision 1.7  2001/09/12 17:58:26  steudten
196  *
197  *    add #include <string.h>
198  *
199  *    Revision 1.6  2001/09/10 10:41:16  oes
200  *    Added #include in.h
201  *
202  *    Revision 1.5  2001/07/29 18:47:57  jongfoster
203  *    Adding missing #include project.h
204  *
205  *    Revision 1.4  2001/07/24 12:47:06  oes
206  *    Applied BeOS support update by Eugenia
207  *
208  *    Revision 1.3  2001/06/09 10:55:28  jongfoster
209  *    Changing BUFSIZ ==> BUFFER_SIZE
210  *
211  *    Revision 1.2  2001/06/07 23:11:38  jongfoster
212  *    Removing gateways[] list - no longer used.
213  *    Replacing function pointer in struct gateway with a directly
214  *    called function forwarded_connect(), which can do the common
215  *    task of deciding whether to connect to the web server or HTTP
216  *    proxy.
217  *    Replacing struct gateway with struct forward_spec
218  *    Fixing bug with SOCKS4A and HTTP proxy server in combination.
219  *    It was a bug which led to the connection being made to the web
220  *    server rather than the HTTP proxy, and also a buffer overrun.
221  *
222  *    Revision 1.1.1.1  2001/05/15 13:58:54  oes
223  *    Initial import of version 2.9.3 source tree
224  *
225  *
226  *********************************************************************/
227 \f
228
229 #include "config.h"
230
231 #include <stdio.h>
232 #include <sys/types.h>
233
234 #ifndef _WIN32
235 #include <netinet/in.h>
236 #endif
237
238 #include <errno.h>
239 #include <string.h>
240 #include "assert.h"
241
242 #ifdef _WIN32
243 #include <winsock2.h>
244 #endif /* def _WIN32 */
245
246 #ifdef __BEOS__
247 #include <netdb.h>
248 #endif /* def __BEOS__ */
249
250 #ifdef __OS2__
251 #include <utils.h>
252 #endif /* def __OS2__ */
253
254 #include "project.h"
255 #include "jcc.h"
256 #include "errlog.h"
257 #include "jbsockets.h"
258 #include "gateway.h"
259 #include "miscutil.h"
260 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
261 #ifdef HAVE_POLL
262 #ifdef __GLIBC__ 
263 #include <sys/poll.h>
264 #else
265 #include <poll.h>
266 #endif /* def __GLIBC__ */
267 #endif /* HAVE_POLL */
268 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
269
270 const char gateway_h_rcs[] = GATEWAY_H_VERSION;
271
272 static jb_socket socks4_connect(const struct forward_spec * fwd,
273                                 const char * target_host,
274                                 int target_port,
275                                 struct client_state *csp);
276
277 static jb_socket socks5_connect(const struct forward_spec *fwd,
278                                 const char *target_host,
279                                 int target_port,
280                                 struct client_state *csp);
281
282
283 #define SOCKS_REQUEST_GRANTED          90
284 #define SOCKS_REQUEST_REJECT           91
285 #define SOCKS_REQUEST_IDENT_FAILED     92
286 #define SOCKS_REQUEST_IDENT_CONFLICT   93
287
288 #define SOCKS5_REQUEST_GRANTED             0
289 #define SOCKS5_REQUEST_FAILED              1
290 #define SOCKS5_REQUEST_DENIED              2
291 #define SOCKS5_REQUEST_NETWORK_UNREACHABLE 3
292 #define SOCKS5_REQUEST_HOST_UNREACHABLE    4
293 #define SOCKS5_REQUEST_CONNECTION_REFUSED  5
294 #define SOCKS5_REQUEST_TTL_EXPIRED         6
295 #define SOCKS5_REQUEST_PROTOCOL_ERROR      7
296 #define SOCKS5_REQUEST_BAD_ADDRESS_TYPE    8
297
298 /* structure of a socks client operation */
299 struct socks_op {
300    unsigned char vn;          /* socks version number */
301    unsigned char cd;          /* command code */
302    unsigned char dstport[2];  /* destination port */
303    unsigned char dstip[4];    /* destination address */
304    char userid;               /* first byte of userid */
305    char padding[3];           /* make sure sizeof(struct socks_op) is endian-independent. */
306    /* more bytes of the userid follow, terminated by a NULL */
307 };
308
309 /* structure of a socks server reply */
310 struct socks_reply {
311    unsigned char vn;          /* socks version number */
312    unsigned char cd;          /* command code */
313    unsigned char dstport[2];  /* destination port */
314    unsigned char dstip[4];    /* destination address */
315 };
316
317 static const char socks_userid[] = "anonymous";
318
319 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
320
321 #define MAX_REUSABLE_CONNECTIONS 100
322 static int keep_alive_timeout = DEFAULT_KEEP_ALIVE_TIMEOUT;
323
324 struct reusable_connection
325 {
326    jb_socket sfd;
327    int       in_use;
328    char      *host;
329    int       port;
330    time_t    timestamp;
331
332    int       forwarder_type;
333    char      *gateway_host;
334    int       gateway_port;
335    char      *forward_host;
336    int       forward_port;
337 };
338
339 static struct reusable_connection reusable_connection[MAX_REUSABLE_CONNECTIONS];
340
341 static int mark_connection_unused(jb_socket sfd);
342 static void mark_connection_closed(struct reusable_connection *closed_connection);
343 static int socket_is_still_usable(jb_socket sfd);
344
345
346 /*********************************************************************
347  *
348  * Function    :  initialize_reusable_connections
349  *
350  * Description :  Initializes the reusable_connection structures.
351  *                Must be called with connection_reuse_mutex locked.
352  *
353  * Parameters  : N/A
354  *
355  * Returns     : void
356  *
357  *********************************************************************/
358 extern void initialize_reusable_connections(void)
359 {
360    unsigned int slot = 0;
361
362    log_error(LOG_LEVEL_INFO,
363       "Support for 'Connection: keep-alive' is experimental."
364 #ifndef HAVE_POLL
365       " Detecting already dead connections might not work"
366       " correctly on your platform."
367 #endif /* ndef HAVE_POLL */
368    );
369
370    for (slot = 0; slot < SZ(reusable_connection); slot++)
371    {
372       mark_connection_closed(&reusable_connection[slot]);
373    }
374
375    log_error(LOG_LEVEL_CONNECT, "Initialized %d socket slots.", slot);
376 }
377
378
379 /*********************************************************************
380  *
381  * Function    :  remember_connection
382  *
383  * Description :  Remembers a connection for reuse later on.
384  *
385  * Parameters  :
386  *          1  :  sfd  = Open socket to remember.
387  *          2  :  http = The destination for the connection.
388  *          3  :  fwd  = The forwarder settings used.
389  *
390  * Returns     : void
391  *
392  *********************************************************************/
393 void remember_connection(jb_socket sfd, const struct http_request *http,
394                                         const struct forward_spec *fwd)
395 {
396    unsigned int slot = 0;
397    int free_slot_found = FALSE;
398
399    assert(sfd != JB_INVALID_SOCKET);
400
401    if (mark_connection_unused(sfd))
402    {
403       return;
404    }
405
406    privoxy_mutex_lock(&connection_reuse_mutex);
407
408    /* Find free socket slot. */
409    for (slot = 0; slot < SZ(reusable_connection); slot++)
410    {
411       if (reusable_connection[slot].sfd == JB_INVALID_SOCKET)
412       {
413          assert(reusable_connection[slot].in_use == 0);
414          log_error(LOG_LEVEL_CONNECT,
415             "Remembering socket %d for %s:%d in slot %d.",
416             sfd, http->host, http->port, slot);
417          free_slot_found = TRUE;
418          break;
419       }
420    }
421
422    if (!free_slot_found)
423    {
424       log_error(LOG_LEVEL_CONNECT,
425         "No free slots found to remembering socket for %s:%d. Last slot %d.",
426         http->host, http->port, slot);
427       privoxy_mutex_unlock(&connection_reuse_mutex);
428       close_socket(sfd);
429       return;
430    }
431
432    assert(NULL != http->host);
433    reusable_connection[slot].host = strdup(http->host);
434    if (NULL == reusable_connection[slot].host)
435    {
436       log_error(LOG_LEVEL_FATAL, "Out of memory saving socket.");
437    }
438    reusable_connection[slot].sfd = sfd;
439    reusable_connection[slot].port = http->port;
440    reusable_connection[slot].in_use = 0;
441    reusable_connection[slot].timestamp = time(NULL);
442
443    assert(NULL != fwd);
444    assert(reusable_connection[slot].gateway_host == NULL);
445    assert(reusable_connection[slot].gateway_port == 0);
446    assert(reusable_connection[slot].forwarder_type == SOCKS_NONE);
447    assert(reusable_connection[slot].forward_host == NULL);
448    assert(reusable_connection[slot].forward_port == 0);
449
450    reusable_connection[slot].forwarder_type = fwd->type;
451    if (NULL != fwd->gateway_host)
452    {
453       reusable_connection[slot].gateway_host = strdup(fwd->gateway_host);
454       if (NULL == reusable_connection[slot].gateway_host)
455       {
456          log_error(LOG_LEVEL_FATAL, "Out of memory saving gateway_host.");
457       }
458    }
459    else
460    {
461       reusable_connection[slot].gateway_host = NULL;
462    }
463    reusable_connection[slot].gateway_port = fwd->gateway_port;
464
465    if (NULL != fwd->forward_host)
466    {
467       reusable_connection[slot].forward_host = strdup(fwd->forward_host);
468       if (NULL == reusable_connection[slot].forward_host)
469       {
470          log_error(LOG_LEVEL_FATAL, "Out of memory saving forward_host.");
471       }
472    }
473    else
474    {
475       reusable_connection[slot].forward_host = NULL;
476    }
477    reusable_connection[slot].forward_port = fwd->forward_port;
478
479    privoxy_mutex_unlock(&connection_reuse_mutex);
480 }
481
482
483 /*********************************************************************
484  *
485  * Function    :  mark_connection_closed
486  *
487  * Description : Marks a reused connection closed.
488  *               Must be called with connection_reuse_mutex locked.
489  *
490  * Parameters  :
491  *          1  :  closed_connection = The connection to mark as closed.
492  *
493  * Returns     : void
494  *
495  *********************************************************************/
496 static void mark_connection_closed(struct reusable_connection *closed_connection)
497 {
498    closed_connection->in_use = FALSE;
499    closed_connection->sfd = JB_INVALID_SOCKET;
500    freez(closed_connection->host);
501    closed_connection->port = 0;
502    closed_connection->timestamp = 0;
503    closed_connection->forwarder_type = SOCKS_NONE;
504    freez(closed_connection->gateway_host);
505    closed_connection->gateway_port = 0;
506    freez(closed_connection->forward_host);
507    closed_connection->forward_port = 0;
508 }
509
510
511 /*********************************************************************
512  *
513  * Function    :  forget_connection
514  *
515  * Description :  Removes a previously remembered connection from
516  *                the list of reusable connections.
517  *
518  * Parameters  :
519  *          1  :  sfd = The socket belonging to the connection in question.
520  *
521  * Returns     : void
522  *
523  *********************************************************************/
524 void forget_connection(jb_socket sfd)
525 {
526    unsigned int slot = 0;
527
528    assert(sfd != JB_INVALID_SOCKET);
529
530    privoxy_mutex_lock(&connection_reuse_mutex);
531
532    for (slot = 0; slot < SZ(reusable_connection); slot++)
533    {
534       if (reusable_connection[slot].sfd == sfd)
535       {
536          assert(reusable_connection[slot].in_use);
537
538          log_error(LOG_LEVEL_CONNECT,
539             "Forgetting socket %d for %s:%d in slot %d.",
540             sfd, reusable_connection[slot].host,
541             reusable_connection[slot].port, slot);
542          mark_connection_closed(&reusable_connection[slot]);
543          privoxy_mutex_unlock(&connection_reuse_mutex);
544
545          return;
546       }
547    }
548
549    log_error(LOG_LEVEL_CONNECT,
550       "Socket %d already forgotten or never remembered.", sfd);
551
552    privoxy_mutex_unlock(&connection_reuse_mutex);
553 }
554
555
556 /*********************************************************************
557  *
558  * Function    :  connection_destination_matches
559  *
560  * Description :  Determines whether a remembered connection can
561  *                be reused. That is whether the destination and
562  *                the forwarding settings match.
563  *
564  * Parameters  :
565  *          1  :  connection = The connection to check.
566  *          2  :  http = The destination for the connection.
567  *          3  :  fwd  = The forwarder settings.
568  *
569  * Returns     :  TRUE for yes, FALSE otherwise.
570  *
571  *********************************************************************/
572 static int connection_destination_matches(const struct reusable_connection *connection,
573                                           const struct http_request *http,
574                                           const struct forward_spec *fwd)
575 {
576    /* XXX: Start of duplicated checks for debugging purposes. */
577    if (strcmpic(connection->host, http->host))
578    {
579       return FALSE;
580    }
581
582    if (connection->forwarder_type != fwd->type)
583    {
584       log_error(LOG_LEVEL_CONNECT, "Type mismatch: %d %d (%s)",
585          connection->forwarder_type, fwd->type, http->host);
586       return FALSE;
587    }
588    if (connection->gateway_port   != fwd->gateway_port)
589    {
590       log_error(LOG_LEVEL_CONNECT, "Gateway port mismatch: %d %d (%s)",
591          connection->gateway_port, fwd->gateway_port, http->host);
592       return FALSE;
593    }
594    if (connection->forward_port   != fwd->forward_port)
595    {
596       log_error(LOG_LEVEL_CONNECT, "Forward port mismatch: %d %d (%s)",
597          connection->forward_port, fwd->forward_port, http->host);
598       return FALSE;
599    }
600    if (connection->port != http->port)
601    {
602       log_error(LOG_LEVEL_CONNECT, "Server port mismatch: %d %d (%s)",
603          connection->port, http->port, http->host);
604       return FALSE;
605    }
606
607    /* XXX: End of duplicated checks for debugging purposes. */
608
609    if ((connection->forwarder_type != fwd->type)
610     || (connection->gateway_port   != fwd->gateway_port)
611     || (connection->forward_port   != fwd->forward_port)
612     || (connection->port           != http->port))
613    {
614       return FALSE;
615    }
616
617    if ((    (NULL != connection->gateway_host)
618          && (NULL != fwd->gateway_host)
619          && strcmpic(connection->gateway_host, fwd->gateway_host))
620        && (connection->gateway_host != fwd->gateway_host))
621    {
622       log_error(LOG_LEVEL_CONNECT, "Gateway mismatch.");
623       return FALSE;
624    }
625
626    if ((    (NULL != connection->forward_host)
627          && (NULL != fwd->forward_host)
628          && strcmpic(connection->forward_host, fwd->forward_host))
629       && (connection->forward_host != fwd->forward_host))
630    {
631       log_error(LOG_LEVEL_CONNECT, "Forwarding proxy mismatch.");
632       return FALSE;
633    }
634
635    return (!strcmpic(connection->host, http->host));
636
637 }
638
639
640 /*********************************************************************
641  *
642  * Function    :  close_unusable_connections
643  *
644  * Description :  Closes remembered connections that have timed
645  *                out or have been closed on the other side.
646  *
647  * Parameters  :  none
648  *
649  * Returns     :  void
650  *
651  *********************************************************************/
652 static void close_unusable_connections(void)
653 {
654    unsigned int slot = 0;
655
656    for (slot = 0; slot < SZ(reusable_connection); slot++)
657    {
658       if (!reusable_connection[slot].in_use
659          && (JB_INVALID_SOCKET != reusable_connection[slot].sfd))
660       {
661          time_t time_open = time(NULL) - reusable_connection[slot].timestamp;
662
663          if (keep_alive_timeout < time_open)
664          {
665             log_error(LOG_LEVEL_CONNECT,
666                "The connection to %s:%d in slot %d timed out. "
667                "Closing socket %d. Timeout is: %d.",
668                reusable_connection[slot].host,
669                reusable_connection[slot].port, slot,
670                reusable_connection[slot].sfd, keep_alive_timeout);
671             close_socket(reusable_connection[slot].sfd);
672             mark_connection_closed(&reusable_connection[slot]);
673             continue;
674          }
675
676          if (!socket_is_still_usable(reusable_connection[slot].sfd))
677          {
678             log_error(LOG_LEVEL_CONNECT,
679                "The connection to %s:%d in slot %d is no longer usable. "
680                "Closing socket %d.", reusable_connection[slot].host,
681                reusable_connection[slot].port, slot,
682                reusable_connection[slot].sfd);
683             close_socket(reusable_connection[slot].sfd);
684             mark_connection_closed(&reusable_connection[slot]);
685             continue;
686          }
687       }
688    }
689 }
690
691
692 /*********************************************************************
693  *
694  * Function    :  socket_is_still_usable
695  *
696  * Description :  Decides whether or not an open socket is still usable.
697  *
698  * Parameters  :
699  *          1  :  sfd = The socket to check.
700  *
701  * Returns     :  TRUE for yes, otherwise FALSE.
702  *
703  *********************************************************************/
704 static int socket_is_still_usable(jb_socket sfd)
705 {
706 #ifdef HAVE_POLL
707    int poll_result;
708    struct pollfd poll_fd[1];
709
710    memset(poll_fd, 0, sizeof(poll_fd));
711    poll_fd[0].fd = sfd;
712    poll_fd[0].events = POLLIN;
713
714    poll_result = poll(poll_fd, 1, 0);
715
716    if (-1 != poll_result)
717    {
718       return !(poll_fd[0].revents & POLLIN);
719    }
720    else
721    {
722       log_error(LOG_LEVEL_CONNECT, "Polling socket %d failed.", sfd);
723       return FALSE;
724    }
725 #else
726    fd_set readable_fds;
727    struct timeval timeout;
728    int ret;
729    int socket_is_alive = 0;
730
731    memset(&timeout, '\0', sizeof(timeout));
732    FD_ZERO(&readable_fds);
733    FD_SET(sfd, &readable_fds);
734
735    ret = select((int)sfd+1, &readable_fds, NULL, NULL, &timeout);
736    if (ret < 0)
737    {
738       log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
739    }
740
741    /*
742     * XXX: I'm not sure why !FD_ISSET() works,
743     * but apparently it does.
744     */
745    socket_is_alive = !FD_ISSET(sfd, &readable_fds);
746
747    return socket_is_alive;
748 #endif /* def HAVE_POLL */
749 }
750
751
752 /*********************************************************************
753  *
754  * Function    :  get_reusable_connection
755  *
756  * Description :  Returns an open socket to a previously remembered
757  *                open connection (if there is one).
758  *
759  * Parameters  :
760  *          1  :  http = The destination for the connection.
761  *          2  :  fwd  = The forwarder settings.
762  *
763  * Returns     :  JB_INVALID_SOCKET => No reusable connection found,
764  *                otherwise a usable socket.
765  *
766  *********************************************************************/
767 static jb_socket get_reusable_connection(const struct http_request *http,
768                                          const struct forward_spec *fwd)
769 {
770    jb_socket sfd = JB_INVALID_SOCKET;
771    unsigned int slot = 0;
772
773    privoxy_mutex_lock(&connection_reuse_mutex);
774
775    close_unusable_connections();
776
777    for (slot = 0; slot < SZ(reusable_connection); slot++)
778    {
779       if (!reusable_connection[slot].in_use
780          && (JB_INVALID_SOCKET != reusable_connection[slot].sfd))
781       {
782          if (connection_destination_matches(&reusable_connection[slot], http, fwd))
783          {
784             reusable_connection[slot].in_use = TRUE;
785             sfd = reusable_connection[slot].sfd;
786             log_error(LOG_LEVEL_CONNECT,
787                "Found reusable socket %d for %s:%d in slot %d.",
788                sfd, reusable_connection[slot].host, reusable_connection[slot].port, slot);
789             break;
790          }
791       }
792    }
793
794    privoxy_mutex_unlock(&connection_reuse_mutex);
795
796    return sfd;
797
798 }
799
800
801 /*********************************************************************
802  *
803  * Function    :  mark_connection_unused
804  *
805  * Description :  Gives a remembered connection free for reuse.
806  *
807  * Parameters  :
808  *          1  :  sfd = The socket belonging to the connection in question.
809  *
810  * Returns     :  TRUE => Socket found and marked as unused.
811  *                FALSE => Socket not found.
812  *
813  *********************************************************************/
814 static int mark_connection_unused(jb_socket sfd)
815 {
816    unsigned int slot = 0;
817    int socket_found = FALSE;
818
819    assert(sfd != JB_INVALID_SOCKET);
820
821    privoxy_mutex_lock(&connection_reuse_mutex);
822
823    for (slot = 0; slot < SZ(reusable_connection); slot++)
824    {
825       if (reusable_connection[slot].sfd == sfd)
826       {
827          assert(reusable_connection[slot].in_use);
828          socket_found = TRUE;
829          log_error(LOG_LEVEL_CONNECT,
830             "Marking open socket %d for %s:%d in slot %d as unused.",
831             sfd, reusable_connection[slot].host,
832             reusable_connection[slot].port, slot);
833          reusable_connection[slot].in_use = 0;
834          reusable_connection[slot].timestamp = time(NULL);
835          break;
836       }
837    }
838
839    privoxy_mutex_unlock(&connection_reuse_mutex);
840
841    return socket_found;
842
843 }
844
845
846 /*********************************************************************
847  *
848  * Function    :  set_keep_alive_timeout
849  *
850  * Description :  Sets the timeout after which open
851  *                connections will no longer be reused.
852  *
853  * Parameters  :
854  *          1  :  timeout = The timeout in seconds.
855  *
856  * Returns     :  void
857  *
858  *********************************************************************/
859 void set_keep_alive_timeout(int timeout)
860 {
861    keep_alive_timeout = timeout;
862 }
863 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
864
865
866 /*********************************************************************
867  *
868  * Function    :  forwarded_connect
869  *
870  * Description :  Connect to a specified web server, possibly via
871  *                a HTTP proxy and/or a SOCKS proxy.
872  *
873  * Parameters  :
874  *          1  :  fwd = the proxies to use when connecting.
875  *          2  :  http = the http request and apropos headers
876  *          3  :  csp = Current client state (buffers, headers, etc...)
877  *
878  * Returns     :  JB_INVALID_SOCKET => failure, else it is the socket file descriptor.
879  *
880  *********************************************************************/
881 jb_socket forwarded_connect(const struct forward_spec * fwd,
882                             struct http_request *http,
883                             struct client_state *csp)
884 {
885    const char * dest_host;
886    int dest_port;
887    jb_socket sfd = JB_INVALID_SOCKET;
888
889 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
890    sfd = get_reusable_connection(http, fwd);
891    if (JB_INVALID_SOCKET != sfd)
892    {
893       return sfd;
894    }
895 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
896
897    /* Figure out if we need to connect to the web server or a HTTP proxy. */
898    if (fwd->forward_host)
899    {
900       /* HTTP proxy */
901       dest_host = fwd->forward_host;
902       dest_port = fwd->forward_port;
903    }
904    else
905    {
906       /* Web server */
907       dest_host = http->host;
908       dest_port = http->port;
909    }
910
911    /* Connect, maybe using a SOCKS proxy */
912    switch (fwd->type)
913    {
914       case SOCKS_NONE:
915          sfd = connect_to(dest_host, dest_port, csp);
916          break;
917       case SOCKS_4:
918       case SOCKS_4A:
919          sfd = socks4_connect(fwd, dest_host, dest_port, csp);
920          break;
921       case SOCKS_5:
922          sfd = socks5_connect(fwd, dest_host, dest_port, csp);
923          break;
924       default:
925          /* Should never get here */
926          log_error(LOG_LEVEL_FATAL,
927             "SOCKS4 impossible internal error - bad SOCKS type.");
928    }
929
930    if (JB_INVALID_SOCKET != sfd)
931    {
932       log_error(LOG_LEVEL_CONNECT,
933          "Created new connection to %s:%d on socket %d.",
934          http->host, http->port, sfd);
935    }
936
937    return sfd;
938
939 }
940
941
942 /*********************************************************************
943  *
944  * Function    :  socks4_connect
945  *
946  * Description :  Connect to the SOCKS server, and connect through
947  *                it to the specified server.   This handles
948  *                all the SOCKS negotiation, and returns a file
949  *                descriptor for a socket which can be treated as a
950  *                normal (non-SOCKS) socket.
951  *
952  *                Logged error messages are saved to csp->error_message
953  *                and later reused by error_response() for the CGI
954  *                message. strdup allocation failures are handled there.
955  *
956  * Parameters  :
957  *          1  :  fwd = Specifies the SOCKS proxy to use.
958  *          2  :  target_host = The final server to connect to.
959  *          3  :  target_port = The final port to connect to.
960  *          4  :  csp = Current client state (buffers, headers, etc...)
961  *
962  * Returns     :  JB_INVALID_SOCKET => failure, else a socket file descriptor.
963  *
964  *********************************************************************/
965 static jb_socket socks4_connect(const struct forward_spec * fwd,
966                                 const char * target_host,
967                                 int target_port,
968                                 struct client_state *csp)
969 {
970    unsigned int web_server_addr;
971    char buf[BUFFER_SIZE];
972    struct socks_op    *c = (struct socks_op    *)buf;
973    struct socks_reply *s = (struct socks_reply *)buf;
974    size_t n;
975    size_t csiz;
976    jb_socket sfd;
977    int err = 0;
978    char *errstr = NULL;
979
980    if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
981    {
982       /* XXX: Shouldn't the config file parser prevent this? */
983       errstr = "NULL gateway host specified.";
984       err = 1;
985    }
986
987    if (fwd->gateway_port <= 0)
988    {
989       errstr = "invalid gateway port specified.";
990       err = 1;
991    }
992
993    if (err)
994    {
995       log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
996       csp->error_message = strdup(errstr); 
997       errno = EINVAL;
998       return(JB_INVALID_SOCKET);
999    }
1000
1001    /* build a socks request for connection to the web server */
1002
1003    strlcpy(&(c->userid), socks_userid, sizeof(buf) - sizeof(struct socks_op));
1004
1005    csiz = sizeof(*c) + sizeof(socks_userid) - sizeof(c->userid) - sizeof(c->padding);
1006
1007    switch (fwd->type)
1008    {
1009       case SOCKS_4:
1010          web_server_addr = resolve_hostname_to_ip(target_host);
1011          if (web_server_addr == INADDR_NONE)
1012          {
1013             errstr = "could not resolve target host";
1014             log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s %s", errstr, target_host);
1015             err = 1;
1016          }
1017          else
1018          {
1019             web_server_addr = htonl(web_server_addr);
1020          }
1021          break;
1022       case SOCKS_4A:
1023          web_server_addr = 0x00000001;
1024          n = csiz + strlen(target_host) + 1;
1025          if (n > sizeof(buf))
1026          {
1027             errno = EINVAL;
1028             errstr = "buffer cbuf too small.";
1029             log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
1030             err = 1;
1031          }
1032          else
1033          {
1034             strlcpy(buf + csiz, target_host, sizeof(buf) - sizeof(struct socks_op) - csiz);
1035             /*
1036              * What we forward to the socks4a server should have the
1037              * size of socks_op, plus the length of the userid plus
1038              * its \0 byte (which we don't have to add because the
1039              * first byte of the userid is counted twice as it's also
1040              * part of sock_op) minus the padding bytes (which are part
1041              * of the userid as well), plus the length of the target_host
1042              * (which is stored csiz bytes after the beginning of the buffer),
1043              * plus another \0 byte.
1044              */
1045             assert(n == sizeof(struct socks_op) + strlen(&(c->userid)) - sizeof(c->padding) + strlen(buf + csiz) + 1);
1046             csiz = n;
1047          }
1048          break;
1049       default:
1050          /* Should never get here */
1051          log_error(LOG_LEVEL_FATAL,
1052             "socks4_connect: SOCKS4 impossible internal error - bad SOCKS type.");
1053          /* Not reached */
1054          return(JB_INVALID_SOCKET);
1055    }
1056
1057    if (err)
1058    {
1059       csp->error_message = strdup(errstr);
1060       return(JB_INVALID_SOCKET);
1061    }
1062
1063    c->vn          = 4;
1064    c->cd          = 1;
1065    c->dstport[0]  = (unsigned char)((target_port       >> 8  ) & 0xff);
1066    c->dstport[1]  = (unsigned char)((target_port             ) & 0xff);
1067    c->dstip[0]    = (unsigned char)((web_server_addr   >> 24 ) & 0xff);
1068    c->dstip[1]    = (unsigned char)((web_server_addr   >> 16 ) & 0xff);
1069    c->dstip[2]    = (unsigned char)((web_server_addr   >>  8 ) & 0xff);
1070    c->dstip[3]    = (unsigned char)((web_server_addr         ) & 0xff);
1071
1072    /* pass the request to the socks server */
1073    sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
1074
1075    if (sfd == JB_INVALID_SOCKET)
1076    {
1077       /*
1078        * XXX: connect_to should fill in the exact reason.
1079        * Most likely resolving the IP of the forwarder failed.
1080        */
1081       errstr = "connect_to failed: see logfile for details";
1082       err = 1;
1083    }
1084    else if (write_socket(sfd, (char *)c, csiz))
1085    {
1086       errstr = "SOCKS4 negotiation write failed.";
1087       log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
1088       err = 1;
1089       close_socket(sfd);
1090    }
1091    else if (read_socket(sfd, buf, sizeof(buf)) != sizeof(*s))
1092    {
1093       errstr = "SOCKS4 negotiation read failed.";
1094       log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
1095       err = 1;
1096       close_socket(sfd);
1097    }
1098
1099    if (err)
1100    {
1101       csp->error_message = strdup(errstr);      
1102       return(JB_INVALID_SOCKET);
1103    }
1104
1105    switch (s->cd)
1106    {
1107       case SOCKS_REQUEST_GRANTED:
1108          return(sfd);
1109          break;
1110       case SOCKS_REQUEST_REJECT:
1111          errstr = "SOCKS request rejected or failed.";
1112          errno = EINVAL;
1113          break;
1114       case SOCKS_REQUEST_IDENT_FAILED:
1115          errstr = "SOCKS request rejected because "
1116             "SOCKS server cannot connect to identd on the client.";
1117          errno = EACCES;
1118          break;
1119       case SOCKS_REQUEST_IDENT_CONFLICT:
1120          errstr = "SOCKS request rejected because "
1121             "the client program and identd report "
1122             "different user-ids.";
1123          errno = EACCES;
1124          break;
1125       default:
1126          errno = ENOENT;
1127          snprintf(buf, sizeof(buf),
1128             "SOCKS request rejected for reason code %d.", s->cd);
1129          errstr = buf;
1130    }
1131
1132    log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
1133    csp->error_message = strdup(errstr);
1134    close_socket(sfd);
1135
1136    return(JB_INVALID_SOCKET);
1137
1138 }
1139
1140 /*********************************************************************
1141  *
1142  * Function    :  translate_socks5_error
1143  *
1144  * Description :  Translates a SOCKS errors to a string.
1145  *
1146  * Parameters  :
1147  *          1  :  socks_error = The error code to translate.
1148  *
1149  * Returns     :  The string translation.
1150  *
1151  *********************************************************************/
1152 static const char *translate_socks5_error(int socks_error)
1153 {
1154    switch (socks_error)
1155    {
1156       /* XXX: these should be more descriptive */
1157       case SOCKS5_REQUEST_FAILED:
1158          return "SOCKS5 request failed";
1159       case SOCKS5_REQUEST_DENIED:
1160          return "SOCKS5 request denied";
1161       case SOCKS5_REQUEST_NETWORK_UNREACHABLE:
1162          return "SOCKS5 network unreachable";
1163       case SOCKS5_REQUEST_HOST_UNREACHABLE:
1164          return "SOCKS5 host unreachable";
1165       case SOCKS5_REQUEST_CONNECTION_REFUSED:
1166          return "SOCKS5 connection refused";
1167       case SOCKS5_REQUEST_TTL_EXPIRED:
1168          return "SOCKS5 TTL expired";
1169       case SOCKS5_REQUEST_PROTOCOL_ERROR:
1170          return "SOCKS5 client protocol error";
1171       case SOCKS5_REQUEST_BAD_ADDRESS_TYPE:
1172          return "SOCKS5 domain names unsupported";
1173       case SOCKS5_REQUEST_GRANTED:
1174          return "everything's peachy";
1175       default:
1176          return "SOCKS5 negotiation protocol error";
1177    }
1178 }
1179
1180 /*********************************************************************
1181  *
1182  * Function    :  socks5_connect
1183  *
1184  * Description :  Connect to the SOCKS server, and connect through
1185  *                it to the specified server.   This handles
1186  *                all the SOCKS negotiation, and returns a file
1187  *                descriptor for a socket which can be treated as a
1188  *                normal (non-SOCKS) socket.
1189  *
1190  * Parameters  :
1191  *          1  :  fwd = Specifies the SOCKS proxy to use.
1192  *          2  :  target_host = The final server to connect to.
1193  *          3  :  target_port = The final port to connect to.
1194  *          4  :  csp = Current client state (buffers, headers, etc...)
1195  *
1196  * Returns     :  JB_INVALID_SOCKET => failure, else a socket file descriptor.
1197  *
1198  *********************************************************************/
1199 static jb_socket socks5_connect(const struct forward_spec *fwd,
1200                                 const char *target_host,
1201                                 int target_port,
1202                                 struct client_state *csp)
1203 {
1204    int err = 0;
1205    char cbuf[300];
1206    char sbuf[30];
1207    size_t client_pos = 0;
1208    int server_size = 0;
1209    size_t hostlen = 0;
1210    jb_socket sfd;
1211    const char *errstr = NULL;
1212
1213    assert(fwd->gateway_host);
1214    if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
1215    {
1216       errstr = "NULL gateway host specified";
1217       err = 1;
1218    }
1219
1220    if (fwd->gateway_port <= 0)
1221    {
1222       /*
1223        * XXX: currently this can't happen because in
1224        * case of invalid gateway ports we use the defaults.
1225        * Of course we really shouldn't do that.
1226        */
1227       errstr = "invalid gateway port specified";
1228       err = 1;
1229    }
1230
1231    hostlen = strlen(target_host);
1232    if (hostlen > 255)
1233    {
1234       errstr = "target host name is longer than 255 characters";
1235       err = 1;
1236    }
1237
1238    if (fwd->type != SOCKS_5)
1239    {
1240       /* Should never get here */
1241       log_error(LOG_LEVEL_FATAL,
1242          "SOCKS5 impossible internal error - bad SOCKS type");
1243       err = 1;
1244    }
1245
1246    if (err)
1247    {
1248       errno = EINVAL;
1249       assert(errstr != NULL);
1250       log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1251       csp->error_message = strdup(errstr);
1252       return(JB_INVALID_SOCKET);
1253    }
1254
1255    /* pass the request to the socks server */
1256    sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
1257
1258    if (sfd == JB_INVALID_SOCKET)
1259    {
1260       errstr = "socks5 server unreachable";
1261       log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1262       csp->error_message = strdup(errstr);
1263       return(JB_INVALID_SOCKET);
1264    }
1265
1266    client_pos = 0;
1267    cbuf[client_pos++] = '\x05'; /* Version */
1268    cbuf[client_pos++] = '\x01'; /* One authentication method supported */
1269    cbuf[client_pos++] = '\x00'; /* The no authentication authentication method */
1270
1271    if (write_socket(sfd, cbuf, client_pos))
1272    {
1273       errstr = "SOCKS5 negotiation write failed";
1274       csp->error_message = strdup(errstr);
1275       log_error(LOG_LEVEL_CONNECT, "%s", errstr);
1276       close_socket(sfd);
1277       return(JB_INVALID_SOCKET);
1278    }
1279
1280    if (read_socket(sfd, sbuf, sizeof(sbuf)) != 2)
1281    {
1282       errstr = "SOCKS5 negotiation read failed";
1283       err = 1;
1284    }
1285
1286    if (!err && (sbuf[0] != '\x05'))
1287    {
1288       errstr = "SOCKS5 negotiation protocol version error";
1289       err = 1;
1290    }
1291
1292    if (!err && (sbuf[1] == '\xff'))
1293    {
1294       errstr = "SOCKS5 authentication required";
1295       err = 1;
1296    }
1297
1298    if (!err && (sbuf[1] != '\x00'))
1299    {
1300       errstr = "SOCKS5 negotiation protocol error";
1301       err = 1;
1302    }
1303
1304    if (err)
1305    {
1306       assert(errstr != NULL);
1307       log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1308       csp->error_message = strdup(errstr);
1309       close_socket(sfd);
1310       errno = EINVAL;
1311       return(JB_INVALID_SOCKET);
1312    }
1313
1314    client_pos = 0;
1315    cbuf[client_pos++] = '\x05'; /* Version */
1316    cbuf[client_pos++] = '\x01'; /* TCP connect */
1317    cbuf[client_pos++] = '\x00'; /* Reserved, must be 0x00 */
1318    cbuf[client_pos++] = '\x03'; /* Address is domain name */
1319    cbuf[client_pos++] = (char)(hostlen & 0xffu);
1320    assert(sizeof(cbuf) - client_pos > 255);
1321    /* Using strncpy because we really want the nul byte padding. */
1322    strncpy(cbuf + client_pos, target_host, sizeof(cbuf) - client_pos);
1323    client_pos += (hostlen & 0xffu);
1324    cbuf[client_pos++] = (char)((target_port >> 8) & 0xff);
1325    cbuf[client_pos++] = (char)((target_port     ) & 0xff);
1326
1327    if (write_socket(sfd, cbuf, client_pos))
1328    {
1329       errstr = "SOCKS5 negotiation read failed";
1330       csp->error_message = strdup(errstr);
1331       log_error(LOG_LEVEL_CONNECT, "%s", errstr);
1332       close_socket(sfd);
1333       errno = EINVAL;
1334       return(JB_INVALID_SOCKET);
1335    }
1336
1337    server_size = read_socket(sfd, sbuf, sizeof(sbuf));
1338    if (server_size < 3)
1339    {
1340       errstr = "SOCKS5 negotiation read failed";
1341       err = 1;
1342    }
1343    else if (server_size > 20)
1344    {
1345       /* This is somewhat unexpected but doesn't realy matter. */
1346       log_error(LOG_LEVEL_CONNECT, "socks5_connect: read %d bytes "
1347          "from socks server. Would have accepted up to %d.",
1348          server_size, sizeof(sbuf));
1349    }
1350
1351    if (!err && (sbuf[0] != '\x05'))
1352    {
1353       errstr = "SOCKS5 negotiation protocol version error";
1354       err = 1;
1355    }
1356
1357    if (!err && (sbuf[2] != '\x00'))
1358    {
1359       errstr = "SOCKS5 negotiation protocol error";
1360       err = 1;
1361    }
1362
1363    if (!err)
1364    {
1365       if (sbuf[1] == SOCKS5_REQUEST_GRANTED)
1366       {
1367          return(sfd);
1368       }
1369       errstr = translate_socks5_error(sbuf[1]);
1370       err = 1;
1371    }
1372
1373    assert(errstr != NULL);
1374    csp->error_message = strdup(errstr);
1375    log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1376    close_socket(sfd);
1377    errno = EINVAL;
1378
1379    return(JB_INVALID_SOCKET);
1380
1381 }
1382
1383 /*
1384   Local Variables:
1385   tab-width: 3
1386   end:
1387 */