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