562265101288ce9838099f326df8bc21dbc84e11
[privoxy.git] / jbsockets.c
1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.113 2012/03/09 16:23:50 fabiankeil Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/jbsockets.c,v $
5  *
6  * Purpose     :  Contains wrappers for system-specific sockets code,
7  *                so that the rest of Junkbuster can be more
8  *                OS-independent.  Contains #ifdefs to make this work
9  *                on many platforms.
10  *
11  * Copyright   :  Written by and Copyright (C) 2001-2011 the
12  *                Privoxy team. http://www.privoxy.org/
13  *
14  *                Based on the Internet Junkbuster originally written
15  *                by and Copyright (C) 1997 Anonymous Coders and
16  *                Junkbusters Corporation.  http://www.junkbusters.com
17  *
18  *                This program is free software; you can redistribute it
19  *                and/or modify it under the terms of the GNU General
20  *                Public License as published by the Free Software
21  *                Foundation; either version 2 of the License, or (at
22  *                your option) any later version.
23  *
24  *                This program is distributed in the hope that it will
25  *                be useful, but WITHOUT ANY WARRANTY; without even the
26  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
27  *                PARTICULAR PURPOSE.  See the GNU General Public
28  *                License for more details.
29  *
30  *                The GNU General Public License should be included with
31  *                this file.  If not, you can view it at
32  *                http://www.gnu.org/copyleft/gpl.html
33  *                or write to the Free Software Foundation, Inc., 59
34  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
35  *
36  *********************************************************************/
37
38
39 #include "config.h"
40
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <sys/types.h>
47
48 #ifdef _WIN32
49
50 #ifndef STRICT
51 #define STRICT
52 #endif
53 #include <windows.h>
54 #include <sys/timeb.h>
55 #include <io.h>
56
57 #else
58
59 #ifndef __OS2__
60 #include <unistd.h>
61 #endif
62 #include <sys/time.h>
63 #include <netinet/in.h>
64 #include <sys/ioctl.h>
65 #include <netdb.h>
66 #include <sys/socket.h>
67
68 #ifndef __BEOS__
69 #include <netinet/tcp.h>
70 #ifndef __OS2__
71 #include <arpa/inet.h>
72 #endif
73 #else
74 #include <socket.h>
75 #endif
76
77 #if defined(__EMX__) || defined (__OS2__)
78 #include <sys/select.h>  /* OS/2/EMX needs a little help with select */
79 #ifdef __OS2__
80 #include <nerrno.h>
81 #endif
82 #endif
83
84 #endif
85
86 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
87 #ifdef HAVE_POLL
88 #ifdef __GLIBC__
89 #include <sys/poll.h>
90 #else
91 #include <poll.h>
92 #endif /* def __GLIBC__ */
93 #endif /* HAVE_POLL */
94 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
95
96 #include "project.h"
97
98 /* For mutex semaphores only */
99 #include "jcc.h"
100
101 #include "jbsockets.h"
102 #include "filters.h"
103 #include "errlog.h"
104
105 /* Mac OSX doesn't define AI_NUMERICSESRV */
106 #ifndef AI_NUMERICSERV
107 #define AI_NUMERICSERV 0
108 #endif
109
110 const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
111
112 /*
113  * Maximum number of gethostbyname(_r) retries in case of
114  * soft errors (TRY_AGAIN).
115  * XXX: Does it make sense to make this a config option?
116  */
117 #define MAX_DNS_RETRIES 10
118
119 #define MAX_LISTEN_BACKLOG 128
120
121 #ifdef HAVE_RFC2553
122 static jb_socket rfc2553_connect_to(const char *host, int portnum, struct client_state *csp);
123 #else
124 static jb_socket no_rfc2553_connect_to(const char *host, int portnum, struct client_state *csp);
125 #endif
126
127 /*********************************************************************
128  *
129  * Function    :  connect_to
130  *
131  * Description :  Open a socket and connect to it.  Will check
132  *                that this is allowed according to ACL.
133  *
134  * Parameters  :
135  *          1  :  host = hostname to connect to
136  *          2  :  portnum = port to connent on (XXX: should be unsigned)
137  *          3  :  csp = Current client state (buffers, headers, etc...)
138  *
139  * Returns     :  JB_INVALID_SOCKET => failure, else it is the socket
140  *                file descriptor.
141  *
142  *********************************************************************/
143 jb_socket connect_to(const char *host, int portnum, struct client_state *csp)
144 {
145    jb_socket fd;
146    int forwarded_connect_retries = 0;
147
148    do
149    {
150       /*
151        * XXX: The whole errno overloading is ridiculous and should
152        *      be replaced with something sane and thread safe
153        */
154       /* errno = 0;*/
155 #ifdef HAVE_RFC2553
156       fd = rfc2553_connect_to(host, portnum, csp);
157 #else
158       fd = no_rfc2553_connect_to(host, portnum, csp);
159 #endif
160       if ((fd != JB_INVALID_SOCKET) || (errno == EINVAL)
161          || (csp->fwd == NULL)
162          || ((csp->fwd->forward_host == NULL) && (csp->fwd->type == SOCKS_NONE)))
163       {
164          break;
165       }
166       forwarded_connect_retries++;
167       if (csp->config->forwarded_connect_retries != 0)
168       {
169          log_error(LOG_LEVEL_ERROR,
170             "Attempt %d of %d to connect to %s failed. Trying again.",
171             forwarded_connect_retries, csp->config->forwarded_connect_retries + 1, host);
172       }
173
174    } while (forwarded_connect_retries < csp->config->forwarded_connect_retries);
175
176    return fd;
177 }
178
179 #ifdef HAVE_RFC2553
180 /* Getaddrinfo implementation */
181 static jb_socket rfc2553_connect_to(const char *host, int portnum, struct client_state *csp)
182 {
183    struct addrinfo hints, *result, *rp;
184    char service[6];
185    int retval;
186    jb_socket fd;
187    fd_set wfds;
188    struct timeval timeout;
189 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
190    int   flags;
191 #endif
192    int connect_failed;
193    /*
194     * XXX: Initializeing it here is only necessary
195     *      because not all situations are properly
196     *      covered yet.
197     */
198    int socket_error = 0;
199
200 #ifdef FEATURE_ACL
201    struct access_control_addr dst[1];
202 #endif /* def FEATURE_ACL */
203
204    /* Don't leak memory when retrying. */
205    freez(csp->error_message);
206    freez(csp->http->host_ip_addr_str);
207
208    retval = snprintf(service, sizeof(service), "%d", portnum);
209    if ((-1 == retval) || (sizeof(service) <= retval))
210    {
211       log_error(LOG_LEVEL_ERROR,
212          "Port number (%d) ASCII decimal representation doesn't fit into 6 bytes",
213          portnum);
214       csp->error_message = strdup("Invalid port number");
215       csp->http->host_ip_addr_str = strdup("unknown");
216       return(JB_INVALID_SOCKET);
217    }
218
219    memset((char *)&hints, 0, sizeof(hints));
220    hints.ai_family = AF_UNSPEC;
221    hints.ai_socktype = SOCK_STREAM;
222    hints.ai_flags = AI_NUMERICSERV; /* avoid service look-up */
223 #ifdef AI_ADDRCONFIG
224    hints.ai_flags |= AI_ADDRCONFIG;
225 #endif
226    if ((retval = getaddrinfo(host, service, &hints, &result)))
227    {
228       log_error(LOG_LEVEL_INFO,
229          "Can not resolve %s: %s", host, gai_strerror(retval));
230       /* XXX: Should find a better way to propagate this error. */
231       errno = EINVAL;
232       csp->error_message = strdup(gai_strerror(retval));
233       csp->http->host_ip_addr_str = strdup("unknown");
234       return(JB_INVALID_SOCKET);
235    }
236
237    csp->http->host_ip_addr_str = malloc(NI_MAXHOST);
238    if (NULL == csp->http->host_ip_addr_str)
239    {
240       freeaddrinfo(result);
241       log_error(LOG_LEVEL_ERROR,
242          "Out of memory while getting the server IP address.");
243       return JB_INVALID_SOCKET;
244    }
245
246    for (rp = result; rp != NULL; rp = rp->ai_next)
247    {
248
249 #ifdef FEATURE_ACL
250       memcpy(&dst->addr, rp->ai_addr, rp->ai_addrlen);
251
252       if (block_acl(dst, csp))
253       {
254 #ifdef __OS2__
255          socket_error = errno = SOCEPERM;
256 #else
257          socket_error = errno = EPERM;
258 #endif
259          continue;
260       }
261 #endif /* def FEATURE_ACL */
262
263       retval = getnameinfo(rp->ai_addr, rp->ai_addrlen,
264          csp->http->host_ip_addr_str, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
265       if (retval)
266       {
267          log_error(LOG_LEVEL_ERROR,
268             "Failed to get the host name from the socket structure: %s",
269             gai_strerror(retval));
270          continue;
271       }
272
273       fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
274 #ifdef _WIN32
275       if (fd == JB_INVALID_SOCKET)
276 #else
277       if (fd < 0)
278 #endif
279       {
280          continue;
281       }
282
283 #ifdef TCP_NODELAY
284       {  /* turn off TCP coalescence */
285          int mi = 1;
286          setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
287       }
288 #endif /* def TCP_NODELAY */
289
290 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
291       if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
292       {
293          flags |= O_NDELAY;
294          fcntl(fd, F_SETFL, flags);
295       }
296 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
297
298       connect_failed = 0;
299       while (connect(fd, rp->ai_addr, rp->ai_addrlen) == JB_INVALID_SOCKET)
300       {
301 #ifdef __OS2__
302          errno = sock_errno();
303 #endif /* __OS2__ */
304
305 #ifdef _WIN32
306          if (errno == WSAEINPROGRESS)
307 #else /* ifndef _WIN32 */
308          if (errno == EINPROGRESS)
309 #endif /* ndef _WIN32 || __OS2__ */
310          {
311             break;
312          }
313
314          if (errno != EINTR)
315          {
316             socket_error = errno;
317             close_socket(fd);
318             connect_failed = 1;
319             break;
320          }
321       }
322       if (connect_failed)
323       {
324          continue;
325       }
326
327 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
328       if (flags != -1)
329       {
330          flags &= ~O_NDELAY;
331          fcntl(fd, F_SETFL, flags);
332       }
333 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
334
335       /* wait for connection to complete */
336       FD_ZERO(&wfds);
337       FD_SET(fd, &wfds);
338
339       memset(&timeout, 0, sizeof(timeout));
340       timeout.tv_sec  = 30;
341
342       /* MS Windows uses int, not SOCKET, for the 1st arg of select(). Weird! */
343       if ((select((int)fd + 1, NULL, &wfds, NULL, &timeout) > 0)
344          && FD_ISSET(fd, &wfds))
345       {
346          socklen_t optlen = sizeof(socket_error);
347          if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &socket_error, &optlen))
348          {
349             if (!socket_error)
350             {
351                /* Connection established, no need to try other addresses. */
352                break;
353             }
354             if (rp->ai_next != NULL)
355             {
356                /*
357                 * There's another address we can try, so log that this
358                 * one didn't work out. If the last one fails, too,
359                 * it will get logged outside the loop body so we don't
360                 * have to mention it here.
361                 */
362                log_error(LOG_LEVEL_CONNECT, "Could not connect to [%s]:%s: %s.",
363                   csp->http->host_ip_addr_str, service, strerror(socket_error));
364             }
365          }
366          else
367          {
368             socket_error = errno;
369             log_error(LOG_LEVEL_ERROR, "Could not get the state of "
370                "the connection to [%s]:%s: %s; dropping connection.",
371                csp->http->host_ip_addr_str, service, strerror(errno));
372          }
373       }
374
375       /* Connection failed, try next address */
376       close_socket(fd);
377    }
378
379    freeaddrinfo(result);
380    if (!rp)
381    {
382       log_error(LOG_LEVEL_CONNECT, "Could not connect to [%s]:%s: %s.",
383          host, service, strerror(socket_error));
384       csp->error_message = strdup(strerror(socket_error));
385       return(JB_INVALID_SOCKET);
386    }
387    log_error(LOG_LEVEL_CONNECT, "Connected to %s[%s]:%s.",
388       host, csp->http->host_ip_addr_str, service);
389
390    return(fd);
391
392 }
393
394 #else /* ndef HAVE_RFC2553 */
395 /* Pre-getaddrinfo implementation */
396
397 static jb_socket no_rfc2553_connect_to(const char *host, int portnum, struct client_state *csp)
398 {
399    struct sockaddr_in inaddr;
400    jb_socket fd;
401    unsigned int addr;
402    fd_set wfds;
403    struct timeval tv[1];
404 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
405    int   flags;
406 #endif
407
408 #ifdef FEATURE_ACL
409    struct access_control_addr dst[1];
410 #endif /* def FEATURE_ACL */
411
412    /* Don't leak memory when retrying. */
413    freez(csp->http->host_ip_addr_str);
414
415    memset((char *)&inaddr, 0, sizeof inaddr);
416
417    if ((addr = resolve_hostname_to_ip(host)) == INADDR_NONE)
418    {
419       csp->http->host_ip_addr_str = strdup("unknown");
420       return(JB_INVALID_SOCKET);
421    }
422
423 #ifdef FEATURE_ACL
424    dst->addr = ntohl(addr);
425    dst->port = portnum;
426
427    if (block_acl(dst, csp))
428    {
429 #ifdef __OS2__
430       errno = SOCEPERM;
431 #else
432       errno = EPERM;
433 #endif
434       return(JB_INVALID_SOCKET);
435    }
436 #endif /* def FEATURE_ACL */
437
438    inaddr.sin_addr.s_addr = addr;
439    inaddr.sin_family      = AF_INET;
440    csp->http->host_ip_addr_str = strdup(inet_ntoa(inaddr.sin_addr));
441
442 #ifndef _WIN32
443    if (sizeof(inaddr.sin_port) == sizeof(short))
444 #endif /* ndef _WIN32 */
445    {
446       inaddr.sin_port = htons((unsigned short) portnum);
447    }
448 #ifndef _WIN32
449    else
450    {
451       inaddr.sin_port = htonl((unsigned long)portnum);
452    }
453 #endif /* ndef _WIN32 */
454
455    fd = socket(inaddr.sin_family, SOCK_STREAM, 0);
456 #ifdef _WIN32
457    if (fd == JB_INVALID_SOCKET)
458 #else
459    if (fd < 0)
460 #endif
461    {
462       return(JB_INVALID_SOCKET);
463    }
464
465 #ifdef TCP_NODELAY
466    {  /* turn off TCP coalescence */
467       int mi = 1;
468       setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
469    }
470 #endif /* def TCP_NODELAY */
471
472 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
473    if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
474    {
475       flags |= O_NDELAY;
476       fcntl(fd, F_SETFL, flags);
477    }
478 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
479
480    while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == JB_INVALID_SOCKET)
481    {
482 #ifdef _WIN32
483       if (errno == WSAEINPROGRESS)
484 #elif __OS2__
485       if (sock_errno() == EINPROGRESS)
486 #else /* ifndef _WIN32 */
487       if (errno == EINPROGRESS)
488 #endif /* ndef _WIN32 || __OS2__ */
489       {
490          break;
491       }
492
493 #ifdef __OS2__
494       if (sock_errno() != EINTR)
495 #else
496       if (errno != EINTR)
497 #endif /* __OS2__ */
498       {
499          close_socket(fd);
500          return(JB_INVALID_SOCKET);
501       }
502    }
503
504 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
505    if (flags != -1)
506    {
507       flags &= ~O_NDELAY;
508       fcntl(fd, F_SETFL, flags);
509    }
510 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
511
512    /* wait for connection to complete */
513    FD_ZERO(&wfds);
514    FD_SET(fd, &wfds);
515
516    tv->tv_sec  = 30;
517    tv->tv_usec = 0;
518
519    /* MS Windows uses int, not SOCKET, for the 1st arg of select(). Weird! */
520    if (select((int)fd + 1, NULL, &wfds, NULL, tv) <= 0)
521    {
522       close_socket(fd);
523       return(JB_INVALID_SOCKET);
524    }
525    return(fd);
526
527 }
528 #endif /* ndef HAVE_RFC2553 */
529
530
531 /*********************************************************************
532  *
533  * Function    :  write_socket
534  *
535  * Description :  Write the contents of buf (for n bytes) to socket fd.
536  *
537  * Parameters  :
538  *          1  :  fd = file descriptor (aka. handle) of socket to write to.
539  *          2  :  buf = pointer to data to be written.
540  *          3  :  len = length of data to be written to the socket "fd".
541  *
542  * Returns     :  0 on success (entire buffer sent).
543  *                nonzero on error.
544  *
545  *********************************************************************/
546 #ifdef AMIGA
547 int write_socket(jb_socket fd, const char *buf, ssize_t len)
548 #else
549 int write_socket(jb_socket fd, const char *buf, size_t len)
550 #endif
551 {
552    if (len == 0)
553    {
554       return 0;
555    }
556
557    log_error(LOG_LEVEL_WRITING, "to socket %d: %N", fd, len, buf);
558
559 #if defined(_WIN32)
560    return (send(fd, buf, (int)len, 0) != (int)len);
561 #elif defined(__BEOS__) || defined(AMIGA)
562    return (send(fd, buf, len, 0) != len);
563 #elif defined(__OS2__)
564    /*
565     * Break the data up into SOCKET_SEND_MAX chunks for sending...
566     * OS/2 seemed to complain when the chunks were too large.
567     */
568 #define SOCKET_SEND_MAX 65000
569    {
570       int send_len, send_rc = 0, i = 0;
571       while ((i < len) && (send_rc != -1))
572       {
573          if ((i + SOCKET_SEND_MAX) > len)
574             send_len = len - i;
575          else
576             send_len = SOCKET_SEND_MAX;
577          send_rc = send(fd,(char*)buf + i, send_len, 0);
578          if (send_rc == -1)
579             return 1;
580          i = i + send_len;
581       }
582       return 0;
583    }
584 #else
585    return (write(fd, buf, len) != len);
586 #endif
587
588 }
589
590
591 /*********************************************************************
592  *
593  * Function    :  read_socket
594  *
595  * Description :  Read from a TCP/IP socket in a platform independent way.
596  *
597  * Parameters  :
598  *          1  :  fd = file descriptor of the socket to read
599  *          2  :  buf = pointer to buffer where data will be written
600  *                Must be >= len bytes long.
601  *          3  :  len = maximum number of bytes to read
602  *
603  * Returns     :  On success, the number of bytes read is returned (zero
604  *                indicates end of file), and the file position is advanced
605  *                by this number.  It is not an error if this number is
606  *                smaller than the number of bytes requested; this may hap-
607  *                pen for example because fewer bytes are actually available
608  *                right now (maybe because we were close to end-of-file, or
609  *                because we are reading from a pipe, or from a terminal,
610  *                or because read() was interrupted by a signal).  On error,
611  *                -1 is returned, and errno is set appropriately.  In this
612  *                case it is left unspecified whether the file position (if
613  *                any) changes.
614  *
615  *********************************************************************/
616 int read_socket(jb_socket fd, char *buf, int len)
617 {
618    int ret;
619
620    if (len <= 0)
621    {
622       return(0);
623    }
624
625 #if defined(_WIN32)
626    ret = recv(fd, buf, len, 0);
627 #elif defined(__BEOS__) || defined(AMIGA) || defined(__OS2__)
628    ret = recv(fd, buf, (size_t)len, 0);
629 #else
630    ret = (int)read(fd, buf, (size_t)len);
631 #endif
632
633    if (ret > 0)
634    {
635       log_error(LOG_LEVEL_RECEIVED, "from socket %d: %N", fd, ret, buf);
636    }
637
638    return ret;
639 }
640
641
642 /*********************************************************************
643  *
644  * Function    :  data_is_available
645  *
646  * Description :  Waits for data to arrive on a socket.
647  *
648  * Parameters  :
649  *          1  :  fd = file descriptor of the socket to read
650  *          2  :  seconds_to_wait = number of seconds after which we give up.
651  *
652  * Returns     :  TRUE if data arrived in time,
653  *                FALSE otherwise.
654  *
655  *********************************************************************/
656 int data_is_available(jb_socket fd, int seconds_to_wait)
657 {
658    char buf[10];
659    fd_set rfds;
660    struct timeval timeout;
661    int n;
662
663    memset(&timeout, 0, sizeof(timeout));
664    timeout.tv_sec = seconds_to_wait;
665
666 #ifdef __OS2__
667    /* Copy and pasted from jcc.c ... */
668    memset(&rfds, 0, sizeof(fd_set));
669 #else
670    FD_ZERO(&rfds);
671 #endif
672    FD_SET(fd, &rfds);
673
674    n = select(fd+1, &rfds, NULL, NULL, &timeout);
675
676    /*
677     * XXX: Do we care about the different error conditions?
678     */
679    return ((n == 1) && (1 == recv(fd, buf, 1, MSG_PEEK)));
680 }
681
682
683 /*********************************************************************
684  *
685  * Function    :  close_socket
686  *
687  * Description :  Closes a TCP/IP socket
688  *
689  * Parameters  :
690  *          1  :  fd = file descriptor of socket to be closed
691  *
692  * Returns     :  void
693  *
694  *********************************************************************/
695 void close_socket(jb_socket fd)
696 {
697 #if defined(_WIN32) || defined(__BEOS__)
698    closesocket(fd);
699 #elif defined(AMIGA)
700    CloseSocket(fd);
701 #elif defined(__OS2__)
702    soclose(fd);
703 #else
704    close(fd);
705 #endif
706
707 }
708
709
710 /*********************************************************************
711  *
712  * Function    :  bind_port
713  *
714  * Description :  Call socket, set socket options, and listen.
715  *                Called by listen_loop to "boot up" our proxy address.
716  *
717  * Parameters  :
718  *          1  :  hostnam = TCP/IP address to bind/listen to
719  *          2  :  portnum = port to listen on
720  *          3  :  pfd = pointer used to return file descriptor.
721  *
722  * Returns     :  if success, returns 0 and sets *pfd.
723  *                if failure, returns -3 if address is in use,
724  *                                    -2 if address unresolvable,
725  *                                    -1 otherwise
726  *********************************************************************/
727 int bind_port(const char *hostnam, int portnum, jb_socket *pfd)
728 {
729 #ifdef HAVE_RFC2553
730    struct addrinfo hints;
731    struct addrinfo *result, *rp;
732    /*
733     * XXX: portnum should be a string to allow symbolic service
734     * names in the configuration file and to avoid the following
735     * int2string.
736     */
737    char servnam[6];
738    int retval;
739 #else
740    struct sockaddr_in inaddr;
741 #endif /* def HAVE_RFC2553 */
742    jb_socket fd;
743 #ifndef _WIN32
744    int one = 1;
745 #endif /* ndef _WIN32 */
746
747    *pfd = JB_INVALID_SOCKET;
748
749 #ifdef HAVE_RFC2553
750    retval = snprintf(servnam, sizeof(servnam), "%d", portnum);
751    if ((-1 == retval) || (sizeof(servnam) <= retval))
752    {
753       log_error(LOG_LEVEL_ERROR,
754          "Port number (%d) ASCII decimal representation doesn't fit into 6 bytes",
755          portnum);
756       return -1;
757    }
758
759    memset(&hints, 0, sizeof(struct addrinfo));
760    if (hostnam == NULL)
761    {
762       /*
763        * XXX: This is a hack. The right thing to do
764        * would be to bind to both AF_INET and AF_INET6.
765        * This will also fail if there is no AF_INET
766        * version available.
767        */
768       hints.ai_family = AF_INET;
769    }
770    else
771    {
772       hints.ai_family = AF_UNSPEC;
773    }
774    hints.ai_socktype = SOCK_STREAM;
775    hints.ai_flags = AI_PASSIVE;
776    hints.ai_protocol = 0; /* Really any stream protocol or TCP only */
777    hints.ai_canonname = NULL;
778    hints.ai_addr = NULL;
779    hints.ai_next = NULL;
780
781    if ((retval = getaddrinfo(hostnam, servnam, &hints, &result)))
782    {
783       log_error(LOG_LEVEL_ERROR,
784          "Can not resolve %s: %s", hostnam, gai_strerror(retval));
785       return -2;
786    }
787 #else
788    memset((char *)&inaddr, '\0', sizeof inaddr);
789
790    inaddr.sin_family      = AF_INET;
791    inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
792
793    if (inaddr.sin_addr.s_addr == INADDR_NONE)
794    {
795       return(-2);
796    }
797
798 #ifndef _WIN32
799    if (sizeof(inaddr.sin_port) == sizeof(short))
800 #endif /* ndef _WIN32 */
801    {
802       inaddr.sin_port = htons((unsigned short) portnum);
803    }
804 #ifndef _WIN32
805    else
806    {
807       inaddr.sin_port = htonl((unsigned long) portnum);
808    }
809 #endif /* ndef _WIN32 */
810 #endif /* def HAVE_RFC2553 */
811
812 #ifdef HAVE_RFC2553
813    for (rp = result; rp != NULL; rp = rp->ai_next)
814    {
815       fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
816 #else
817    fd = socket(AF_INET, SOCK_STREAM, 0);
818 #endif /* def HAVE_RFC2553 */
819
820 #ifdef _WIN32
821    if (fd == JB_INVALID_SOCKET)
822 #else
823    if (fd < 0)
824 #endif
825    {
826 #ifdef HAVE_RFC2553
827       continue;
828 #else
829       return(-1);
830 #endif
831    }
832
833 #ifndef _WIN32
834    /*
835     * This is not needed for Win32 - in fact, it stops
836     * duplicate instances of Privoxy from being caught.
837     *
838     * On UNIX, we assume the user is sensible enough not
839     * to start Privoxy multiple times on the same IP.
840     * Without this, stopping and restarting Privoxy
841     * from a script fails.
842     * Note: SO_REUSEADDR is meant to only take over
843     * sockets which are *not* in listen state in Linux,
844     * e.g. sockets in TIME_WAIT. YMMV.
845     */
846    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
847 #endif /* ndef _WIN32 */
848
849 #ifdef HAVE_RFC2553
850    if (bind(fd, rp->ai_addr, rp->ai_addrlen) < 0)
851 #else
852    if (bind(fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
853 #endif
854    {
855 #ifdef _WIN32
856       errno = WSAGetLastError();
857       if (errno == WSAEADDRINUSE)
858 #else
859       if (errno == EADDRINUSE)
860 #endif
861       {
862 #ifdef HAVE_RFC2553
863          freeaddrinfo(result);
864 #endif
865          close_socket(fd);
866          return(-3);
867       }
868       else
869       {
870          close_socket(fd);
871 #ifndef HAVE_RFC2553
872          return(-1);
873       }
874    }
875 #else
876       }
877    }
878    else
879    {
880       /* bind() succeeded, escape from for-loop */
881       /*
882        * XXX: Support multiple listening sockets (e.g. localhost
883        * resolves to AF_INET and AF_INET6, but only the first address
884        * is used
885        */
886       break;
887    }
888    }
889
890    freeaddrinfo(result);
891    if (rp == NULL)
892    {
893       /* All bind()s failed */
894       return(-1);
895    }
896 #endif /* ndef HAVE_RFC2553 */
897
898    while (listen(fd, MAX_LISTEN_BACKLOG) == -1)
899    {
900       if (errno != EINTR)
901       {
902          return(-1);
903       }
904    }
905
906    *pfd = fd;
907    return 0;
908
909 }
910
911
912 /*********************************************************************
913  *
914  * Function    :  get_host_information
915  *
916  * Description :  Determines the IP address the client used to
917  *                reach us and the hostname associated with it.
918  *
919  *                XXX: Most of the code has been copy and pasted
920  *                from accept_connection() and not all of the
921  *                ifdefs paths have been tested afterwards.
922  *
923  * Parameters  :
924  *          1  :  afd = File descriptor returned from accept().
925  *          2  :  ip_address = Pointer to return the pointer to
926  *                             the ip address string.
927  *          3  :  port =       Pointer to return the pointer to
928  *                             the TCP port string.
929  *          4  :  hostname =   Pointer to return the pointer to
930  *                             the hostname or NULL if the caller
931  *                             isn't interested in it.
932  *
933  * Returns     :  void.
934  *
935  *********************************************************************/
936 void get_host_information(jb_socket afd, char **ip_address, char **port,
937                           char **hostname)
938 {
939 #ifdef HAVE_RFC2553
940    struct sockaddr_storage server;
941    int retval;
942 #else
943    struct sockaddr_in server;
944    struct hostent *host = NULL;
945 #endif /* HAVE_RFC2553 */
946 #if defined(_WIN32) || defined(__OS2__) || defined(__APPLE_CC__) || defined(AMIGA)
947    /* according to accept_connection() this fixes a warning. */
948    int s_length, s_length_provided;
949 #else
950    socklen_t s_length, s_length_provided;
951 #endif
952 #ifndef HAVE_RFC2553
953 #if defined(HAVE_GETHOSTBYADDR_R_8_ARGS) ||  defined(HAVE_GETHOSTBYADDR_R_7_ARGS) || defined(HAVE_GETHOSTBYADDR_R_5_ARGS)
954    struct hostent result;
955 #if defined(HAVE_GETHOSTBYADDR_R_5_ARGS)
956    struct hostent_data hdata;
957 #else
958    char hbuf[HOSTENT_BUFFER_SIZE];
959    int thd_err;
960 #endif /* def HAVE_GETHOSTBYADDR_R_5_ARGS */
961 #endif /* def HAVE_GETHOSTBYADDR_R_(8|7|5)_ARGS */
962 #endif /* ifndef HAVE_RFC2553 */
963    s_length = s_length_provided = sizeof(server);
964
965    if (NULL != hostname)
966    {
967       *hostname = NULL;
968    }
969    *ip_address = NULL;
970    *port = NULL;
971
972    if (!getsockname(afd, (struct sockaddr *) &server, &s_length))
973    {
974       if (s_length > s_length_provided)
975       {
976          log_error(LOG_LEVEL_ERROR, "getsockname() truncated server address");
977          return;
978       }
979 /*
980  * XXX: Workaround for missing header on Windows when
981  *      configured with --disable-ipv6-support.
982  *      The proper fix is to not use NI_MAXSERV in
983  *      that case. It works by accident on other platforms
984  *      as <netdb.h> in included unconditionally there.
985  */
986 #ifndef NI_MAXSERV
987 #define NI_MAXSERV 32
988 #endif
989       *port = malloc(NI_MAXSERV);
990       if (NULL == *port)
991       {
992          log_error(LOG_LEVEL_ERROR,
993             "Out of memory while getting the client's port.");
994          return;
995       }
996 #ifdef HAVE_RFC2553
997       *ip_address = malloc(NI_MAXHOST);
998       if (NULL == *ip_address)
999       {
1000          log_error(LOG_LEVEL_ERROR,
1001             "Out of memory while getting the client's IP address.");
1002          freez(*port);
1003          return;
1004       }
1005       retval = getnameinfo((struct sockaddr *) &server, s_length,
1006          *ip_address, NI_MAXHOST, *port, NI_MAXSERV,
1007          NI_NUMERICHOST|NI_NUMERICSERV);
1008       if (retval)
1009       {
1010          log_error(LOG_LEVEL_ERROR,
1011             "Unable to print my own IP address: %s", gai_strerror(retval));
1012          freez(*ip_address);
1013          freez(*port);
1014          return;
1015       }
1016 #else
1017       *ip_address = strdup(inet_ntoa(server.sin_addr));
1018       snprintf(*port, NI_MAXSERV, "%hu", ntohs(server.sin_port));
1019 #endif /* HAVE_RFC2553 */
1020       if (NULL == hostname)
1021       {
1022          /*
1023           * We're done here, the caller isn't
1024           * interested in knowing the hostname.
1025           */
1026          return;
1027       }
1028
1029 #ifdef HAVE_RFC2553
1030       *hostname = malloc(NI_MAXHOST);
1031       if (NULL == *hostname)
1032       {
1033          log_error(LOG_LEVEL_ERROR,
1034             "Out of memory while getting the client's hostname.");
1035          return;
1036       }
1037       retval = getnameinfo((struct sockaddr *) &server, s_length,
1038          *hostname, NI_MAXHOST, NULL, 0, NI_NAMEREQD);
1039       if (retval)
1040       {
1041          log_error(LOG_LEVEL_ERROR,
1042             "Unable to resolve my own IP address: %s", gai_strerror(retval));
1043          freez(*hostname);
1044       }
1045 #else
1046 #if defined(HAVE_GETHOSTBYADDR_R_8_ARGS)
1047       gethostbyaddr_r((const char *)&server.sin_addr,
1048                       sizeof(server.sin_addr), AF_INET,
1049                       &result, hbuf, HOSTENT_BUFFER_SIZE,
1050                       &host, &thd_err);
1051 #elif defined(HAVE_GETHOSTBYADDR_R_7_ARGS)
1052       host = gethostbyaddr_r((const char *)&server.sin_addr,
1053                       sizeof(server.sin_addr), AF_INET,
1054                       &result, hbuf, HOSTENT_BUFFER_SIZE, &thd_err);
1055 #elif defined(HAVE_GETHOSTBYADDR_R_5_ARGS)
1056       if (0 == gethostbyaddr_r((const char *)&server.sin_addr,
1057                                sizeof(server.sin_addr), AF_INET,
1058                                &result, &hdata))
1059       {
1060          host = &result;
1061       }
1062       else
1063       {
1064          host = NULL;
1065       }
1066 #elif defined(MUTEX_LOCKS_AVAILABLE)
1067       privoxy_mutex_lock(&resolver_mutex);
1068       host = gethostbyaddr((const char *)&server.sin_addr,
1069                            sizeof(server.sin_addr), AF_INET);
1070       privoxy_mutex_unlock(&resolver_mutex);
1071 #else
1072       host = gethostbyaddr((const char *)&server.sin_addr,
1073                            sizeof(server.sin_addr), AF_INET);
1074 #endif
1075       if (host == NULL)
1076       {
1077          log_error(LOG_LEVEL_ERROR, "Unable to get my own hostname: %E\n");
1078       }
1079       else
1080       {
1081          *hostname = strdup(host->h_name);
1082       }
1083 #endif /* else def HAVE_RFC2553 */
1084    }
1085
1086    return;
1087 }
1088
1089
1090 /*********************************************************************
1091  *
1092  * Function    :  accept_connection
1093  *
1094  * Description :  Accepts a connection on one of possibly multiple
1095  *                sockets. The socket(s) to check must have been
1096  *                created using bind_port().
1097  *
1098  * Parameters  :
1099  *          1  :  csp = Client state, cfd, ip_addr_str, and
1100  *                      ip_addr_long will be set by this routine.
1101  *          2  :  fds = File descriptors returned from bind_port
1102  *
1103  * Returns     :  when a connection is accepted, it returns 1 (TRUE).
1104  *                On an error it returns 0 (FALSE).
1105  *
1106  *********************************************************************/
1107 int accept_connection(struct client_state * csp, jb_socket fds[])
1108 {
1109 #ifdef HAVE_RFC2553
1110    /* XXX: client is stored directly into csp->tcp_addr */
1111 #define client (csp->tcp_addr)
1112 #else
1113    struct sockaddr_in client;
1114 #endif
1115    jb_socket afd;
1116 #if defined(_WIN32) || defined(__OS2__) || defined(__APPLE_CC__) || defined(AMIGA)
1117    /* Wierdness - fix a warning. */
1118    int c_length;
1119 #else
1120    socklen_t c_length;
1121 #endif
1122    int retval;
1123    int i;
1124    int max_selected_socket;
1125    fd_set selected_fds;
1126    jb_socket fd;
1127
1128    c_length = sizeof(client);
1129
1130    /*
1131     * Wait for a connection on any socket.
1132     * Return immediately if no socket is listening.
1133     * XXX: Why not treat this as fatal error?
1134     */
1135    FD_ZERO(&selected_fds);
1136    max_selected_socket = 0;
1137    for (i = 0; i < MAX_LISTENING_SOCKETS; i++)
1138    {
1139       if (JB_INVALID_SOCKET != fds[i])
1140       {
1141          FD_SET(fds[i], &selected_fds);
1142          if (max_selected_socket < fds[i] + 1)
1143          {
1144             max_selected_socket = fds[i] + 1;
1145          }
1146       }
1147    }
1148    if (0 == max_selected_socket)
1149    {
1150       return 0;
1151    }
1152    do
1153    {
1154       retval = select(max_selected_socket, &selected_fds, NULL, NULL, NULL);
1155    } while (retval < 0 && errno == EINTR);
1156    if (retval <= 0)
1157    {
1158       if (0 == retval)
1159       {
1160          log_error(LOG_LEVEL_ERROR,
1161             "Waiting on new client failed because select(2) returned 0."
1162             " This should not happen.");
1163       }
1164       else
1165       {
1166          log_error(LOG_LEVEL_ERROR,
1167             "Waiting on new client failed because of problems in select(2): "
1168             "%s.", strerror(errno));
1169       }
1170       return 0;
1171    }
1172    for (i = 0; i < MAX_LISTENING_SOCKETS && !FD_ISSET(fds[i], &selected_fds);
1173          i++);
1174    if (i >= MAX_LISTENING_SOCKETS)
1175    {
1176       log_error(LOG_LEVEL_ERROR,
1177          "select(2) reported connected clients (number = %u, "
1178          "descriptor boundary = %u), but none found.",
1179          retval, max_selected_socket);
1180       return 0;
1181    }
1182    fd = fds[i];
1183
1184    /* Accept selected connection */
1185 #ifdef _WIN32
1186    afd = accept (fd, (struct sockaddr *) &client, &c_length);
1187    if (afd == JB_INVALID_SOCKET)
1188    {
1189       return 0;
1190    }
1191 #else
1192    do
1193    {
1194 #if defined(FEATURE_ACCEPT_FILTER) && defined(SO_ACCEPTFILTER)
1195       struct accept_filter_arg af_options;
1196       bzero(&af_options, sizeof(af_options));
1197       strlcpy(af_options.af_name, "httpready", sizeof(af_options.af_name));
1198       setsockopt(fd, SOL_SOCKET, SO_ACCEPTFILTER, &af_options, sizeof(af_options));
1199 #endif
1200       afd = accept (fd, (struct sockaddr *) &client, &c_length);
1201    } while (afd < 1 && errno == EINTR);
1202    if (afd < 0)
1203    {
1204       return 0;
1205    }
1206 #endif
1207
1208    csp->cfd = afd;
1209 #ifdef HAVE_RFC2553
1210    csp->ip_addr_str = malloc(NI_MAXHOST);
1211    if (NULL == csp->ip_addr_str)
1212    {
1213       log_error(LOG_LEVEL_ERROR,
1214          "Out of memory while getting the client's IP address.");
1215       return 0;
1216    }
1217    retval = getnameinfo((struct sockaddr *) &client, c_length,
1218          csp->ip_addr_str, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
1219    if (!csp->ip_addr_str || retval)
1220    {
1221       log_error(LOG_LEVEL_ERROR, "Can not save csp->ip_addr_str: %s",
1222          (csp->ip_addr_str) ? gai_strerror(retval) : "Insuffcient memory");
1223       freez(csp->ip_addr_str);
1224    }
1225 #undef client
1226 #else
1227    csp->ip_addr_str  = strdup(inet_ntoa(client.sin_addr));
1228    csp->ip_addr_long = ntohl(client.sin_addr.s_addr);
1229 #endif /* def HAVE_RFC2553 */
1230
1231    return 1;
1232
1233 }
1234
1235
1236 /*********************************************************************
1237  *
1238  * Function    :  resolve_hostname_to_ip
1239  *
1240  * Description :  Resolve a hostname to an internet tcp/ip address.
1241  *                NULL or an empty string resolve to INADDR_ANY.
1242  *
1243  * Parameters  :
1244  *          1  :  host = hostname to resolve
1245  *
1246  * Returns     :  INADDR_NONE => failure, INADDR_ANY or tcp/ip address if successful.
1247  *
1248  *********************************************************************/
1249 unsigned long resolve_hostname_to_ip(const char *host)
1250 {
1251    struct sockaddr_in inaddr;
1252    struct hostent *hostp;
1253 #if defined(HAVE_GETHOSTBYNAME_R_6_ARGS) || defined(HAVE_GETHOSTBYNAME_R_5_ARGS) || defined(HAVE_GETHOSTBYNAME_R_3_ARGS)
1254    struct hostent result;
1255 #if defined(HAVE_GETHOSTBYNAME_R_6_ARGS) || defined(HAVE_GETHOSTBYNAME_R_5_ARGS)
1256    char hbuf[HOSTENT_BUFFER_SIZE];
1257    int thd_err;
1258 #else /* defined(HAVE_GETHOSTBYNAME_R_3_ARGS) */
1259    struct hostent_data hdata;
1260 #endif /* def HAVE_GETHOSTBYNAME_R_(6|5)_ARGS */
1261 #endif /* def HAVE_GETHOSTBYNAME_R_(6|5|3)_ARGS */
1262
1263    if ((host == NULL) || (*host == '\0'))
1264    {
1265       return(INADDR_ANY);
1266    }
1267
1268    memset((char *) &inaddr, 0, sizeof inaddr);
1269
1270    if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
1271    {
1272       unsigned int dns_retries = 0;
1273 #if defined(HAVE_GETHOSTBYNAME_R_6_ARGS)
1274       while (gethostbyname_r(host, &result, hbuf,
1275                 HOSTENT_BUFFER_SIZE, &hostp, &thd_err)
1276              && (thd_err == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES))
1277       {
1278          log_error(LOG_LEVEL_ERROR,
1279             "Timeout #%u while trying to resolve %s. Trying again.",
1280             dns_retries, host);
1281       }
1282 #elif defined(HAVE_GETHOSTBYNAME_R_5_ARGS)
1283       while (NULL == (hostp = gethostbyname_r(host, &result,
1284                                  hbuf, HOSTENT_BUFFER_SIZE, &thd_err))
1285              && (thd_err == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES))
1286       {
1287          log_error(LOG_LEVEL_ERROR,
1288             "Timeout #%u while trying to resolve %s. Trying again.",
1289             dns_retries, host);
1290       }
1291 #elif defined(HAVE_GETHOSTBYNAME_R_3_ARGS)
1292       /*
1293        * XXX: Doesn't retry in case of soft errors.
1294        * Does this gethostbyname_r version set h_errno?
1295        */
1296       if (0 == gethostbyname_r(host, &result, &hdata))
1297       {
1298          hostp = &result;
1299       }
1300       else
1301       {
1302          hostp = NULL;
1303       }
1304 #elif defined(MUTEX_LOCKS_AVAILABLE)
1305       privoxy_mutex_lock(&resolver_mutex);
1306       while (NULL == (hostp = gethostbyname(host))
1307              && (h_errno == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES))
1308       {
1309          log_error(LOG_LEVEL_ERROR,
1310             "Timeout #%u while trying to resolve %s. Trying again.",
1311             dns_retries, host);
1312       }
1313       privoxy_mutex_unlock(&resolver_mutex);
1314 #else
1315       while (NULL == (hostp = gethostbyname(host))
1316              && (h_errno == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES))
1317       {
1318          log_error(LOG_LEVEL_ERROR,
1319             "Timeout #%u while trying to resolve %s. Trying again.",
1320             dns_retries, host);
1321       }
1322 #endif /* def HAVE_GETHOSTBYNAME_R_(6|5|3)_ARGS */
1323       /*
1324        * On Mac OSX, if a domain exists but doesn't have a type A
1325        * record associated with it, the h_addr member of the struct
1326        * hostent returned by gethostbyname is NULL, even if h_length
1327        * is 4. Therefore the second test below.
1328        */
1329       if (hostp == NULL || hostp->h_addr == NULL)
1330       {
1331          errno = EINVAL;
1332          log_error(LOG_LEVEL_ERROR, "could not resolve hostname %s", host);
1333          return(INADDR_NONE);
1334       }
1335       if (hostp->h_addrtype != AF_INET)
1336       {
1337 #ifdef _WIN32
1338          errno = WSAEPROTOTYPE;
1339 #else
1340          errno = EPROTOTYPE;
1341 #endif
1342          log_error(LOG_LEVEL_ERROR, "hostname %s resolves to unknown address type.", host);
1343          return(INADDR_NONE);
1344       }
1345       memcpy((char *)&inaddr.sin_addr, (char *)hostp->h_addr, sizeof(inaddr.sin_addr));
1346    }
1347    return(inaddr.sin_addr.s_addr);
1348
1349 }
1350
1351
1352 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1353 /*********************************************************************
1354  *
1355  * Function    :  socket_is_still_alive
1356  *
1357  * Description :  Figures out whether or not a socket is still alive.
1358  *
1359  * Parameters  :
1360  *          1  :  sfd = The socket to check.
1361  *
1362  * Returns     :  TRUE for yes, otherwise FALSE.
1363  *
1364  *********************************************************************/
1365 int socket_is_still_alive(jb_socket sfd)
1366 {
1367    char buf[10];
1368    int no_data_waiting;
1369
1370 #ifdef HAVE_POLL
1371    int poll_result;
1372    struct pollfd poll_fd[1];
1373
1374    memset(poll_fd, 0, sizeof(poll_fd));
1375    poll_fd[0].fd = sfd;
1376    poll_fd[0].events = POLLIN;
1377
1378    poll_result = poll(poll_fd, 1, 0);
1379
1380    if (-1 == poll_result)
1381    {
1382       log_error(LOG_LEVEL_CONNECT, "Polling socket %d failed.", sfd);
1383       return FALSE;
1384    }
1385    no_data_waiting = !(poll_fd[0].revents & POLLIN);
1386 #else
1387    fd_set readable_fds;
1388    struct timeval timeout;
1389    int ret;
1390
1391    memset(&timeout, '\0', sizeof(timeout));
1392    FD_ZERO(&readable_fds);
1393    FD_SET(sfd, &readable_fds);
1394
1395    ret = select((int)sfd+1, &readable_fds, NULL, NULL, &timeout);
1396    if (ret < 0)
1397    {
1398       log_error(LOG_LEVEL_CONNECT, "select() on socket %d failed: %E", sfd);
1399       return FALSE;
1400    }
1401    no_data_waiting = !FD_ISSET(sfd, &readable_fds);
1402 #endif /* def HAVE_POLL */
1403
1404    return (no_data_waiting || (1 == recv(sfd, buf, 1, MSG_PEEK)));
1405 }
1406 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
1407
1408
1409 /*
1410   Local Variables:
1411   tab-width: 3
1412   end:
1413 */