1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.61 2009/05/28 21:13:34 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/jbsockets.c,v $
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
11 * Copyright : Written by and Copyright (C) 2001-2009 the
12 * Privoxy team. http://www.privoxy.org/
14 * Based on the Internet Junkbuster originally written
15 * by and Copyright (C) 1997 Anonymous Coders and
16 * Junkbusters Corporation. http://www.junkbusters.com
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.
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.
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.
36 *********************************************************************/
46 #include <sys/types.h>
54 #include <sys/timeb.h>
63 #include <netinet/in.h>
64 #include <sys/ioctl.h>
66 #include <sys/socket.h>
69 #include <netinet/tcp.h>
71 #include <arpa/inet.h>
77 #if defined(__EMX__) || defined (__OS2__)
78 #include <sys/select.h> /* OS/2/EMX needs a little help with select */
86 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
92 #endif /* def __GLIBC__ */
93 #endif /* HAVE_POLL */
94 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
98 /* For mutex semaphores only */
101 #include "jbsockets.h"
104 #include "miscutil.h"
106 /* Mac OSX doesn't define AI_NUMERICSESRV */
107 #ifndef AI_NUMERICSERV
108 #define AI_NUMERICSERV 0
111 const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
114 * Maximum number of gethostbyname(_r) retries in case of
115 * soft errors (TRY_AGAIN).
116 * XXX: Does it make sense to make this a config option?
118 #define MAX_DNS_RETRIES 10
120 #define MAX_LISTEN_BACKLOG 128
123 /*********************************************************************
125 * Function : connect_to
127 * Description : Open a socket and connect to it. Will check
128 * that this is allowed according to ACL.
131 * 1 : host = hostname to connect to
132 * 2 : portnum = port to connent on (XXX: should be unsigned)
133 * 3 : csp = Current client state (buffers, headers, etc...)
134 * Not modified, only used for source IP and ACL.
136 * Returns : JB_INVALID_SOCKET => failure, else it is the socket
139 *********************************************************************/
141 /* Getaddrinfo implementation */
142 jb_socket connect_to(const char *host, int portnum, struct client_state *csp)
144 struct addrinfo hints, *result, *rp;
149 struct timeval tv[1];
150 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
152 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
156 struct access_control_addr dst[1];
157 #endif /* def FEATURE_ACL */
159 retval = snprintf(service, sizeof(service), "%d", portnum);
160 if ((-1 == retval) || (sizeof(service) <= retval))
162 log_error(LOG_LEVEL_ERROR,
163 "Port number (%d) ASCII decimal representation doesn't fit into 6 bytes",
165 csp->http->host_ip_addr_str = strdup("unknown");
166 return(JB_INVALID_SOCKET);
169 memset((char *)&hints, 0, sizeof(hints));
170 hints.ai_family = AF_UNSPEC;
171 hints.ai_socktype = SOCK_STREAM;
172 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV; /* avoid service look-up */
173 if ((retval = getaddrinfo(host, service, &hints, &result)))
175 log_error(LOG_LEVEL_INFO,
176 "Can not resolve %s: %s", host, gai_strerror(retval));
177 csp->http->host_ip_addr_str = strdup("unknown");
178 return(JB_INVALID_SOCKET);
181 for (rp = result; rp != NULL; rp = rp->ai_next)
185 memcpy(&dst->addr, rp->ai_addr, rp->ai_addrlen);
187 if (block_acl(dst, csp))
196 #endif /* def FEATURE_ACL */
198 csp->http->host_ip_addr_str = malloc(NI_MAXHOST);
199 retval = getnameinfo(rp->ai_addr, rp->ai_addrlen,
200 csp->http->host_ip_addr_str, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
201 if (!csp->http->host_ip_addr_str || retval)
203 log_error(LOG_LEVEL_ERROR,
204 "Can not save csp->http->host_ip_addr_str: %s",
205 (csp->http->host_ip_addr_str) ?
206 gai_strerror(retval) : "Insufficient memory");
207 freez(csp->http->host_ip_addr_str);
212 if ((fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) ==
215 if ((fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) < 0)
222 { /* turn off TCP coalescence */
224 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
226 #endif /* def TCP_NODELAY */
228 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
229 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
232 fcntl(fd, F_SETFL, flags);
234 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
237 while (connect(fd, rp->ai_addr, rp->ai_addrlen) == JB_INVALID_SOCKET)
240 if (errno == WSAEINPROGRESS)
242 if (sock_errno() == EINPROGRESS)
243 #else /* ifndef _WIN32 */
244 if (errno == EINPROGRESS)
245 #endif /* ndef _WIN32 || __OS2__ */
251 if (sock_errno() != EINTR)
266 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
270 fcntl(fd, F_SETFL, flags);
272 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
274 /* wait for connection to complete */
281 /* MS Windows uses int, not SOCKET, for the 1st arg of select(). Wierd! */
282 if ((select((int)fd + 1, NULL, &wfds, NULL, tv) > 0)
283 && FD_ISSET(fd, &wfds))
286 * See Linux connect(2) man page for more info
287 * about connecting on non-blocking socket.
290 socklen_t optlen = sizeof(socket_in_error);
291 if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &socket_in_error, &optlen))
293 if (!socket_in_error)
295 /* Connection established, no need to try other addresses. */
298 log_error(LOG_LEVEL_CONNECT, "Could not connect to [%s]:%s: %s.",
299 csp->http->host_ip_addr_str, service, strerror(socket_in_error));
303 log_error(LOG_LEVEL_ERROR, "Could not get the state of "
304 "the connection to [%s]:%s: %s; dropping connection.",
305 csp->http->host_ip_addr_str, service, strerror(errno));
309 /* Connection failed, try next address */
313 freeaddrinfo(result);
316 log_error(LOG_LEVEL_CONNECT, "Could not connect to [%s]:%s.",
318 return(JB_INVALID_SOCKET);
320 log_error(LOG_LEVEL_CONNECT, "Connected to %s[%s]:%s.",
321 host, csp->http->host_ip_addr_str, service);
327 #else /* ndef HAVE_RFC2553 */
328 /* Pre-getaddrinfo implementation */
330 jb_socket connect_to(const char *host, int portnum, struct client_state *csp)
332 struct sockaddr_in inaddr;
336 struct timeval tv[1];
337 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
339 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
342 struct access_control_addr dst[1];
343 #endif /* def FEATURE_ACL */
345 memset((char *)&inaddr, 0, sizeof inaddr);
347 if ((addr = resolve_hostname_to_ip(host)) == INADDR_NONE)
349 csp->http->host_ip_addr_str = strdup("unknown");
350 return(JB_INVALID_SOCKET);
354 dst->addr = ntohl(addr);
357 if (block_acl(dst, csp))
364 return(JB_INVALID_SOCKET);
366 #endif /* def FEATURE_ACL */
368 inaddr.sin_addr.s_addr = addr;
369 inaddr.sin_family = AF_INET;
370 csp->http->host_ip_addr_str = strdup(inet_ntoa(inaddr.sin_addr));
373 if (sizeof(inaddr.sin_port) == sizeof(short))
374 #endif /* ndef _WIN32 */
376 inaddr.sin_port = htons((unsigned short) portnum);
381 inaddr.sin_port = htonl((unsigned long)portnum);
383 #endif /* ndef _WIN32 */
386 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) == JB_INVALID_SOCKET)
388 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
391 return(JB_INVALID_SOCKET);
395 { /* turn off TCP coalescence */
397 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
399 #endif /* def TCP_NODELAY */
401 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
402 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
405 fcntl(fd, F_SETFL, flags);
407 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
409 while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == JB_INVALID_SOCKET)
412 if (errno == WSAEINPROGRESS)
414 if (sock_errno() == EINPROGRESS)
415 #else /* ifndef _WIN32 */
416 if (errno == EINPROGRESS)
417 #endif /* ndef _WIN32 || __OS2__ */
423 if (sock_errno() != EINTR)
429 return(JB_INVALID_SOCKET);
433 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
437 fcntl(fd, F_SETFL, flags);
439 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
441 /* wait for connection to complete */
448 /* MS Windows uses int, not SOCKET, for the 1st arg of select(). Wierd! */
449 if (select((int)fd + 1, NULL, &wfds, NULL, tv) <= 0)
452 return(JB_INVALID_SOCKET);
457 #endif /* ndef HAVE_RFC2553 */
460 /*********************************************************************
462 * Function : write_socket
464 * Description : Write the contents of buf (for n bytes) to socket fd.
467 * 1 : fd = file descriptor (aka. handle) of socket to write to.
468 * 2 : buf = pointer to data to be written.
469 * 3 : len = length of data to be written to the socket "fd".
471 * Returns : 0 on success (entire buffer sent).
474 *********************************************************************/
476 int write_socket(jb_socket fd, const char *buf, ssize_t len)
478 int write_socket(jb_socket fd, const char *buf, size_t len)
486 if (len < 0) /* constant condition - size_t isn't ever negative */
491 log_error(LOG_LEVEL_LOG, "%N", len, buf);
494 return (send(fd, buf, (int)len, 0) != (int)len);
495 #elif defined(__BEOS__) || defined(AMIGA)
496 return (send(fd, buf, len, 0) != len);
497 #elif defined(__OS2__)
499 * Break the data up into SOCKET_SEND_MAX chunks for sending...
500 * OS/2 seemed to complain when the chunks were too large.
502 #define SOCKET_SEND_MAX 65000
504 int write_len = 0, send_len, send_rc = 0, i = 0;
505 while ((i < len) && (send_rc != -1))
507 if ((i + SOCKET_SEND_MAX) > len)
510 send_len = SOCKET_SEND_MAX;
511 send_rc = send(fd,(char*)buf + i, send_len, 0);
519 return (write(fd, buf, len) != len);
525 /*********************************************************************
527 * Function : read_socket
529 * Description : Read from a TCP/IP socket in a platform independent way.
532 * 1 : fd = file descriptor of the socket to read
533 * 2 : buf = pointer to buffer where data will be written
534 * Must be >= len bytes long.
535 * 3 : len = maximum number of bytes to read
537 * Returns : On success, the number of bytes read is returned (zero
538 * indicates end of file), and the file position is advanced
539 * by this number. It is not an error if this number is
540 * smaller than the number of bytes requested; this may hap-
541 * pen for example because fewer bytes are actually available
542 * right now (maybe because we were close to end-of-file, or
543 * because we are reading from a pipe, or from a terminal,
544 * or because read() was interrupted by a signal). On error,
545 * -1 is returned, and errno is set appropriately. In this
546 * case it is left unspecified whether the file position (if
549 *********************************************************************/
550 int read_socket(jb_socket fd, char *buf, int len)
558 return(recv(fd, buf, len, 0));
559 #elif defined(__BEOS__) || defined(AMIGA) || defined(__OS2__)
560 return(recv(fd, buf, (size_t)len, 0));
562 return(read(fd, buf, (size_t)len));
567 /*********************************************************************
569 * Function : data_is_available
571 * Description : Waits for data to arrive on a socket.
574 * 1 : fd = file descriptor of the socket to read
575 * 2 : seconds_to_wait = number of seconds after which we give up.
577 * Returns : TRUE if data arrived in time,
580 *********************************************************************/
581 int data_is_available(jb_socket fd, int seconds_to_wait)
584 struct timeval timeout;
587 memset(&timeout, 0, sizeof(timeout));
588 timeout.tv_sec = seconds_to_wait;
591 /* Copy and pasted from jcc.c ... */
592 memset(&rfds, 0, sizeof(fd_set));
598 n = select(fd+1, &rfds, NULL, NULL, &timeout);
601 * XXX: Do we care about the different error conditions?
607 /*********************************************************************
609 * Function : close_socket
611 * Description : Closes a TCP/IP socket
614 * 1 : fd = file descriptor of socket to be closed
618 *********************************************************************/
619 void close_socket(jb_socket fd)
621 #if defined(_WIN32) || defined(__BEOS__)
625 #elif defined(__OS2__)
634 /*********************************************************************
636 * Function : bind_port
638 * Description : Call socket, set socket options, and listen.
639 * Called by listen_loop to "boot up" our proxy address.
642 * 1 : hostnam = TCP/IP address to bind/listen to
643 * 2 : portnum = port to listen on
644 * 3 : pfd = pointer used to return file descriptor.
646 * Returns : if success, returns 0 and sets *pfd.
647 * if failure, returns -3 if address is in use,
648 * -2 if address unresolvable,
650 *********************************************************************/
651 int bind_port(const char *hostnam, int portnum, jb_socket *pfd)
654 struct addrinfo hints;
655 struct addrinfo *result, *rp;
657 * XXX: portnum should be a string to allow symbolic service
658 * names in the configuration file and to avoid the following
664 struct sockaddr_in inaddr;
665 #endif /* def HAVE_RFC2553 */
669 #endif /* ndef _WIN32 */
671 *pfd = JB_INVALID_SOCKET;
674 retval = snprintf(servnam, sizeof(servnam), "%d", portnum);
675 if ((-1 == retval) || (sizeof(servnam) <= retval))
677 log_error(LOG_LEVEL_ERROR,
678 "Port number (%d) ASCII decimal representation doesn't fit into 6 bytes",
683 memset(&hints, 0, sizeof(struct addrinfo));
684 if ((hostnam == NULL) || !strcmpic(hostnam, "localhost"))
687 * XXX: This is a hack. The right thing to do
688 * would be to bind to both AF_INET and AF_INET6.
689 * This will also fail if there is no AF_INET
692 hints.ai_family = AF_INET;
696 hints.ai_family = AF_UNSPEC;
698 hints.ai_socktype = SOCK_STREAM;
699 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
700 hints.ai_protocol = 0; /* Realy any stream protocol or TCP only */
701 hints.ai_canonname = NULL;
702 hints.ai_addr = NULL;
703 hints.ai_next = NULL;
705 if ((retval = getaddrinfo(hostnam, servnam, &hints, &result)))
707 log_error(LOG_LEVEL_ERROR,
708 "Can not resolve %s: %s", hostnam, gai_strerror(retval));
712 memset((char *)&inaddr, '\0', sizeof inaddr);
714 inaddr.sin_family = AF_INET;
715 inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
717 if (inaddr.sin_addr.s_addr == INADDR_NONE)
723 if (sizeof(inaddr.sin_port) == sizeof(short))
724 #endif /* ndef _WIN32 */
726 inaddr.sin_port = htons((unsigned short) portnum);
731 inaddr.sin_port = htonl((unsigned long) portnum);
733 #endif /* ndef _WIN32 */
734 #endif /* def HAVE_RFC2553 */
737 for (rp = result; rp != NULL; rp = rp->ai_next)
739 fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
741 fd = socket(AF_INET, SOCK_STREAM, 0);
742 #endif /* def HAVE_RFC2553 */
745 if (fd == JB_INVALID_SOCKET)
759 * This is not needed for Win32 - in fact, it stops
760 * duplicate instances of Privoxy from being caught.
762 * On UNIX, we assume the user is sensible enough not
763 * to start Privoxy multiple times on the same IP.
764 * Without this, stopping and restarting Privoxy
765 * from a script fails.
766 * Note: SO_REUSEADDR is meant to only take over
767 * sockets which are *not* in listen state in Linux,
768 * e.g. sockets in TIME_WAIT. YMMV.
770 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
771 #endif /* ndef _WIN32 */
774 if (bind(fd, rp->ai_addr, rp->ai_addrlen) < 0)
776 if (bind(fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
780 errno = WSAGetLastError();
781 if (errno == WSAEADDRINUSE)
783 if (errno == EADDRINUSE)
787 freeaddrinfo(result);
804 /* bind() succeeded, escape from for-loop */
806 * XXX: Support multiple listening sockets (e.g. localhost
807 * resolves to AF_INET and AF_INET6, but only the first address
814 freeaddrinfo(result);
817 /* All bind()s failed */
820 #endif /* ndef HAVE_RFC2553 */
822 while (listen(fd, MAX_LISTEN_BACKLOG) == -1)
836 /*********************************************************************
838 * Function : get_host_information
840 * Description : Determines the IP address the client used to
841 * reach us and the hostname associated with it.
843 * XXX: Most of the code has been copy and pasted
844 * from accept_connection() and not all of the
845 * ifdefs paths have been tested afterwards.
848 * 1 : afd = File descriptor returned from accept().
849 * 2 : ip_address = Pointer to return the pointer to
850 * the ip address string.
851 * 3 : hostname = Pointer to return the pointer to
852 * the hostname or NULL if the caller
853 * isn't interested in it.
857 *********************************************************************/
858 void get_host_information(jb_socket afd, char **ip_address, char **hostname)
861 struct sockaddr_storage server;
864 struct sockaddr_in server;
865 struct hostent *host = NULL;
866 #endif /* HAVE_RFC2553 */
867 #if defined(_WIN32) || defined(__OS2__) || defined(__APPLE_CC__) || defined(AMIGA)
868 /* according to accept_connection() this fixes a warning. */
869 int s_length, s_length_provided;
871 socklen_t s_length, s_length_provided;
874 #if defined(HAVE_GETHOSTBYADDR_R_8_ARGS) || defined(HAVE_GETHOSTBYADDR_R_7_ARGS) || defined(HAVE_GETHOSTBYADDR_R_5_ARGS)
875 struct hostent result;
876 #if defined(HAVE_GETHOSTBYADDR_R_5_ARGS)
877 struct hostent_data hdata;
879 char hbuf[HOSTENT_BUFFER_SIZE];
881 #endif /* def HAVE_GETHOSTBYADDR_R_5_ARGS */
882 #endif /* def HAVE_GETHOSTBYADDR_R_(8|7|5)_ARGS */
883 #endif /* ifndef HAVE_RFC2553 */
884 s_length = s_length_provided = sizeof(server);
886 if (NULL != hostname)
892 if (!getsockname(afd, (struct sockaddr *) &server, &s_length))
894 if (s_length > s_length_provided)
896 log_error(LOG_LEVEL_ERROR, "getsockname() truncated server address");
900 *ip_address = malloc(NI_MAXHOST);
901 retval = getnameinfo((struct sockaddr *) &server, s_length,
902 *ip_address, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
905 log_error(LOG_LEVEL_ERROR,
906 "Unable to print my own IP address: %s", gai_strerror(retval));
911 *ip_address = strdup(inet_ntoa(server.sin_addr));
912 #endif /* HAVE_RFC2553 */
913 if (NULL == hostname)
916 * We're done here, the caller isn't
917 * interested in knowing the hostname.
923 *hostname = malloc(NI_MAXHOST);
924 retval = getnameinfo((struct sockaddr *) &server, s_length,
925 *hostname, NI_MAXHOST, NULL, 0, NI_NAMEREQD);
928 log_error(LOG_LEVEL_ERROR,
929 "Unable to resolve my own IP address: %s", gai_strerror(retval));
933 #if defined(HAVE_GETHOSTBYADDR_R_8_ARGS)
934 gethostbyaddr_r((const char *)&server.sin_addr,
935 sizeof(server.sin_addr), AF_INET,
936 &result, hbuf, HOSTENT_BUFFER_SIZE,
938 #elif defined(HAVE_GETHOSTBYADDR_R_7_ARGS)
939 host = gethostbyaddr_r((const char *)&server.sin_addr,
940 sizeof(server.sin_addr), AF_INET,
941 &result, hbuf, HOSTENT_BUFFER_SIZE, &thd_err);
942 #elif defined(HAVE_GETHOSTBYADDR_R_5_ARGS)
943 if (0 == gethostbyaddr_r((const char *)&server.sin_addr,
944 sizeof(server.sin_addr), AF_INET,
953 #elif defined(MUTEX_LOCKS_AVAILABLE)
954 privoxy_mutex_lock(&resolver_mutex);
955 host = gethostbyaddr((const char *)&server.sin_addr,
956 sizeof(server.sin_addr), AF_INET);
957 privoxy_mutex_unlock(&resolver_mutex);
959 host = gethostbyaddr((const char *)&server.sin_addr,
960 sizeof(server.sin_addr), AF_INET);
964 log_error(LOG_LEVEL_ERROR, "Unable to get my own hostname: %E\n");
968 *hostname = strdup(host->h_name);
970 #endif /* else def HAVE_RFC2553 */
977 /*********************************************************************
979 * Function : accept_connection
981 * Description : Accepts a connection on a socket. Socket must have
982 * been created using bind_port().
985 * 1 : csp = Client state, cfd, ip_addr_str, and
986 * ip_addr_long will be set by this routine.
987 * 2 : fd = file descriptor returned from bind_port
989 * Returns : when a connection is accepted, it returns 1 (TRUE).
990 * On an error it returns 0 (FALSE).
992 *********************************************************************/
993 int accept_connection(struct client_state * csp, jb_socket fd)
996 /* XXX: client is stored directly into csp->tcp_addr */
997 #define client (csp->tcp_addr)
1000 struct sockaddr_in client;
1003 #if defined(_WIN32) || defined(__OS2__) || defined(__APPLE_CC__) || defined(AMIGA)
1004 /* Wierdness - fix a warning. */
1010 c_length = sizeof(client);
1013 afd = accept (fd, (struct sockaddr *) &client, &c_length);
1014 if (afd == JB_INVALID_SOCKET)
1021 afd = accept (fd, (struct sockaddr *) &client, &c_length);
1022 } while (afd < 1 && errno == EINTR);
1031 csp->ip_addr_str = malloc(NI_MAXHOST);
1032 retval = getnameinfo((struct sockaddr *) &client, c_length,
1033 csp->ip_addr_str, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
1034 if (!csp->ip_addr_str || retval)
1036 log_error(LOG_LEVEL_ERROR, "Can not save csp->ip_addr_str: %s",
1037 (csp->ip_addr_str) ? gai_strerror(retval) : "Insuffcient memory");
1038 freez(csp->ip_addr_str);
1042 csp->ip_addr_str = strdup(inet_ntoa(client.sin_addr));
1043 csp->ip_addr_long = ntohl(client.sin_addr.s_addr);
1044 #endif /* def HAVE_RFC2553 */
1051 /*********************************************************************
1053 * Function : resolve_hostname_to_ip
1055 * Description : Resolve a hostname to an internet tcp/ip address.
1056 * NULL or an empty string resolve to INADDR_ANY.
1059 * 1 : host = hostname to resolve
1061 * Returns : INADDR_NONE => failure, INADDR_ANY or tcp/ip address if succesful.
1063 *********************************************************************/
1064 unsigned long resolve_hostname_to_ip(const char *host)
1066 struct sockaddr_in inaddr;
1067 struct hostent *hostp;
1068 unsigned int dns_retries = 0;
1069 #if defined(HAVE_GETHOSTBYNAME_R_6_ARGS) || defined(HAVE_GETHOSTBYNAME_R_5_ARGS) || defined(HAVE_GETHOSTBYNAME_R_3_ARGS)
1070 struct hostent result;
1071 #if defined(HAVE_GETHOSTBYNAME_R_6_ARGS) || defined(HAVE_GETHOSTBYNAME_R_5_ARGS)
1072 char hbuf[HOSTENT_BUFFER_SIZE];
1074 #else /* defined(HAVE_GETHOSTBYNAME_R_3_ARGS) */
1075 struct hostent_data hdata;
1076 #endif /* def HAVE_GETHOSTBYNAME_R_(6|5)_ARGS */
1077 #endif /* def HAVE_GETHOSTBYNAME_R_(6|5|3)_ARGS */
1079 if ((host == NULL) || (*host == '\0'))
1084 memset((char *) &inaddr, 0, sizeof inaddr);
1086 if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
1088 #if defined(HAVE_GETHOSTBYNAME_R_6_ARGS)
1089 while (gethostbyname_r(host, &result, hbuf,
1090 HOSTENT_BUFFER_SIZE, &hostp, &thd_err)
1091 && (thd_err == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES))
1093 log_error(LOG_LEVEL_ERROR,
1094 "Timeout #%u while trying to resolve %s. Trying again.",
1097 #elif defined(HAVE_GETHOSTBYNAME_R_5_ARGS)
1098 while (NULL == (hostp = gethostbyname_r(host, &result,
1099 hbuf, HOSTENT_BUFFER_SIZE, &thd_err))
1100 && (thd_err == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES))
1102 log_error(LOG_LEVEL_ERROR,
1103 "Timeout #%u while trying to resolve %s. Trying again.",
1106 #elif defined(HAVE_GETHOSTBYNAME_R_3_ARGS)
1108 * XXX: Doesn't retry in case of soft errors.
1109 * Does this gethostbyname_r version set h_errno?
1111 if (0 == gethostbyname_r(host, &result, &hdata))
1119 #elif defined(MUTEX_LOCKS_AVAILABLE)
1120 privoxy_mutex_lock(&resolver_mutex);
1121 while (NULL == (hostp = gethostbyname(host))
1122 && (h_errno == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES))
1124 log_error(LOG_LEVEL_ERROR,
1125 "Timeout #%u while trying to resolve %s. Trying again.",
1128 privoxy_mutex_unlock(&resolver_mutex);
1130 while (NULL == (hostp = gethostbyname(host))
1131 && (h_errno == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES))
1133 log_error(LOG_LEVEL_ERROR,
1134 "Timeout #%u while trying to resolve %s. Trying again.",
1137 #endif /* def HAVE_GETHOSTBYNAME_R_(6|5|3)_ARGS */
1139 * On Mac OSX, if a domain exists but doesn't have a type A
1140 * record associated with it, the h_addr member of the struct
1141 * hostent returned by gethostbyname is NULL, even if h_length
1142 * is 4. Therefore the second test below.
1144 if (hostp == NULL || hostp->h_addr == NULL)
1147 log_error(LOG_LEVEL_ERROR, "could not resolve hostname %s", host);
1148 return(INADDR_NONE);
1150 if (hostp->h_addrtype != AF_INET)
1153 errno = WSAEPROTOTYPE;
1157 log_error(LOG_LEVEL_ERROR, "hostname %s resolves to unknown address type.", host);
1158 return(INADDR_NONE);
1161 (char *) &inaddr.sin_addr,
1162 (char *) hostp->h_addr,
1163 sizeof(inaddr.sin_addr)
1166 return(inaddr.sin_addr.s_addr);
1171 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1172 /*********************************************************************
1174 * Function : socket_is_still_usable
1176 * Description : Decides whether or not an open socket is still usable.
1179 * 1 : sfd = The socket to check.
1181 * Returns : TRUE for yes, otherwise FALSE.
1183 *********************************************************************/
1184 int socket_is_still_usable(jb_socket sfd)
1188 struct pollfd poll_fd[1];
1190 memset(poll_fd, 0, sizeof(poll_fd));
1191 poll_fd[0].fd = sfd;
1192 poll_fd[0].events = POLLIN;
1194 poll_result = poll(poll_fd, 1, 0);
1196 if (-1 != poll_result)
1198 return !(poll_fd[0].revents & POLLIN);
1202 log_error(LOG_LEVEL_CONNECT, "Polling socket %d failed.", sfd);
1206 fd_set readable_fds;
1207 struct timeval timeout;
1209 int socket_is_alive = 0;
1211 memset(&timeout, '\0', sizeof(timeout));
1212 FD_ZERO(&readable_fds);
1213 FD_SET(sfd, &readable_fds);
1215 ret = select((int)sfd+1, &readable_fds, NULL, NULL, &timeout);
1218 log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
1222 * XXX: I'm not sure why !FD_ISSET() works,
1223 * but apparently it does.
1225 socket_is_alive = !FD_ISSET(sfd, &readable_fds);
1227 return socket_is_alive;
1228 #endif /* def HAVE_POLL */
1230 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */