1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.59 2009/05/25 15:43: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 const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
109 * Maximum number of gethostbyname(_r) retries in case of
110 * soft errors (TRY_AGAIN).
111 * XXX: Does it make sense to make this a config option?
113 #define MAX_DNS_RETRIES 10
115 #define MAX_LISTEN_BACKLOG 128
118 /*********************************************************************
120 * Function : connect_to
122 * Description : Open a socket and connect to it. Will check
123 * that this is allowed according to ACL.
126 * 1 : host = hostname to connect to
127 * 2 : portnum = port to connent on (XXX: should be unsigned)
128 * 3 : csp = Current client state (buffers, headers, etc...)
129 * Not modified, only used for source IP and ACL.
131 * Returns : JB_INVALID_SOCKET => failure, else it is the socket
134 *********************************************************************/
136 /* Getaddrinfo implementation */
137 jb_socket connect_to(const char *host, int portnum, struct client_state *csp)
139 struct addrinfo hints, *result, *rp;
144 struct timeval tv[1];
145 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
147 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
151 struct access_control_addr dst[1];
152 #endif /* def FEATURE_ACL */
154 retval = snprintf(service, sizeof(service), "%d", portnum);
155 if ((-1 == retval) || (sizeof(service) <= retval))
157 log_error(LOG_LEVEL_ERROR,
158 "Port number (%d) ASCII decimal representation doesn't fit into 6 bytes",
160 csp->http->host_ip_addr_str = strdup("unknown");
161 return(JB_INVALID_SOCKET);
164 memset((char *)&hints, 0, sizeof(hints));
165 hints.ai_family = AF_UNSPEC;
166 hints.ai_socktype = SOCK_STREAM;
167 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV; /* avoid service look-up */
168 if ((retval = getaddrinfo(host, service, &hints, &result)))
170 log_error(LOG_LEVEL_INFO,
171 "Can not resolve %s: %s", host, gai_strerror(retval));
172 csp->http->host_ip_addr_str = strdup("unknown");
173 return(JB_INVALID_SOCKET);
176 for (rp = result; rp != NULL; rp = rp->ai_next)
180 memcpy(&dst->addr, rp->ai_addr, rp->ai_addrlen);
182 if (block_acl(dst, csp))
191 #endif /* def FEATURE_ACL */
193 csp->http->host_ip_addr_str = malloc(NI_MAXHOST);
194 retval = getnameinfo(rp->ai_addr, rp->ai_addrlen,
195 csp->http->host_ip_addr_str, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
196 if (!csp->http->host_ip_addr_str || retval)
198 log_error(LOG_LEVEL_ERROR,
199 "Can not save csp->http->host_ip_addr_str: %s",
200 (csp->http->host_ip_addr_str) ?
201 gai_strerror(retval) : "Insufficient memory");
202 freez(csp->http->host_ip_addr_str);
207 if ((fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) ==
210 if ((fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) < 0)
217 { /* turn off TCP coalescence */
219 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
221 #endif /* def TCP_NODELAY */
223 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
224 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
227 fcntl(fd, F_SETFL, flags);
229 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
232 while (connect(fd, rp->ai_addr, rp->ai_addrlen) == JB_INVALID_SOCKET)
235 if (errno == WSAEINPROGRESS)
237 if (sock_errno() == EINPROGRESS)
238 #else /* ifndef _WIN32 */
239 if (errno == EINPROGRESS)
240 #endif /* ndef _WIN32 || __OS2__ */
246 if (sock_errno() != EINTR)
261 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
265 fcntl(fd, F_SETFL, flags);
267 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
269 /* wait for connection to complete */
276 /* MS Windows uses int, not SOCKET, for the 1st arg of select(). Wierd! */
277 if ((select((int)fd + 1, NULL, &wfds, NULL, tv) > 0)
278 && FD_ISSET(fd, &wfds))
281 * See Linux connect(2) man page for more info
282 * about connecting on non-blocking socket.
285 socklen_t optlen = sizeof(socket_in_error);
286 if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &socket_in_error, &optlen))
288 if (!socket_in_error)
290 /* Connection established, no need to try other addresses. */
293 log_error(LOG_LEVEL_CONNECT, "Could not connect to [%s]:%s: %s.",
294 csp->http->host_ip_addr_str, service, strerror(socket_in_error));
298 log_error(LOG_LEVEL_ERROR, "Could not get the state of "
299 "the connection to [%s]:%s: %s; dropping connection.",
300 csp->http->host_ip_addr_str, service, strerror(errno));
304 /* Connection failed, try next address */
308 freeaddrinfo(result);
311 log_error(LOG_LEVEL_CONNECT, "Could not connect to [%s]:%s.",
313 return(JB_INVALID_SOCKET);
315 log_error(LOG_LEVEL_CONNECT, "Connected to %s[%s]:%s.",
316 host, csp->http->host_ip_addr_str, service);
322 #else /* ndef HAVE_RFC2553 */
323 /* Pre-getaddrinfo implementation */
325 jb_socket connect_to(const char *host, int portnum, struct client_state *csp)
327 struct sockaddr_in inaddr;
331 struct timeval tv[1];
332 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
334 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
337 struct access_control_addr dst[1];
338 #endif /* def FEATURE_ACL */
340 memset((char *)&inaddr, 0, sizeof inaddr);
342 if ((addr = resolve_hostname_to_ip(host)) == INADDR_NONE)
344 csp->http->host_ip_addr_str = strdup("unknown");
345 return(JB_INVALID_SOCKET);
349 dst->addr = ntohl(addr);
352 if (block_acl(dst, csp))
359 return(JB_INVALID_SOCKET);
361 #endif /* def FEATURE_ACL */
363 inaddr.sin_addr.s_addr = addr;
364 inaddr.sin_family = AF_INET;
365 csp->http->host_ip_addr_str = strdup(inet_ntoa(inaddr.sin_addr));
368 if (sizeof(inaddr.sin_port) == sizeof(short))
369 #endif /* ndef _WIN32 */
371 inaddr.sin_port = htons((unsigned short) portnum);
376 inaddr.sin_port = htonl((unsigned long)portnum);
378 #endif /* ndef _WIN32 */
381 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) == JB_INVALID_SOCKET)
383 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
386 return(JB_INVALID_SOCKET);
390 { /* turn off TCP coalescence */
392 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
394 #endif /* def TCP_NODELAY */
396 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
397 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
400 fcntl(fd, F_SETFL, flags);
402 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
404 while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == JB_INVALID_SOCKET)
407 if (errno == WSAEINPROGRESS)
409 if (sock_errno() == EINPROGRESS)
410 #else /* ifndef _WIN32 */
411 if (errno == EINPROGRESS)
412 #endif /* ndef _WIN32 || __OS2__ */
418 if (sock_errno() != EINTR)
424 return(JB_INVALID_SOCKET);
428 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
432 fcntl(fd, F_SETFL, flags);
434 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
436 /* wait for connection to complete */
443 /* MS Windows uses int, not SOCKET, for the 1st arg of select(). Wierd! */
444 if (select((int)fd + 1, NULL, &wfds, NULL, tv) <= 0)
447 return(JB_INVALID_SOCKET);
452 #endif /* ndef HAVE_RFC2553 */
455 /*********************************************************************
457 * Function : write_socket
459 * Description : Write the contents of buf (for n bytes) to socket fd.
462 * 1 : fd = file descriptor (aka. handle) of socket to write to.
463 * 2 : buf = pointer to data to be written.
464 * 3 : len = length of data to be written to the socket "fd".
466 * Returns : 0 on success (entire buffer sent).
469 *********************************************************************/
471 int write_socket(jb_socket fd, const char *buf, ssize_t len)
473 int write_socket(jb_socket fd, const char *buf, size_t len)
481 if (len < 0) /* constant condition - size_t isn't ever negative */
486 log_error(LOG_LEVEL_LOG, "%N", len, buf);
489 return (send(fd, buf, (int)len, 0) != (int)len);
490 #elif defined(__BEOS__) || defined(AMIGA)
491 return (send(fd, buf, len, 0) != len);
492 #elif defined(__OS2__)
494 * Break the data up into SOCKET_SEND_MAX chunks for sending...
495 * OS/2 seemed to complain when the chunks were too large.
497 #define SOCKET_SEND_MAX 65000
499 int write_len = 0, send_len, send_rc = 0, i = 0;
500 while ((i < len) && (send_rc != -1))
502 if ((i + SOCKET_SEND_MAX) > len)
505 send_len = SOCKET_SEND_MAX;
506 send_rc = send(fd,(char*)buf + i, send_len, 0);
514 return (write(fd, buf, len) != len);
520 /*********************************************************************
522 * Function : read_socket
524 * Description : Read from a TCP/IP socket in a platform independent way.
527 * 1 : fd = file descriptor of the socket to read
528 * 2 : buf = pointer to buffer where data will be written
529 * Must be >= len bytes long.
530 * 3 : len = maximum number of bytes to read
532 * Returns : On success, the number of bytes read is returned (zero
533 * indicates end of file), and the file position is advanced
534 * by this number. It is not an error if this number is
535 * smaller than the number of bytes requested; this may hap-
536 * pen for example because fewer bytes are actually available
537 * right now (maybe because we were close to end-of-file, or
538 * because we are reading from a pipe, or from a terminal,
539 * or because read() was interrupted by a signal). On error,
540 * -1 is returned, and errno is set appropriately. In this
541 * case it is left unspecified whether the file position (if
544 *********************************************************************/
545 int read_socket(jb_socket fd, char *buf, int len)
553 return(recv(fd, buf, len, 0));
554 #elif defined(__BEOS__) || defined(AMIGA) || defined(__OS2__)
555 return(recv(fd, buf, (size_t)len, 0));
557 return(read(fd, buf, (size_t)len));
562 /*********************************************************************
564 * Function : data_is_available
566 * Description : Waits for data to arrive on a socket.
569 * 1 : fd = file descriptor of the socket to read
570 * 2 : seconds_to_wait = number of seconds after which we give up.
572 * Returns : TRUE if data arrived in time,
575 *********************************************************************/
576 int data_is_available(jb_socket fd, int seconds_to_wait)
579 struct timeval timeout;
582 memset(&timeout, 0, sizeof(timeout));
583 timeout.tv_sec = seconds_to_wait;
586 /* Copy and pasted from jcc.c ... */
587 memset(&rfds, 0, sizeof(fd_set));
593 n = select(fd+1, &rfds, NULL, NULL, &timeout);
596 * XXX: Do we care about the different error conditions?
602 /*********************************************************************
604 * Function : close_socket
606 * Description : Closes a TCP/IP socket
609 * 1 : fd = file descriptor of socket to be closed
613 *********************************************************************/
614 void close_socket(jb_socket fd)
616 #if defined(_WIN32) || defined(__BEOS__)
620 #elif defined(__OS2__)
629 /*********************************************************************
631 * Function : bind_port
633 * Description : Call socket, set socket options, and listen.
634 * Called by listen_loop to "boot up" our proxy address.
637 * 1 : hostnam = TCP/IP address to bind/listen to
638 * 2 : portnum = port to listen on
639 * 3 : pfd = pointer used to return file descriptor.
641 * Returns : if success, returns 0 and sets *pfd.
642 * if failure, returns -3 if address is in use,
643 * -2 if address unresolvable,
645 *********************************************************************/
646 int bind_port(const char *hostnam, int portnum, jb_socket *pfd)
649 struct addrinfo hints;
650 struct addrinfo *result, *rp;
652 * XXX: portnum should be a string to allow symbolic service
653 * names in the configuration file and to avoid the following
659 struct sockaddr_in inaddr;
660 #endif /* def HAVE_RFC2553 */
664 #endif /* ndef _WIN32 */
666 *pfd = JB_INVALID_SOCKET;
669 retval = snprintf(servnam, sizeof(servnam), "%d", portnum);
670 if ((-1 == retval) || (sizeof(servnam) <= retval))
672 log_error(LOG_LEVEL_ERROR,
673 "Port number (%d) ASCII decimal representation doesn't fit into 6 bytes",
678 memset(&hints, 0, sizeof(struct addrinfo));
679 if ((hostnam == NULL) || !strcmpic(hostnam, "localhost"))
682 * XXX: This is a hack. The right thing to do
683 * would be to bind to both AF_INET and AF_INET6.
684 * This will also fail if there is no AF_INET
687 hints.ai_family = AF_INET;
691 hints.ai_family = AF_UNSPEC;
693 hints.ai_socktype = SOCK_STREAM;
694 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
695 hints.ai_protocol = 0; /* Realy any stream protocol or TCP only */
696 hints.ai_canonname = NULL;
697 hints.ai_addr = NULL;
698 hints.ai_next = NULL;
700 if ((retval = getaddrinfo(hostnam, servnam, &hints, &result)))
702 log_error(LOG_LEVEL_ERROR,
703 "Can not resolve %s: %s", hostnam, gai_strerror(retval));
707 memset((char *)&inaddr, '\0', sizeof inaddr);
709 inaddr.sin_family = AF_INET;
710 inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
712 if (inaddr.sin_addr.s_addr == INADDR_NONE)
718 if (sizeof(inaddr.sin_port) == sizeof(short))
719 #endif /* ndef _WIN32 */
721 inaddr.sin_port = htons((unsigned short) portnum);
726 inaddr.sin_port = htonl((unsigned long) portnum);
728 #endif /* ndef _WIN32 */
729 #endif /* def HAVE_RFC2553 */
732 for (rp = result; rp != NULL; rp = rp->ai_next)
734 fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
736 fd = socket(AF_INET, SOCK_STREAM, 0);
737 #endif /* def HAVE_RFC2553 */
740 if (fd == JB_INVALID_SOCKET)
754 * This is not needed for Win32 - in fact, it stops
755 * duplicate instances of Privoxy from being caught.
757 * On UNIX, we assume the user is sensible enough not
758 * to start Privoxy multiple times on the same IP.
759 * Without this, stopping and restarting Privoxy
760 * from a script fails.
761 * Note: SO_REUSEADDR is meant to only take over
762 * sockets which are *not* in listen state in Linux,
763 * e.g. sockets in TIME_WAIT. YMMV.
765 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
766 #endif /* ndef _WIN32 */
769 if (bind(fd, rp->ai_addr, rp->ai_addrlen) < 0)
771 if (bind(fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
775 errno = WSAGetLastError();
776 if (errno == WSAEADDRINUSE)
778 if (errno == EADDRINUSE)
782 freeaddrinfo(result);
799 /* bind() succeeded, escape from for-loop */
801 * XXX: Support multiple listening sockets (e.g. localhost
802 * resolves to AF_INET and AF_INET6, but only the first address
809 freeaddrinfo(result);
812 /* All bind()s failed */
815 #endif /* ndef HAVE_RFC2553 */
817 while (listen(fd, MAX_LISTEN_BACKLOG) == -1)
831 /*********************************************************************
833 * Function : get_host_information
835 * Description : Determines the IP address the client used to
836 * reach us and the hostname associated with it.
838 * XXX: Most of the code has been copy and pasted
839 * from accept_connection() and not all of the
840 * ifdefs paths have been tested afterwards.
843 * 1 : afd = File descriptor returned from accept().
844 * 2 : ip_address = Pointer to return the pointer to
845 * the ip address string.
846 * 3 : hostname = Pointer to return the pointer to
847 * the hostname or NULL if the caller
848 * isn't interested in it.
852 *********************************************************************/
853 void get_host_information(jb_socket afd, char **ip_address, char **hostname)
856 struct sockaddr_storage server;
859 struct sockaddr_in server;
860 struct hostent *host = NULL;
861 #endif /* HAVE_RFC2553 */
862 #if defined(_WIN32) || defined(__OS2__) || defined(__APPLE_CC__) || defined(AMIGA)
863 /* according to accept_connection() this fixes a warning. */
864 int s_length, s_length_provided;
866 socklen_t s_length, s_length_provided;
869 #if defined(HAVE_GETHOSTBYADDR_R_8_ARGS) || defined(HAVE_GETHOSTBYADDR_R_7_ARGS) || defined(HAVE_GETHOSTBYADDR_R_5_ARGS)
870 struct hostent result;
871 #if defined(HAVE_GETHOSTBYADDR_R_5_ARGS)
872 struct hostent_data hdata;
874 char hbuf[HOSTENT_BUFFER_SIZE];
876 #endif /* def HAVE_GETHOSTBYADDR_R_5_ARGS */
877 #endif /* def HAVE_GETHOSTBYADDR_R_(8|7|5)_ARGS */
878 #endif /* ifndef HAVE_RFC2553 */
879 s_length = s_length_provided = sizeof(server);
881 if (NULL != hostname)
887 if (!getsockname(afd, (struct sockaddr *) &server, &s_length))
889 if (s_length > s_length_provided)
891 log_error(LOG_LEVEL_ERROR, "getsockname() truncated server address");
895 *ip_address = malloc(NI_MAXHOST);
896 retval = getnameinfo((struct sockaddr *) &server, s_length,
897 *ip_address, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
900 log_error(LOG_LEVEL_ERROR,
901 "Unable to print my own IP address: %s", gai_strerror(retval));
906 *ip_address = strdup(inet_ntoa(server.sin_addr));
907 #endif /* HAVE_RFC2553 */
908 if (NULL == hostname)
911 * We're done here, the caller isn't
912 * interested in knowing the hostname.
918 *hostname = malloc(NI_MAXHOST);
919 retval = getnameinfo((struct sockaddr *) &server, s_length,
920 *hostname, NI_MAXHOST, NULL, 0, NI_NAMEREQD);
923 log_error(LOG_LEVEL_ERROR,
924 "Unable to resolve my own IP address: %s", gai_strerror(retval));
928 #if defined(HAVE_GETHOSTBYADDR_R_8_ARGS)
929 gethostbyaddr_r((const char *)&server.sin_addr,
930 sizeof(server.sin_addr), AF_INET,
931 &result, hbuf, HOSTENT_BUFFER_SIZE,
933 #elif defined(HAVE_GETHOSTBYADDR_R_7_ARGS)
934 host = gethostbyaddr_r((const char *)&server.sin_addr,
935 sizeof(server.sin_addr), AF_INET,
936 &result, hbuf, HOSTENT_BUFFER_SIZE, &thd_err);
937 #elif defined(HAVE_GETHOSTBYADDR_R_5_ARGS)
938 if (0 == gethostbyaddr_r((const char *)&server.sin_addr,
939 sizeof(server.sin_addr), AF_INET,
948 #elif def MUTEX_LOCKS_AVAILABLE
949 privoxy_mutex_lock(&resolver_mutex);
950 host = gethostbyaddr((const char *)&server.sin_addr,
951 sizeof(server.sin_addr), AF_INET);
952 privoxy_mutex_unlock(&resolver_mutex);
954 host = gethostbyaddr((const char *)&server.sin_addr,
955 sizeof(server.sin_addr), AF_INET);
959 log_error(LOG_LEVEL_ERROR, "Unable to get my own hostname: %E\n");
963 *hostname = strdup(host->h_name);
965 #endif /* else def HAVE_RFC2553 */
972 /*********************************************************************
974 * Function : accept_connection
976 * Description : Accepts a connection on a socket. Socket must have
977 * been created using bind_port().
980 * 1 : csp = Client state, cfd, ip_addr_str, and
981 * ip_addr_long will be set by this routine.
982 * 2 : fd = file descriptor returned from bind_port
984 * Returns : when a connection is accepted, it returns 1 (TRUE).
985 * On an error it returns 0 (FALSE).
987 *********************************************************************/
988 int accept_connection(struct client_state * csp, jb_socket fd)
991 /* XXX: client is stored directly into csp->tcp_addr */
992 #define client (csp->tcp_addr)
995 struct sockaddr_in client;
998 #if defined(_WIN32) || defined(__OS2__) || defined(__APPLE_CC__) || defined(AMIGA)
999 /* Wierdness - fix a warning. */
1005 c_length = sizeof(client);
1008 afd = accept (fd, (struct sockaddr *) &client, &c_length);
1009 if (afd == JB_INVALID_SOCKET)
1016 afd = accept (fd, (struct sockaddr *) &client, &c_length);
1017 } while (afd < 1 && errno == EINTR);
1026 csp->ip_addr_str = malloc(NI_MAXHOST);
1027 retval = getnameinfo((struct sockaddr *) &client, c_length,
1028 csp->ip_addr_str, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
1029 if (!csp->ip_addr_str || retval)
1031 log_error(LOG_LEVEL_ERROR, "Can not save csp->ip_addr_str: %s",
1032 (csp->ip_addr_str) ? gai_strerror(retval) : "Insuffcient memory");
1033 freez(csp->ip_addr_str);
1037 csp->ip_addr_str = strdup(inet_ntoa(client.sin_addr));
1038 csp->ip_addr_long = ntohl(client.sin_addr.s_addr);
1039 #endif /* def HAVE_RFC2553 */
1046 /*********************************************************************
1048 * Function : resolve_hostname_to_ip
1050 * Description : Resolve a hostname to an internet tcp/ip address.
1051 * NULL or an empty string resolve to INADDR_ANY.
1054 * 1 : host = hostname to resolve
1056 * Returns : INADDR_NONE => failure, INADDR_ANY or tcp/ip address if succesful.
1058 *********************************************************************/
1059 unsigned long resolve_hostname_to_ip(const char *host)
1061 struct sockaddr_in inaddr;
1062 struct hostent *hostp;
1063 unsigned int dns_retries = 0;
1064 #if defined(HAVE_GETHOSTBYNAME_R_6_ARGS) || defined(HAVE_GETHOSTBYNAME_R_5_ARGS) || defined(HAVE_GETHOSTBYNAME_R_3_ARGS)
1065 struct hostent result;
1066 #if defined(HAVE_GETHOSTBYNAME_R_6_ARGS) || defined(HAVE_GETHOSTBYNAME_R_5_ARGS)
1067 char hbuf[HOSTENT_BUFFER_SIZE];
1069 #else /* defined(HAVE_GETHOSTBYNAME_R_3_ARGS) */
1070 struct hostent_data hdata;
1071 #endif /* def HAVE_GETHOSTBYNAME_R_(6|5)_ARGS */
1072 #endif /* def HAVE_GETHOSTBYNAME_R_(6|5|3)_ARGS */
1074 if ((host == NULL) || (*host == '\0'))
1079 memset((char *) &inaddr, 0, sizeof inaddr);
1081 if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
1083 #if defined(HAVE_GETHOSTBYNAME_R_6_ARGS)
1084 while (gethostbyname_r(host, &result, hbuf,
1085 HOSTENT_BUFFER_SIZE, &hostp, &thd_err)
1086 && (thd_err == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES))
1088 log_error(LOG_LEVEL_ERROR,
1089 "Timeout #%u while trying to resolve %s. Trying again.",
1092 #elif defined(HAVE_GETHOSTBYNAME_R_5_ARGS)
1093 while (NULL == (hostp = gethostbyname_r(host, &result,
1094 hbuf, HOSTENT_BUFFER_SIZE, &thd_err))
1095 && (thd_err == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES))
1097 log_error(LOG_LEVEL_ERROR,
1098 "Timeout #%u while trying to resolve %s. Trying again.",
1101 #elif defined(HAVE_GETHOSTBYNAME_R_3_ARGS)
1103 * XXX: Doesn't retry in case of soft errors.
1104 * Does this gethostbyname_r version set h_errno?
1106 if (0 == gethostbyname_r(host, &result, &hdata))
1114 #elif def MUTEX_LOCKS_AVAILABLE
1115 privoxy_mutex_lock(&resolver_mutex);
1116 while (NULL == (hostp = gethostbyname(host))
1117 && (h_errno == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES))
1119 log_error(LOG_LEVEL_ERROR,
1120 "Timeout #%u while trying to resolve %s. Trying again.",
1123 privoxy_mutex_unlock(&resolver_mutex);
1125 while (NULL == (hostp = gethostbyname(host))
1126 && (h_errno == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES))
1128 log_error(LOG_LEVEL_ERROR,
1129 "Timeout #%u while trying to resolve %s. Trying again.",
1132 #endif /* def HAVE_GETHOSTBYNAME_R_(6|5|3)_ARGS */
1134 * On Mac OSX, if a domain exists but doesn't have a type A
1135 * record associated with it, the h_addr member of the struct
1136 * hostent returned by gethostbyname is NULL, even if h_length
1137 * is 4. Therefore the second test below.
1139 if (hostp == NULL || hostp->h_addr == NULL)
1142 log_error(LOG_LEVEL_ERROR, "could not resolve hostname %s", host);
1143 return(INADDR_NONE);
1145 if (hostp->h_addrtype != AF_INET)
1148 errno = WSAEPROTOTYPE;
1152 log_error(LOG_LEVEL_ERROR, "hostname %s resolves to unknown address type.", host);
1153 return(INADDR_NONE);
1156 (char *) &inaddr.sin_addr,
1157 (char *) hostp->h_addr,
1158 sizeof(inaddr.sin_addr)
1161 return(inaddr.sin_addr.s_addr);
1166 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1167 /*********************************************************************
1169 * Function : socket_is_still_usable
1171 * Description : Decides whether or not an open socket is still usable.
1174 * 1 : sfd = The socket to check.
1176 * Returns : TRUE for yes, otherwise FALSE.
1178 *********************************************************************/
1179 int socket_is_still_usable(jb_socket sfd)
1183 struct pollfd poll_fd[1];
1185 memset(poll_fd, 0, sizeof(poll_fd));
1186 poll_fd[0].fd = sfd;
1187 poll_fd[0].events = POLLIN;
1189 poll_result = poll(poll_fd, 1, 0);
1191 if (-1 != poll_result)
1193 return !(poll_fd[0].revents & POLLIN);
1197 log_error(LOG_LEVEL_CONNECT, "Polling socket %d failed.", sfd);
1201 fd_set readable_fds;
1202 struct timeval timeout;
1204 int socket_is_alive = 0;
1206 memset(&timeout, '\0', sizeof(timeout));
1207 FD_ZERO(&readable_fds);
1208 FD_SET(sfd, &readable_fds);
1210 ret = select((int)sfd+1, &readable_fds, NULL, NULL, &timeout);
1213 log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
1217 * XXX: I'm not sure why !FD_ISSET() works,
1218 * but apparently it does.
1220 socket_is_alive = !FD_ISSET(sfd, &readable_fds);
1222 return socket_is_alive;
1223 #endif /* def HAVE_POLL */
1225 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */