1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.65 2009/07/22 22:27:16 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 if (NULL == csp->http->host_ip_addr_str)
201 log_error(LOG_LEVEL_ERROR,
202 "Out of memory while getting the server IP address.");
203 return JB_INVALID_SOCKET;
205 retval = getnameinfo(rp->ai_addr, rp->ai_addrlen,
206 csp->http->host_ip_addr_str, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
207 if (!csp->http->host_ip_addr_str || retval)
209 log_error(LOG_LEVEL_ERROR,
210 "Can not save csp->http->host_ip_addr_str: %s",
211 (csp->http->host_ip_addr_str) ?
212 gai_strerror(retval) : "Insufficient memory");
213 freez(csp->http->host_ip_addr_str);
218 if ((fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) ==
221 if ((fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) < 0)
228 { /* turn off TCP coalescence */
230 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
232 #endif /* def TCP_NODELAY */
234 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
235 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
238 fcntl(fd, F_SETFL, flags);
240 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
243 while (connect(fd, rp->ai_addr, rp->ai_addrlen) == JB_INVALID_SOCKET)
246 if (errno == WSAEINPROGRESS)
248 if (sock_errno() == EINPROGRESS)
249 #else /* ifndef _WIN32 */
250 if (errno == EINPROGRESS)
251 #endif /* ndef _WIN32 || __OS2__ */
257 if (sock_errno() != EINTR)
272 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
276 fcntl(fd, F_SETFL, flags);
278 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
280 /* wait for connection to complete */
287 /* MS Windows uses int, not SOCKET, for the 1st arg of select(). Wierd! */
288 if ((select((int)fd + 1, NULL, &wfds, NULL, tv) > 0)
289 && FD_ISSET(fd, &wfds))
292 * See Linux connect(2) man page for more info
293 * about connecting on non-blocking socket.
296 socklen_t optlen = sizeof(socket_in_error);
297 if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &socket_in_error, &optlen))
299 if (!socket_in_error)
301 /* Connection established, no need to try other addresses. */
304 log_error(LOG_LEVEL_CONNECT, "Could not connect to [%s]:%s: %s.",
305 csp->http->host_ip_addr_str, service, strerror(socket_in_error));
309 log_error(LOG_LEVEL_ERROR, "Could not get the state of "
310 "the connection to [%s]:%s: %s; dropping connection.",
311 csp->http->host_ip_addr_str, service, strerror(errno));
315 /* Connection failed, try next address */
319 freeaddrinfo(result);
322 log_error(LOG_LEVEL_CONNECT, "Could not connect to [%s]:%s.",
324 return(JB_INVALID_SOCKET);
326 log_error(LOG_LEVEL_CONNECT, "Connected to %s[%s]:%s.",
327 host, csp->http->host_ip_addr_str, service);
333 #else /* ndef HAVE_RFC2553 */
334 /* Pre-getaddrinfo implementation */
336 jb_socket connect_to(const char *host, int portnum, struct client_state *csp)
338 struct sockaddr_in inaddr;
342 struct timeval tv[1];
343 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
345 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
348 struct access_control_addr dst[1];
349 #endif /* def FEATURE_ACL */
351 memset((char *)&inaddr, 0, sizeof inaddr);
353 if ((addr = resolve_hostname_to_ip(host)) == INADDR_NONE)
355 csp->http->host_ip_addr_str = strdup("unknown");
356 return(JB_INVALID_SOCKET);
360 dst->addr = ntohl(addr);
363 if (block_acl(dst, csp))
370 return(JB_INVALID_SOCKET);
372 #endif /* def FEATURE_ACL */
374 inaddr.sin_addr.s_addr = addr;
375 inaddr.sin_family = AF_INET;
376 csp->http->host_ip_addr_str = strdup(inet_ntoa(inaddr.sin_addr));
379 if (sizeof(inaddr.sin_port) == sizeof(short))
380 #endif /* ndef _WIN32 */
382 inaddr.sin_port = htons((unsigned short) portnum);
387 inaddr.sin_port = htonl((unsigned long)portnum);
389 #endif /* ndef _WIN32 */
392 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) == JB_INVALID_SOCKET)
394 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
397 return(JB_INVALID_SOCKET);
401 { /* turn off TCP coalescence */
403 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
405 #endif /* def TCP_NODELAY */
407 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
408 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
411 fcntl(fd, F_SETFL, flags);
413 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
415 while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == JB_INVALID_SOCKET)
418 if (errno == WSAEINPROGRESS)
420 if (sock_errno() == EINPROGRESS)
421 #else /* ifndef _WIN32 */
422 if (errno == EINPROGRESS)
423 #endif /* ndef _WIN32 || __OS2__ */
429 if (sock_errno() != EINTR)
435 return(JB_INVALID_SOCKET);
439 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
443 fcntl(fd, F_SETFL, flags);
445 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
447 /* wait for connection to complete */
454 /* MS Windows uses int, not SOCKET, for the 1st arg of select(). Wierd! */
455 if (select((int)fd + 1, NULL, &wfds, NULL, tv) <= 0)
458 return(JB_INVALID_SOCKET);
463 #endif /* ndef HAVE_RFC2553 */
466 /*********************************************************************
468 * Function : write_socket
470 * Description : Write the contents of buf (for n bytes) to socket fd.
473 * 1 : fd = file descriptor (aka. handle) of socket to write to.
474 * 2 : buf = pointer to data to be written.
475 * 3 : len = length of data to be written to the socket "fd".
477 * Returns : 0 on success (entire buffer sent).
480 *********************************************************************/
482 int write_socket(jb_socket fd, const char *buf, ssize_t len)
484 int write_socket(jb_socket fd, const char *buf, size_t len)
492 if (len < 0) /* constant condition - size_t isn't ever negative */
497 log_error(LOG_LEVEL_LOG, "%N", len, buf);
500 return (send(fd, buf, (int)len, 0) != (int)len);
501 #elif defined(__BEOS__) || defined(AMIGA)
502 return (send(fd, buf, len, 0) != len);
503 #elif defined(__OS2__)
505 * Break the data up into SOCKET_SEND_MAX chunks for sending...
506 * OS/2 seemed to complain when the chunks were too large.
508 #define SOCKET_SEND_MAX 65000
510 int write_len = 0, send_len, send_rc = 0, i = 0;
511 while ((i < len) && (send_rc != -1))
513 if ((i + SOCKET_SEND_MAX) > len)
516 send_len = SOCKET_SEND_MAX;
517 send_rc = send(fd,(char*)buf + i, send_len, 0);
525 return (write(fd, buf, len) != len);
531 /*********************************************************************
533 * Function : read_socket
535 * Description : Read from a TCP/IP socket in a platform independent way.
538 * 1 : fd = file descriptor of the socket to read
539 * 2 : buf = pointer to buffer where data will be written
540 * Must be >= len bytes long.
541 * 3 : len = maximum number of bytes to read
543 * Returns : On success, the number of bytes read is returned (zero
544 * indicates end of file), and the file position is advanced
545 * by this number. It is not an error if this number is
546 * smaller than the number of bytes requested; this may hap-
547 * pen for example because fewer bytes are actually available
548 * right now (maybe because we were close to end-of-file, or
549 * because we are reading from a pipe, or from a terminal,
550 * or because read() was interrupted by a signal). On error,
551 * -1 is returned, and errno is set appropriately. In this
552 * case it is left unspecified whether the file position (if
555 *********************************************************************/
556 int read_socket(jb_socket fd, char *buf, int len)
564 return(recv(fd, buf, len, 0));
565 #elif defined(__BEOS__) || defined(AMIGA) || defined(__OS2__)
566 return(recv(fd, buf, (size_t)len, 0));
568 return((int)read(fd, buf, (size_t)len));
573 /*********************************************************************
575 * Function : data_is_available
577 * Description : Waits for data to arrive on a socket.
580 * 1 : fd = file descriptor of the socket to read
581 * 2 : seconds_to_wait = number of seconds after which we give up.
583 * Returns : TRUE if data arrived in time,
586 *********************************************************************/
587 int data_is_available(jb_socket fd, int seconds_to_wait)
590 struct timeval timeout;
593 memset(&timeout, 0, sizeof(timeout));
594 timeout.tv_sec = seconds_to_wait;
597 /* Copy and pasted from jcc.c ... */
598 memset(&rfds, 0, sizeof(fd_set));
604 n = select(fd+1, &rfds, NULL, NULL, &timeout);
607 * XXX: Do we care about the different error conditions?
613 /*********************************************************************
615 * Function : close_socket
617 * Description : Closes a TCP/IP socket
620 * 1 : fd = file descriptor of socket to be closed
624 *********************************************************************/
625 void close_socket(jb_socket fd)
627 #if defined(_WIN32) || defined(__BEOS__)
631 #elif defined(__OS2__)
640 /*********************************************************************
642 * Function : bind_port
644 * Description : Call socket, set socket options, and listen.
645 * Called by listen_loop to "boot up" our proxy address.
648 * 1 : hostnam = TCP/IP address to bind/listen to
649 * 2 : portnum = port to listen on
650 * 3 : pfd = pointer used to return file descriptor.
652 * Returns : if success, returns 0 and sets *pfd.
653 * if failure, returns -3 if address is in use,
654 * -2 if address unresolvable,
656 *********************************************************************/
657 int bind_port(const char *hostnam, int portnum, jb_socket *pfd)
660 struct addrinfo hints;
661 struct addrinfo *result, *rp;
663 * XXX: portnum should be a string to allow symbolic service
664 * names in the configuration file and to avoid the following
670 struct sockaddr_in inaddr;
671 #endif /* def HAVE_RFC2553 */
675 #endif /* ndef _WIN32 */
677 *pfd = JB_INVALID_SOCKET;
680 retval = snprintf(servnam, sizeof(servnam), "%d", portnum);
681 if ((-1 == retval) || (sizeof(servnam) <= retval))
683 log_error(LOG_LEVEL_ERROR,
684 "Port number (%d) ASCII decimal representation doesn't fit into 6 bytes",
689 memset(&hints, 0, sizeof(struct addrinfo));
690 if ((hostnam == NULL) || !strcmpic(hostnam, "localhost"))
693 * XXX: This is a hack. The right thing to do
694 * would be to bind to both AF_INET and AF_INET6.
695 * This will also fail if there is no AF_INET
698 hints.ai_family = AF_INET;
702 hints.ai_family = AF_UNSPEC;
704 hints.ai_socktype = SOCK_STREAM;
705 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
706 hints.ai_protocol = 0; /* Realy any stream protocol or TCP only */
707 hints.ai_canonname = NULL;
708 hints.ai_addr = NULL;
709 hints.ai_next = NULL;
711 if ((retval = getaddrinfo(hostnam, servnam, &hints, &result)))
713 log_error(LOG_LEVEL_ERROR,
714 "Can not resolve %s: %s", hostnam, gai_strerror(retval));
718 memset((char *)&inaddr, '\0', sizeof inaddr);
720 inaddr.sin_family = AF_INET;
721 inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
723 if (inaddr.sin_addr.s_addr == INADDR_NONE)
729 if (sizeof(inaddr.sin_port) == sizeof(short))
730 #endif /* ndef _WIN32 */
732 inaddr.sin_port = htons((unsigned short) portnum);
737 inaddr.sin_port = htonl((unsigned long) portnum);
739 #endif /* ndef _WIN32 */
740 #endif /* def HAVE_RFC2553 */
743 for (rp = result; rp != NULL; rp = rp->ai_next)
745 fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
747 fd = socket(AF_INET, SOCK_STREAM, 0);
748 #endif /* def HAVE_RFC2553 */
751 if (fd == JB_INVALID_SOCKET)
765 * This is not needed for Win32 - in fact, it stops
766 * duplicate instances of Privoxy from being caught.
768 * On UNIX, we assume the user is sensible enough not
769 * to start Privoxy multiple times on the same IP.
770 * Without this, stopping and restarting Privoxy
771 * from a script fails.
772 * Note: SO_REUSEADDR is meant to only take over
773 * sockets which are *not* in listen state in Linux,
774 * e.g. sockets in TIME_WAIT. YMMV.
776 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
777 #endif /* ndef _WIN32 */
780 if (bind(fd, rp->ai_addr, rp->ai_addrlen) < 0)
782 if (bind(fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
786 errno = WSAGetLastError();
787 if (errno == WSAEADDRINUSE)
789 if (errno == EADDRINUSE)
793 freeaddrinfo(result);
810 /* bind() succeeded, escape from for-loop */
812 * XXX: Support multiple listening sockets (e.g. localhost
813 * resolves to AF_INET and AF_INET6, but only the first address
820 freeaddrinfo(result);
823 /* All bind()s failed */
826 #endif /* ndef HAVE_RFC2553 */
828 while (listen(fd, MAX_LISTEN_BACKLOG) == -1)
842 /*********************************************************************
844 * Function : get_host_information
846 * Description : Determines the IP address the client used to
847 * reach us and the hostname associated with it.
849 * XXX: Most of the code has been copy and pasted
850 * from accept_connection() and not all of the
851 * ifdefs paths have been tested afterwards.
854 * 1 : afd = File descriptor returned from accept().
855 * 2 : ip_address = Pointer to return the pointer to
856 * the ip address string.
857 * 3 : hostname = Pointer to return the pointer to
858 * the hostname or NULL if the caller
859 * isn't interested in it.
863 *********************************************************************/
864 void get_host_information(jb_socket afd, char **ip_address, char **hostname)
867 struct sockaddr_storage server;
870 struct sockaddr_in server;
871 struct hostent *host = NULL;
872 #endif /* HAVE_RFC2553 */
873 #if defined(_WIN32) || defined(__OS2__) || defined(__APPLE_CC__) || defined(AMIGA)
874 /* according to accept_connection() this fixes a warning. */
875 int s_length, s_length_provided;
877 socklen_t s_length, s_length_provided;
880 #if defined(HAVE_GETHOSTBYADDR_R_8_ARGS) || defined(HAVE_GETHOSTBYADDR_R_7_ARGS) || defined(HAVE_GETHOSTBYADDR_R_5_ARGS)
881 struct hostent result;
882 #if defined(HAVE_GETHOSTBYADDR_R_5_ARGS)
883 struct hostent_data hdata;
885 char hbuf[HOSTENT_BUFFER_SIZE];
887 #endif /* def HAVE_GETHOSTBYADDR_R_5_ARGS */
888 #endif /* def HAVE_GETHOSTBYADDR_R_(8|7|5)_ARGS */
889 #endif /* ifndef HAVE_RFC2553 */
890 s_length = s_length_provided = sizeof(server);
892 if (NULL != hostname)
898 if (!getsockname(afd, (struct sockaddr *) &server, &s_length))
900 if (s_length > s_length_provided)
902 log_error(LOG_LEVEL_ERROR, "getsockname() truncated server address");
906 *ip_address = malloc(NI_MAXHOST);
907 if (NULL == *ip_address)
909 log_error(LOG_LEVEL_ERROR,
910 "Out of memory while getting the client's IP address.");
913 retval = getnameinfo((struct sockaddr *) &server, s_length,
914 *ip_address, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
917 log_error(LOG_LEVEL_ERROR,
918 "Unable to print my own IP address: %s", gai_strerror(retval));
923 *ip_address = strdup(inet_ntoa(server.sin_addr));
924 #endif /* HAVE_RFC2553 */
925 if (NULL == hostname)
928 * We're done here, the caller isn't
929 * interested in knowing the hostname.
935 *hostname = malloc(NI_MAXHOST);
936 if (NULL == *hostname)
938 log_error(LOG_LEVEL_ERROR,
939 "Out of memory while getting the client's hostname.");
942 retval = getnameinfo((struct sockaddr *) &server, s_length,
943 *hostname, NI_MAXHOST, NULL, 0, NI_NAMEREQD);
946 log_error(LOG_LEVEL_ERROR,
947 "Unable to resolve my own IP address: %s", gai_strerror(retval));
951 #if defined(HAVE_GETHOSTBYADDR_R_8_ARGS)
952 gethostbyaddr_r((const char *)&server.sin_addr,
953 sizeof(server.sin_addr), AF_INET,
954 &result, hbuf, HOSTENT_BUFFER_SIZE,
956 #elif defined(HAVE_GETHOSTBYADDR_R_7_ARGS)
957 host = gethostbyaddr_r((const char *)&server.sin_addr,
958 sizeof(server.sin_addr), AF_INET,
959 &result, hbuf, HOSTENT_BUFFER_SIZE, &thd_err);
960 #elif defined(HAVE_GETHOSTBYADDR_R_5_ARGS)
961 if (0 == gethostbyaddr_r((const char *)&server.sin_addr,
962 sizeof(server.sin_addr), AF_INET,
971 #elif defined(MUTEX_LOCKS_AVAILABLE)
972 privoxy_mutex_lock(&resolver_mutex);
973 host = gethostbyaddr((const char *)&server.sin_addr,
974 sizeof(server.sin_addr), AF_INET);
975 privoxy_mutex_unlock(&resolver_mutex);
977 host = gethostbyaddr((const char *)&server.sin_addr,
978 sizeof(server.sin_addr), AF_INET);
982 log_error(LOG_LEVEL_ERROR, "Unable to get my own hostname: %E\n");
986 *hostname = strdup(host->h_name);
988 #endif /* else def HAVE_RFC2553 */
995 /*********************************************************************
997 * Function : accept_connection
999 * Description : Accepts a connection on a socket. Socket must have
1000 * been created using bind_port().
1003 * 1 : csp = Client state, cfd, ip_addr_str, and
1004 * ip_addr_long will be set by this routine.
1005 * 2 : fd = file descriptor returned from bind_port
1007 * Returns : when a connection is accepted, it returns 1 (TRUE).
1008 * On an error it returns 0 (FALSE).
1010 *********************************************************************/
1011 int accept_connection(struct client_state * csp, jb_socket fd)
1014 /* XXX: client is stored directly into csp->tcp_addr */
1015 #define client (csp->tcp_addr)
1018 struct sockaddr_in client;
1021 #if defined(_WIN32) || defined(__OS2__) || defined(__APPLE_CC__) || defined(AMIGA)
1022 /* Wierdness - fix a warning. */
1028 c_length = sizeof(client);
1031 afd = accept (fd, (struct sockaddr *) &client, &c_length);
1032 if (afd == JB_INVALID_SOCKET)
1039 afd = accept (fd, (struct sockaddr *) &client, &c_length);
1040 } while (afd < 1 && errno == EINTR);
1049 csp->ip_addr_str = malloc(NI_MAXHOST);
1050 if (NULL == csp->ip_addr_str)
1052 log_error(LOG_LEVEL_ERROR,
1053 "Out of memory while getting the client's IP address.");
1056 retval = getnameinfo((struct sockaddr *) &client, c_length,
1057 csp->ip_addr_str, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
1058 if (!csp->ip_addr_str || retval)
1060 log_error(LOG_LEVEL_ERROR, "Can not save csp->ip_addr_str: %s",
1061 (csp->ip_addr_str) ? gai_strerror(retval) : "Insuffcient memory");
1062 freez(csp->ip_addr_str);
1066 csp->ip_addr_str = strdup(inet_ntoa(client.sin_addr));
1067 csp->ip_addr_long = ntohl(client.sin_addr.s_addr);
1068 #endif /* def HAVE_RFC2553 */
1075 /*********************************************************************
1077 * Function : resolve_hostname_to_ip
1079 * Description : Resolve a hostname to an internet tcp/ip address.
1080 * NULL or an empty string resolve to INADDR_ANY.
1083 * 1 : host = hostname to resolve
1085 * Returns : INADDR_NONE => failure, INADDR_ANY or tcp/ip address if succesful.
1087 *********************************************************************/
1088 unsigned long resolve_hostname_to_ip(const char *host)
1090 struct sockaddr_in inaddr;
1091 struct hostent *hostp;
1092 unsigned int dns_retries = 0;
1093 #if defined(HAVE_GETHOSTBYNAME_R_6_ARGS) || defined(HAVE_GETHOSTBYNAME_R_5_ARGS) || defined(HAVE_GETHOSTBYNAME_R_3_ARGS)
1094 struct hostent result;
1095 #if defined(HAVE_GETHOSTBYNAME_R_6_ARGS) || defined(HAVE_GETHOSTBYNAME_R_5_ARGS)
1096 char hbuf[HOSTENT_BUFFER_SIZE];
1098 #else /* defined(HAVE_GETHOSTBYNAME_R_3_ARGS) */
1099 struct hostent_data hdata;
1100 #endif /* def HAVE_GETHOSTBYNAME_R_(6|5)_ARGS */
1101 #endif /* def HAVE_GETHOSTBYNAME_R_(6|5|3)_ARGS */
1103 if ((host == NULL) || (*host == '\0'))
1108 memset((char *) &inaddr, 0, sizeof inaddr);
1110 if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
1112 #if defined(HAVE_GETHOSTBYNAME_R_6_ARGS)
1113 while (gethostbyname_r(host, &result, hbuf,
1114 HOSTENT_BUFFER_SIZE, &hostp, &thd_err)
1115 && (thd_err == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES))
1117 log_error(LOG_LEVEL_ERROR,
1118 "Timeout #%u while trying to resolve %s. Trying again.",
1121 #elif defined(HAVE_GETHOSTBYNAME_R_5_ARGS)
1122 while (NULL == (hostp = gethostbyname_r(host, &result,
1123 hbuf, HOSTENT_BUFFER_SIZE, &thd_err))
1124 && (thd_err == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES))
1126 log_error(LOG_LEVEL_ERROR,
1127 "Timeout #%u while trying to resolve %s. Trying again.",
1130 #elif defined(HAVE_GETHOSTBYNAME_R_3_ARGS)
1132 * XXX: Doesn't retry in case of soft errors.
1133 * Does this gethostbyname_r version set h_errno?
1135 if (0 == gethostbyname_r(host, &result, &hdata))
1143 #elif defined(MUTEX_LOCKS_AVAILABLE)
1144 privoxy_mutex_lock(&resolver_mutex);
1145 while (NULL == (hostp = gethostbyname(host))
1146 && (h_errno == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES))
1148 log_error(LOG_LEVEL_ERROR,
1149 "Timeout #%u while trying to resolve %s. Trying again.",
1152 privoxy_mutex_unlock(&resolver_mutex);
1154 while (NULL == (hostp = gethostbyname(host))
1155 && (h_errno == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES))
1157 log_error(LOG_LEVEL_ERROR,
1158 "Timeout #%u while trying to resolve %s. Trying again.",
1161 #endif /* def HAVE_GETHOSTBYNAME_R_(6|5|3)_ARGS */
1163 * On Mac OSX, if a domain exists but doesn't have a type A
1164 * record associated with it, the h_addr member of the struct
1165 * hostent returned by gethostbyname is NULL, even if h_length
1166 * is 4. Therefore the second test below.
1168 if (hostp == NULL || hostp->h_addr == NULL)
1171 log_error(LOG_LEVEL_ERROR, "could not resolve hostname %s", host);
1172 return(INADDR_NONE);
1174 if (hostp->h_addrtype != AF_INET)
1177 errno = WSAEPROTOTYPE;
1181 log_error(LOG_LEVEL_ERROR, "hostname %s resolves to unknown address type.", host);
1182 return(INADDR_NONE);
1185 (char *) &inaddr.sin_addr,
1186 (char *) hostp->h_addr,
1187 sizeof(inaddr.sin_addr)
1190 return(inaddr.sin_addr.s_addr);
1195 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1196 /*********************************************************************
1198 * Function : socket_is_still_usable
1200 * Description : Decides whether or not an open socket is still usable.
1203 * 1 : sfd = The socket to check.
1205 * Returns : TRUE for yes, otherwise FALSE.
1207 *********************************************************************/
1208 int socket_is_still_usable(jb_socket sfd)
1211 int no_data_waiting;
1215 struct pollfd poll_fd[1];
1217 memset(poll_fd, 0, sizeof(poll_fd));
1218 poll_fd[0].fd = sfd;
1219 poll_fd[0].events = POLLIN;
1221 poll_result = poll(poll_fd, 1, 0);
1223 if (-1 == poll_result)
1225 log_error(LOG_LEVEL_CONNECT, "Polling socket %d failed.", sfd);
1228 no_data_waiting = !(poll_fd[0].revents & POLLIN);
1230 fd_set readable_fds;
1231 struct timeval timeout;
1234 memset(&timeout, '\0', sizeof(timeout));
1235 FD_ZERO(&readable_fds);
1236 FD_SET(sfd, &readable_fds);
1238 ret = select((int)sfd+1, &readable_fds, NULL, NULL, &timeout);
1241 log_error(LOG_LEVEL_CONNECT, "select() on socket %d failed: %E", sfd);
1244 no_data_waiting = !FD_ISSET(sfd, &readable_fds);
1245 #endif /* def HAVE_POLL */
1247 return (no_data_waiting || (1 == recv(sfd, buf, 1, MSG_PEEK)));
1249 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */