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