char service[6];
int retval;
jb_socket fd;
+#ifdef HAVE_POLL
struct pollfd poll_fd[1];
+#else
+ fd_set wfds;
+ struct timeval timeout;
+#endif
#if !defined(_WIN32) && !defined(__BEOS__)
int flags;
#endif
continue;
}
+#ifndef HAVE_POLL
+#ifndef _WIN32
+ if (fd >= FD_SETSIZE)
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "Server socket number too high to use select(): %d >= %d",
+ fd, FD_SETSIZE);
+ close_socket(fd);
+ freeaddrinfo(result);
+ return JB_INVALID_SOCKET;
+ }
+#endif
+#endif
+
#ifdef FEATURE_EXTERNAL_FILTERS
mark_socket_for_close_on_execute(fd);
#endif
}
#endif /* !defined(_WIN32) && !defined(__BEOS__) */
+#ifdef HAVE_POLL
poll_fd[0].fd = fd;
poll_fd[0].events = POLLOUT;
}
}
else if (retval > 0)
+#else
+ /* wait for connection to complete */
+ FD_ZERO(&wfds);
+ FD_SET(fd, &wfds);
+
+ memset(&timeout, 0, sizeof(timeout));
+ timeout.tv_sec = 30;
+
+ /* MS Windows uses int, not SOCKET, for the 1st arg of select(). Weird! */
+ if ((select((int)fd + 1, NULL, &wfds, NULL, &timeout) > 0)
+ && FD_ISSET(fd, &wfds))
+#endif
{
socklen_t optlen = sizeof(socket_error);
if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &socket_error, &optlen))
struct sockaddr_in inaddr;
jb_socket fd;
unsigned int addr;
+#ifdef HAVE_POLL
struct pollfd poll_fd[1];
+#else
+ fd_set wfds;
+ struct timeval tv[1];
+#endif
#if !defined(_WIN32) && !defined(__BEOS__)
int flags;
#endif
return(JB_INVALID_SOCKET);
}
+#ifndef HAVE_POLL
+#ifndef _WIN32
+ if (fd >= FD_SETSIZE)
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "Server socket number too high to use select(): %d >= %d",
+ fd, FD_SETSIZE);
+ close_socket(fd);
+ return JB_INVALID_SOCKET;
+ }
+#endif
+#endif
+
set_no_delay_flag(fd);
#if !defined(_WIN32) && !defined(__BEOS__)
}
#endif /* !defined(_WIN32) && !defined(__BEOS__) */
+#ifdef HAVE_POLL
poll_fd[0].fd = fd;
poll_fd[0].events = POLLOUT;
if (poll(poll_fd, 1, 30000) <= 0)
+#else
+ /* wait for connection to complete */
+ FD_ZERO(&wfds);
+ FD_SET(fd, &wfds);
+
+ tv->tv_sec = 30;
+ tv->tv_usec = 0;
+
+ /* MS Windows uses int, not SOCKET, for the 1st arg of select(). Weird! */
+ if (select((int)fd + 1, NULL, &wfds, NULL, tv) <= 0)
+#endif
{
close_socket(fd);
return(JB_INVALID_SOCKET);
{
int n;
char buf[10];
+#ifdef HAVE_POLL
struct pollfd poll_fd[1];
poll_fd[0].fd = fd;
poll_fd[0].events = POLLIN;
n = poll(poll_fd, 1, seconds_to_wait * 1000);
+#else
+ fd_set rfds;
+ struct timeval timeout;
+
+ memset(&timeout, 0, sizeof(timeout));
+ timeout.tv_sec = seconds_to_wait;
+
+ FD_ZERO(&rfds);
+ FD_SET(fd, &rfds);
+
+ n = select(fd+1, &rfds, NULL, NULL, &timeout);
+#endif
/*
* XXX: Do we care about the different error conditions?
int retval;
int i;
int max_selected_socket;
+#ifdef HAVE_POLL
struct pollfd poll_fds[MAX_LISTENING_SOCKETS];
nfds_t polled_sockets;
+#else
+ fd_set selected_fds;
+#endif
jb_socket fd;
const char *host_addr;
size_t listen_addr_size;
c_length = sizeof(client);
+#ifdef HAVE_POLL
memset(poll_fds, 0, sizeof(poll_fds));
polled_sockets = 0;
+#else
+ /*
+ * Wait for a connection on any socket.
+ * Return immediately if no socket is listening.
+ * XXX: Why not treat this as fatal error?
+ */
+ FD_ZERO(&selected_fds);
+#endif
max_selected_socket = 0;
for (i = 0; i < MAX_LISTENING_SOCKETS; i++)
{
if (JB_INVALID_SOCKET != fds[i])
{
+#ifdef HAVE_POLL
poll_fds[i].fd = fds[i];
poll_fds[i].events = POLLIN;
polled_sockets++;
+#else
+ FD_SET(fds[i], &selected_fds);
+#endif
if (max_selected_socket < fds[i] + 1)
{
max_selected_socket = fds[i] + 1;
}
do
{
+#ifdef HAVE_POLL
retval = poll(poll_fds, polled_sockets, -1);
+#else
+ retval = select(max_selected_socket, &selected_fds, NULL, NULL, NULL);
+#endif
} while (retval < 0 && errno == EINTR);
if (retval <= 0)
{
if (0 == retval)
{
log_error(LOG_LEVEL_ERROR,
- "Waiting on new client failed because poll(2) returned 0."
+ "Waiting on new client failed because select(2) returned 0."
" This should not happen.");
}
else
{
log_error(LOG_LEVEL_ERROR,
- "Waiting on new client failed because of problems in poll(2): "
+ "Waiting on new client failed because of problems in select(2): "
"%s.", strerror(errno));
}
return 0;
}
+#ifdef HAVE_POLL
for (i = 0; i < MAX_LISTENING_SOCKETS && (poll_fds[i].revents == 0); i++);
+#else
+ for (i = 0; i < MAX_LISTENING_SOCKETS && !FD_ISSET(fds[i], &selected_fds);
+ i++);
+#endif
if (i >= MAX_LISTENING_SOCKETS)
{
log_error(LOG_LEVEL_ERROR,
- "poll(2) reported connected clients (number = %u, "
+ "select(2) reported connected clients (number = %u, "
"descriptor boundary = %u), but none found.",
retval, max_selected_socket);
return 0;
}
#endif
+#ifndef HAVE_POLL
+#ifndef _WIN32
+ if (afd >= FD_SETSIZE)
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "Client socket number too high to use select(): %d >= %d",
+ afd, FD_SETSIZE);
+ close_socket(afd);
+ return 0;
+ }
+#endif
+#endif
+
#ifdef FEATURE_EXTERNAL_FILTERS
mark_socket_for_close_on_execute(afd);
#endif
{
char buf[10];
int no_data_waiting;
+#ifdef HAVE_POLL
int poll_result;
struct pollfd poll_fd[1];
return FALSE;
}
no_data_waiting = !(poll_fd[0].revents & POLLIN);
+#else
+ fd_set readable_fds;
+ struct timeval timeout;
+ int ret;
+
+ memset(&timeout, '\0', sizeof(timeout));
+ FD_ZERO(&readable_fds);
+ FD_SET(sfd, &readable_fds);
+
+ ret = select((int)sfd+1, &readable_fds, NULL, NULL, &timeout);
+ if (ret < 0)
+ {
+ log_error(LOG_LEVEL_CONNECT, "select() on socket %d failed: %E", sfd);
+ return FALSE;
+ }
+ no_data_waiting = !FD_ISSET(sfd, &readable_fds);
+#endif /* def HAVE_POLL */
return (no_data_waiting || (1 == recv(sfd, buf, 1, MSG_PEEK)));
}
#else
#include <poll.h>
#endif /* def __GLIBC__ */
+#else
+# ifndef FD_ZERO
+# include <select.h>
+# endif
+#warning poll() appears to be unavailable. Your platform will become unsupported in the future.
#endif /* HAVE_POLL */
#endif
char *hdr;
char *p;
int n;
+#ifdef HAVE_POLL
struct pollfd poll_fds[2];
+#else
+ fd_set rfds;
+ jb_socket maxfd;
+ struct timeval timeout;
+#endif
int server_body;
int ms_iis5_hack = 0;
unsigned long long byte_count = 0;
http = csp->http;
+#ifndef HAVE_POLL
+ maxfd = (csp->cfd > csp->server_connection.sfd) ?
+ csp->cfd : csp->server_connection.sfd;
+#endif
+
/* pass data between the client and server
* until one or the other shuts down the connection.
*/
for (;;)
{
+#ifndef HAVE_POLL
+ FD_ZERO(&rfds);
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+ if (!watch_client_socket)
+ {
+ maxfd = csp->server_connection.sfd;
+ }
+ else
+#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
+ {
+ FD_SET(csp->cfd, &rfds);
+ }
+
+ FD_SET(csp->server_connection.sfd, &rfds);
+#endif /* ndef HAVE_POLL */
+
#ifdef FEATURE_CONNECTION_KEEP_ALIVE
if ((csp->flags & CSP_FLAG_CHUNKED)
&& !(csp->flags & CSP_FLAG_CONTENT_LENGTH_SET)
}
#endif /* FEATURE_CONNECTION_KEEP_ALIVE */
+#ifdef HAVE_POLL
poll_fds[0].fd = csp->cfd;
#ifdef FEATURE_CONNECTION_KEEP_ALIVE
if (!watch_client_socket)
poll_fds[1].fd = csp->server_connection.sfd;
poll_fds[1].events = POLLIN;
n = poll(poll_fds, 2, csp->config->socket_timeout * 1000);
+#else
+ timeout.tv_sec = csp->config->socket_timeout;
+ timeout.tv_usec = 0;
+ n = select((int)maxfd + 1, &rfds, NULL, NULL, &timeout);
+#endif /* def HAVE_POLL */
/*server or client not responding in timeout */
if (n == 0)
}
else if (n < 0)
{
+#ifdef HAVE_POLL
log_error(LOG_LEVEL_ERROR, "poll() failed!: %E");
+#else
+ log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
+#endif
mark_server_socket_tainted(csp);
#ifdef FEATURE_HTTPS_INSPECTION
close_client_and_server_ssl_connections(csp);
* XXX: Make sure the client doesn't use pipelining
* behind Privoxy's back.
*/
+#ifdef HAVE_POLL
if ((poll_fds[0].revents & (POLLERR|POLLHUP|POLLNVAL)) != 0)
{
log_error(LOG_LEVEL_CONNECT,
}
if (poll_fds[0].revents != 0)
+#else
+ if (FD_ISSET(csp->cfd, &rfds))
+#endif /* def HAVE_POLL*/
{
int max_bytes_to_read = (int)csp->receive_buffer_size;
{
/*
* If the next request is already waiting, we have
- * to stop poll()ing the client socket. Otherwise
+ * to stop select()ing the client socket. Otherwise
* we would always return right away and get nothing
* else done.
*/
* If `hdr' is null, then it's the header otherwise it's the body.
* FIXME: Does `hdr' really mean `host'? No.
*/
+#ifdef HAVE_POLL
if (poll_fds[1].revents != 0)
+#else
+ if (FD_ISSET(csp->server_connection.sfd, &rfds))
+#endif /* HAVE_POLL */
{
#ifdef FEATURE_CONNECTION_KEEP_ALIVE
/*
return JB_INVALID_SOCKET;
}
+#ifndef HAVE_POLL
+#ifndef _WIN32
+ if (bfd >= FD_SETSIZE)
+ {
+ log_error(LOG_LEVEL_FATAL,
+ "Bind socket number too high to use select(): %d >= %d",
+ bfd, FD_SETSIZE);
+ }
+#endif
+#endif
+
if (haddr == NULL)
{
log_error(LOG_LEVEL_INFO, "Listening on port %d on all IP addresses",
{
int max_client_connections = parse_numeric_value(cmd, arg);
+#if !defined(_WIN32) && !defined(HAVE_POLL)
+ /*
+ * Reject values below 1 for obvious reasons and values above
+ * FD_SETSIZE/2 because Privoxy needs two sockets to serve
+ * client connections that need forwarding.
+ *
+ * We ignore the fact that the first three file descriptors
+ * are usually set to /dev/null, one is used for logging
+ * and yet another file descriptor is required to load
+ * config files.
+ */
+ if ((max_client_connections < 1) || (FD_SETSIZE/2 < max_client_connections))
+ {
+ log_error(LOG_LEVEL_FATAL, "max-client-connections value %d"
+ " is invalid. Value needs to be above 1 and below %d"
+ " (FD_SETSIZE/2).", max_client_connections, FD_SETSIZE/2);
+ }
+#else
+ /*
+ * The Windows libc uses FD_SETSIZE for an array used
+ * by select(), but has no problems with file descriptors
+ * above the limit as long as no more than FD_SETSIZE are
+ * passed to select().
+ * https://msdn.microsoft.com/en-us/library/windows/desktop/ms739169%28v=vs.85%29.aspx
+ *
+ * On platforms were we use poll() we don't have to enforce
+ * an upper connection limit either.
+ */
if (max_client_connections < 1)
{
log_error(LOG_LEVEL_FATAL, "max-client-connections value"
" has to be a number above 1. %d is invalid.",
max_client_connections);
}
-
+#endif
config->max_client_connections = max_client_connections;
break;
}