2eec9956e9f5f5b3345e867acf78220a471f5901
[privoxy.git] / gateway.c
1 const char gateway_rcs[] = "$Id: gateway.c,v 1.21 2007/07/28 12:30:03 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-2007 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.21  2007/07/28 12:30:03  fabiankeil
38  *    Modified patch from Song Weijia (#1762559) to
39  *    fix socks requests on big-endian platforms.
40  *
41  *    Revision 1.20  2007/05/14 10:23:48  fabiankeil
42  *    - Use strlcpy() instead of strcpy().
43  *    - Use the same buffer for socks requests and socks responses.
44  *    - Fix bogus warning about web_server_addr being used uninitialized.
45  *
46  *    Revision 1.19  2007/01/25 14:09:45  fabiankeil
47  *    - Save errors in socks4_connect() to csp->error_message.
48  *    - Silence some gcc43 warnings, hopefully the right way.
49  *
50  *    Revision 1.18  2006/07/18 14:48:46  david__schmidt
51  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
52  *    with what was really the latest development (the v_3_0_branch branch)
53  *
54  *    Revision 1.16  2002/05/12 21:36:29  jongfoster
55  *    Correcting function comments
56  *
57  *    Revision 1.15  2002/03/26 22:29:54  swa
58  *    we have a new homepage!
59  *
60  *    Revision 1.14  2002/03/24 13:25:43  swa
61  *    name change related issues
62  *
63  *    Revision 1.13  2002/03/13 00:29:59  jongfoster
64  *    Killing warnings
65  *
66  *    Revision 1.12  2002/03/09 20:03:52  jongfoster
67  *    - Making various functions return int rather than size_t.
68  *      (Undoing a recent change).  Since size_t is unsigned on
69  *      Windows, functions like read_socket that return -1 on
70  *      error cannot return a size_t.
71  *
72  *      THIS WAS A MAJOR BUG - it caused frequent, unpredictable
73  *      crashes, and also frequently caused JB to jump to 100%
74  *      CPU and stay there.  (Because it thought it had just
75  *      read ((unsigned)-1) == 4Gb of data...)
76  *
77  *    - The signature of write_socket has changed, it now simply
78  *      returns success=0/failure=nonzero.
79  *
80  *    - Trying to get rid of a few warnings --with-debug on
81  *      Windows, I've introduced a new type "jb_socket".  This is
82  *      used for the socket file descriptors.  On Windows, this
83  *      is SOCKET (a typedef for unsigned).  Everywhere else, it's
84  *      an int.  The error value can't be -1 any more, so it's
85  *      now JB_INVALID_SOCKET (which is -1 on UNIX, and in
86  *      Windows it maps to the #define INVALID_SOCKET.)
87  *
88  *    - The signature of bind_port has changed.
89  *
90  *    Revision 1.11  2002/03/08 17:46:04  jongfoster
91  *    Fixing int/size_t warnings
92  *
93  *    Revision 1.10  2002/03/07 03:50:19  oes
94  *     - Improved handling of failed DNS lookups
95  *     - Fixed compiler warnings
96  *
97  *    Revision 1.9  2001/10/25 03:40:48  david__schmidt
98  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
99  *    threads to call select() simultaneously.  So, it's time to do a real, live,
100  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
101  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
102  *
103  *    Revision 1.8  2001/09/13 20:10:12  jongfoster
104  *    Fixing missing #include under Windows
105  *
106  *    Revision 1.7  2001/09/12 17:58:26  steudten
107  *
108  *    add #include <string.h>
109  *
110  *    Revision 1.6  2001/09/10 10:41:16  oes
111  *    Added #include in.h
112  *
113  *    Revision 1.5  2001/07/29 18:47:57  jongfoster
114  *    Adding missing #include project.h
115  *
116  *    Revision 1.4  2001/07/24 12:47:06  oes
117  *    Applied BeOS support update by Eugenia
118  *
119  *    Revision 1.3  2001/06/09 10:55:28  jongfoster
120  *    Changing BUFSIZ ==> BUFFER_SIZE
121  *
122  *    Revision 1.2  2001/06/07 23:11:38  jongfoster
123  *    Removing gateways[] list - no longer used.
124  *    Replacing function pointer in struct gateway with a directly
125  *    called function forwarded_connect(), which can do the common
126  *    task of deciding whether to connect to the web server or HTTP
127  *    proxy.
128  *    Replacing struct gateway with struct forward_spec
129  *    Fixing bug with SOCKS4A and HTTP proxy server in combination.
130  *    It was a bug which led to the connection being made to the web
131  *    server rather than the HTTP proxy, and also a buffer overrun.
132  *
133  *    Revision 1.1.1.1  2001/05/15 13:58:54  oes
134  *    Initial import of version 2.9.3 source tree
135  *
136  *
137  *********************************************************************/
138 \f
139
140 #include "config.h"
141
142 #include <stdio.h>
143 #include <sys/types.h>
144
145 #ifndef _WIN32
146 #include <netinet/in.h>
147 #endif
148
149 #include <errno.h>
150 #include <string.h>
151 #include "assert.h"
152
153 #ifdef _WIN32
154 #include <winsock2.h>
155 #endif /* def _WIN32 */
156
157 #ifdef __BEOS__
158 #include <netdb.h>
159 #endif /* def __BEOS__ */
160
161 #ifdef __OS2__
162 #include <utils.h>
163 #endif /* def __OS2__ */
164
165 #include "project.h"
166 #include "jcc.h"
167 #include "errlog.h"
168 #include "jbsockets.h"
169 #include "gateway.h"
170 #include "miscutil.h"
171
172 const char gateway_h_rcs[] = GATEWAY_H_VERSION;
173
174 static jb_socket socks4_connect(const struct forward_spec * fwd,
175                                 const char * target_host,
176                                 int target_port,
177                                 struct client_state *csp);
178
179 static jb_socket socks5_connect(const struct forward_spec *fwd,
180                                 const char *target_host,
181                                 int target_port,
182                                 struct client_state *csp);
183
184
185 #define SOCKS_REQUEST_GRANTED          90
186 #define SOCKS_REQUEST_REJECT           91
187 #define SOCKS_REQUEST_IDENT_FAILED     92
188 #define SOCKS_REQUEST_IDENT_CONFLICT   93
189
190 #define SOCKS5_REQUEST_GRANTED             0
191 #define SOCKS5_REQUEST_FAILED              1
192 #define SOCKS5_REQUEST_DENIED              2
193 #define SOCKS5_REQUEST_NETWORK_UNREACHABLE 3
194 #define SOCKS5_REQUEST_HOST_UNREACHABLE    4
195 #define SOCKS5_REQUEST_CONNECTION_REFUSEDD 5
196 #define SOCKS5_REQUEST_TTL_EXPIRED         6
197 #define SOCKS5_REQUEST_PROTOCOL_ERROR      7
198 #define SOCKS5_REQUEST_BAD_ADDRESS_TYPE    8
199
200 /* structure of a socks client operation */
201 struct socks_op {
202    unsigned char vn;          /* socks version number */
203    unsigned char cd;          /* command code */
204    unsigned char dstport[2];  /* destination port */
205    unsigned char dstip[4];    /* destination address */
206    char userid;               /* first byte of userid */
207    char padding[3];           /* make sure sizeof(struct socks_op) is endian-independent. */
208    /* more bytes of the userid follow, terminated by a NULL */
209 };
210
211 /* structure of a socks server reply */
212 struct socks_reply {
213    unsigned char vn;          /* socks version number */
214    unsigned char cd;          /* command code */
215    unsigned char dstport[2];  /* destination port */
216    unsigned char dstip[4];    /* destination address */
217 };
218
219 static const char socks_userid[] = "anonymous";
220
221
222 /*********************************************************************
223  *
224  * Function    :  forwarded_connect
225  *
226  * Description :  Connect to a specified web server, possibly via
227  *                a HTTP proxy and/or a SOCKS proxy.
228  *
229  * Parameters  :
230  *          1  :  fwd = the proxies to use when connecting.
231  *          2  :  http = the http request and apropos headers
232  *          3  :  csp = Current client state (buffers, headers, etc...)
233  *
234  * Returns     :  JB_INVALID_SOCKET => failure, else it is the socket file descriptor.
235  *
236  *********************************************************************/
237 jb_socket forwarded_connect(const struct forward_spec * fwd,
238                             struct http_request *http,
239                             struct client_state *csp)
240 {
241    const char * dest_host;
242    int dest_port;
243
244    /* Figure out if we need to connect to the web server or a HTTP proxy. */
245    if (fwd->forward_host)
246    {
247       /* HTTP proxy */
248       dest_host = fwd->forward_host;
249       dest_port = fwd->forward_port;
250    }
251    else
252    {
253       /* Web server */
254       dest_host = http->host;
255       dest_port = http->port;
256    }
257
258    /* Connect, maybe using a SOCKS proxy */
259    switch (fwd->type)
260    {
261       case SOCKS_NONE:
262          return (connect_to(dest_host, dest_port, csp));
263
264       case SOCKS_4:
265       case SOCKS_4A:
266          return (socks4_connect(fwd, dest_host, dest_port, csp));
267
268       case SOCKS_5:
269          return (socks5_connect(fwd, dest_host, dest_port, csp));
270
271       default:
272          /* Should never get here */
273          log_error(LOG_LEVEL_FATAL, "SOCKS4 impossible internal error - bad SOCKS type.");
274          errno = EINVAL;
275          return(JB_INVALID_SOCKET);
276    }
277 }
278
279
280 /*********************************************************************
281  *
282  * Function    :  socks4_connect
283  *
284  * Description :  Connect to the SOCKS server, and connect through
285  *                it to the specified server.   This handles
286  *                all the SOCKS negotiation, and returns a file
287  *                descriptor for a socket which can be treated as a
288  *                normal (non-SOCKS) socket.
289  *
290  *                Logged error messages are saved to csp->error_message
291  *                and later reused by error_response() for the CGI
292  *                message. strdup allocation failures are handled there.
293  *
294  * Parameters  :
295  *          1  :  fwd = Specifies the SOCKS proxy to use.
296  *          2  :  target_host = The final server to connect to.
297  *          3  :  target_port = The final port to connect to.
298  *          4  :  csp = Current client state (buffers, headers, etc...)
299  *
300  * Returns     :  JB_INVALID_SOCKET => failure, else a socket file descriptor.
301  *
302  *********************************************************************/
303 static jb_socket socks4_connect(const struct forward_spec * fwd,
304                                 const char * target_host,
305                                 int target_port,
306                                 struct client_state *csp)
307 {
308    int web_server_addr;
309    char buf[BUFFER_SIZE];
310    struct socks_op    *c = (struct socks_op    *)buf;
311    struct socks_reply *s = (struct socks_reply *)buf;
312    size_t n;
313    size_t csiz;
314    jb_socket sfd;
315    int err = 0;
316    char *errstr = NULL;
317
318    if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
319    {
320       /* XXX: Shouldn't the config file parser prevent this? */
321       errstr = "NULL gateway host specified.";
322       err = 1;
323    }
324
325    if (fwd->gateway_port <= 0)
326    {
327       errstr = "invalid gateway port specified.";
328       err = 1;
329    }
330
331    if (err)
332    {
333       log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
334       csp->error_message = strdup(errstr); 
335       errno = EINVAL;
336       return(JB_INVALID_SOCKET);
337    }
338
339    /* build a socks request for connection to the web server */
340
341    strlcpy(&(c->userid), socks_userid, sizeof(buf) - sizeof(struct socks_op));
342
343    csiz = sizeof(*c) + sizeof(socks_userid) - sizeof(c->userid) - sizeof(c->padding);
344
345    switch (fwd->type)
346    {
347       case SOCKS_4:
348          web_server_addr = htonl(resolve_hostname_to_ip(target_host));
349          if (web_server_addr == INADDR_NONE)
350          {
351             errstr = "could not resolve target host";
352             log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s %s", errstr, target_host);
353             err = 1;
354          }
355          break;
356       case SOCKS_4A:
357          web_server_addr = 0x00000001;
358          n = csiz + strlen(target_host) + 1;
359          if (n > sizeof(buf))
360          {
361             errno = EINVAL;
362             errstr = "buffer cbuf too small.";
363             log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
364             err = 1;
365          }
366          else
367          {
368             strlcpy(buf + csiz, target_host, sizeof(buf) - sizeof(struct socks_op) - csiz);
369             /*
370              * What we forward to the socks4a server should have the
371              * size of socks_op, plus the length of the userid plus
372              * its \0 byte (which we don't have to add because the
373              * first byte of the userid is counted twice as it's also
374              * part of sock_op) minus the padding bytes (which are part
375              * of the userid as well), plus the length of the target_host
376              * (which is stored csiz bytes after the beginning of the buffer),
377              * plus another \0 byte.
378              */
379             assert(n == sizeof(struct socks_op) + strlen(&(c->userid)) - sizeof(c->padding) + strlen(buf + csiz) + 1);
380             csiz = n;
381          }
382          break;
383       default:
384          /* Should never get here */
385          log_error(LOG_LEVEL_FATAL,
386             "socks4_connect: SOCKS4 impossible internal error - bad SOCKS type.");
387          /* Not reached */
388          return(JB_INVALID_SOCKET);
389    }
390
391    if (err)
392    {
393       csp->error_message = strdup(errstr);
394       return(JB_INVALID_SOCKET);
395    }
396
397    c->vn          = 4;
398    c->cd          = 1;
399    c->dstport[0]  = (unsigned char)((target_port       >> 8  ) & 0xff);
400    c->dstport[1]  = (unsigned char)((target_port             ) & 0xff);
401    c->dstip[0]    = (unsigned char)((web_server_addr   >> 24 ) & 0xff);
402    c->dstip[1]    = (unsigned char)((web_server_addr   >> 16 ) & 0xff);
403    c->dstip[2]    = (unsigned char)((web_server_addr   >>  8 ) & 0xff);
404    c->dstip[3]    = (unsigned char)((web_server_addr         ) & 0xff);
405
406    /* pass the request to the socks server */
407    sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
408
409    if (sfd == JB_INVALID_SOCKET)
410    {
411       /*
412        * XXX: connect_to should fill in the exact reason.
413        * Most likely resolving the IP of the forwarder failed.
414        */
415       errstr = "connect_to failed: see logfile for details";
416       err = 1;
417    }
418    else if (write_socket(sfd, (char *)c, csiz))
419    {
420       errstr = "SOCKS4 negotiation write failed.";
421       log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
422       err = 1;
423       close_socket(sfd);
424    }
425    else if (read_socket(sfd, buf, sizeof(buf)) != sizeof(*s))
426    {
427       errstr = "SOCKS4 negotiation read failed.";
428       log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
429       err = 1;
430       close_socket(sfd);
431    }
432
433    if (err)
434    {
435       csp->error_message = strdup(errstr);      
436       return(JB_INVALID_SOCKET);
437    }
438
439    switch (s->cd)
440    {
441       case SOCKS_REQUEST_GRANTED:
442          return(sfd);
443          break;
444       case SOCKS_REQUEST_REJECT:
445          errstr = "SOCKS request rejected or failed.";
446          errno = EINVAL;
447          break;
448       case SOCKS_REQUEST_IDENT_FAILED:
449          errstr = "SOCKS request rejected because "
450             "SOCKS server cannot connect to identd on the client.";
451          errno = EACCES;
452          break;
453       case SOCKS_REQUEST_IDENT_CONFLICT:
454          errstr = "SOCKS request rejected because "
455             "the client program and identd report "
456             "different user-ids.";
457          errno = EACCES;
458          break;
459       default:
460          errno = ENOENT;
461          snprintf(buf, sizeof(buf),
462             "SOCKS request rejected for reason code %d.", s->cd);
463          errstr = buf;
464    }
465
466    log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
467    csp->error_message = strdup(errstr);
468    close_socket(sfd);
469
470    return(JB_INVALID_SOCKET);
471
472 }
473
474
475 /*********************************************************************
476  *
477  * Function    :  socks5_connect
478  *
479  * Description :  Connect to the SOCKS server, and connect through
480  *                it to the specified server.   This handles
481  *                all the SOCKS negotiation, and returns a file
482  *                descriptor for a socket which can be treated as a
483  *                normal (non-SOCKS) socket.
484  *
485  * Parameters  :
486  *          1  :  fwd = Specifies the SOCKS proxy to use.
487  *          2  :  target_host = The final server to connect to.
488  *          3  :  target_port = The final port to connect to.
489  *          4  :  csp = Current client state (buffers, headers, etc...)
490  *
491  * Returns     :  JB_INVALID_SOCKET => failure, else a socket file descriptor.
492  *
493  *********************************************************************/
494 static jb_socket socks5_connect(const struct forward_spec *fwd,
495                                 const char *target_host,
496                                 int target_port,
497                                 struct client_state *csp)
498 {
499    int err = 0;
500    char cbuf[BUFFER_SIZE];
501    char sbuf[BUFFER_SIZE];
502    size_t client_pos = 0;
503    int server_size = 0;
504    size_t hostlen = 0;
505    jb_socket sfd;
506
507    if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
508    {
509       log_error(LOG_LEVEL_CONNECT, "socks5_connect: NULL gateway host specified");
510       err = 1;
511    }
512
513    if (fwd->gateway_port <= 0)
514    {
515       log_error(LOG_LEVEL_CONNECT, "socks5_connect: invalid gateway port specified");
516       err = 1;
517    }
518
519    hostlen = strlen(target_host);
520    if (hostlen > 255)
521    {
522       log_error(LOG_LEVEL_CONNECT, "socks5_connect: target host name is longer than 255 characters.");
523       err = 1;
524    }
525
526    if (fwd->type != SOCKS_5)
527    {
528       /* Should never get here */
529       log_error(LOG_LEVEL_FATAL, "SOCKS5 impossible internal error - bad SOCKS type.");
530       err = 1;
531    }
532
533    if (err)
534    {
535       errno = EINVAL;
536       return(JB_INVALID_SOCKET);
537    }
538
539    /* pass the request to the socks server */
540    sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
541
542    if (sfd == JB_INVALID_SOCKET)
543    {
544       return(JB_INVALID_SOCKET);
545    }
546
547    client_pos = 0;
548    cbuf[client_pos++] = '\x05'; /* Version */
549    cbuf[client_pos++] = '\x01'; /* One authentication method supported */
550    cbuf[client_pos++] = '\x00'; /* The no authentication authentication method */
551
552    if (write_socket(sfd, cbuf, client_pos))
553    {
554       log_error(LOG_LEVEL_CONNECT, "SOCKS5 negotiation write failed...");
555       close_socket(sfd);
556       return(JB_INVALID_SOCKET);
557    }
558
559    if (read_socket(sfd, sbuf, sizeof(sbuf)) != 2)
560    {
561       log_error(LOG_LEVEL_CONNECT, "SOCKS5 negotiation read failed...");
562       err = 1;
563    }
564
565    if (!err && (sbuf[0] != '\x05'))
566    {
567       log_error(LOG_LEVEL_CONNECT, "SOCKS5 negotiation protocol version error");
568       err = 1;
569    }
570
571    if (!err && (sbuf[1] == '\xff'))
572    {
573       log_error(LOG_LEVEL_CONNECT, "SOCKS5 authentication required");
574       err = 1;
575    }
576
577    if (!err && (sbuf[1] != '\x00'))
578    {
579       log_error(LOG_LEVEL_CONNECT, "SOCKS5 negotiation protocol error");
580       err = 1;
581    }
582
583    if (err)
584    {
585       close_socket(sfd);
586       errno = EINVAL;
587       return(JB_INVALID_SOCKET);
588    }
589
590    client_pos = 0;
591    cbuf[client_pos++] = '\x05'; /* Version */
592    cbuf[client_pos++] = '\x01'; /* TCP connect */
593    cbuf[client_pos++] = '\x00'; /* Reserved, must be 0x00 */
594    cbuf[client_pos++] = '\x03'; /* Address is domain name */
595    cbuf[client_pos++] = (char)(hostlen & 0xffu);
596    strncpy(cbuf + client_pos, target_host, 0xffu);
597    client_pos += (hostlen & 0xffu);
598    cbuf[client_pos++] = (char)((target_port >> 8) & 0xffu);
599    cbuf[client_pos++] = (char)((target_port     ) & 0xffu);
600
601    if (write_socket(sfd, cbuf, client_pos))
602    {
603       log_error(LOG_LEVEL_CONNECT, "SOCKS5 negotiation write failed...");
604       close_socket(sfd);
605       errno = EINVAL;
606       return(JB_INVALID_SOCKET);
607    }
608
609    server_size = read_socket(sfd, sbuf, sizeof(sbuf));
610    if (server_size < 3)
611    {
612       log_error(LOG_LEVEL_CONNECT, "SOCKS5 negotiation read failed...");
613       err = 1;
614    }
615
616    if (!err && (sbuf[0] != '\x05'))
617    {
618       log_error(LOG_LEVEL_CONNECT, "SOCKS5 negotiation protocol version error");
619       err = 1;
620    }
621
622    if (!err && (sbuf[2] != '\x00'))
623    {
624       log_error(LOG_LEVEL_CONNECT, "SOCKS5 negotiation protocol error");
625       err = 1;
626    }
627
628    if (!err)
629    {
630       switch (sbuf[1])
631       {
632          case SOCKS5_REQUEST_GRANTED:
633             return(sfd);
634             break;
635          case SOCKS5_REQUEST_FAILED:
636             log_error(LOG_LEVEL_CONNECT, "SOCKS5 request failed");
637             break;
638          case SOCKS5_REQUEST_DENIED:
639             log_error(LOG_LEVEL_CONNECT, "SOCKS5 request denied");
640             break;
641          case SOCKS5_REQUEST_NETWORK_UNREACHABLE:
642             log_error(LOG_LEVEL_CONNECT, "SOCKS5 request - network unreachable");
643             break;
644          case SOCKS5_REQUEST_HOST_UNREACHABLE:
645             log_error(LOG_LEVEL_CONNECT, "SOCKS5 request - host unreachable");
646             break;
647          case SOCKS5_REQUEST_CONNECTION_REFUSEDD:
648             log_error(LOG_LEVEL_CONNECT, "SOCKS5 request - connection refused");
649             break;
650          case SOCKS5_REQUEST_TTL_EXPIRED:
651             log_error(LOG_LEVEL_CONNECT, "SOCKS5 request - TTL expired");
652             break;
653          case SOCKS5_REQUEST_PROTOCOL_ERROR:
654             log_error(LOG_LEVEL_CONNECT, "SOCKS5 request - client protocol error");
655             break;
656          case SOCKS5_REQUEST_BAD_ADDRESS_TYPE:
657             log_error(LOG_LEVEL_CONNECT, "SOCKS5 request - domain names unsupported");
658             break;
659          default:
660             log_error(LOG_LEVEL_CONNECT, "SOCKS5 negotiation protocol error");
661             break;
662       }
663       err = 1;
664    }
665
666    close_socket(sfd);
667    errno = EINVAL;
668    return(JB_INVALID_SOCKET);
669 }
670
671 /*
672   Local Variables:
673   tab-width: 3
674   end:
675 */