2003-12-13 22:53:17 +00:00
|
|
|
/* Copyright 2002,2003 Nick Mathewson, Roger Dingledine */
|
2003-12-09 21:37:26 +00:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
|
|
|
|
2004-05-01 20:46:28 +00:00
|
|
|
/*****
|
|
|
|
* fakepoll.c: On systems where 'poll' doesn't exist, fake it with 'select'.
|
|
|
|
*****/
|
2002-09-03 18:43:50 +00:00
|
|
|
|
2003-08-12 03:08:41 +00:00
|
|
|
#include "orconfig.h"
|
2003-08-12 06:41:40 +00:00
|
|
|
#include "fakepoll.h"
|
2003-08-14 17:13:52 +00:00
|
|
|
|
2004-05-05 01:01:34 +00:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
2002-09-03 18:43:50 +00:00
|
|
|
#include <sys/types.h>
|
2004-05-05 01:01:34 +00:00
|
|
|
#endif
|
2003-08-12 03:08:41 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2002-09-03 18:43:50 +00:00
|
|
|
#include <unistd.h>
|
2003-08-12 03:08:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
2002-09-03 18:43:50 +00:00
|
|
|
#include <string.h>
|
2003-08-12 03:08:41 +00:00
|
|
|
#endif
|
2004-04-03 04:05:12 +00:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2003-08-12 03:08:41 +00:00
|
|
|
#if _MSC_VER > 1300
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
#include <winsock.h>
|
|
|
|
#endif
|
|
|
|
|
2004-05-04 18:17:28 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2003-08-12 03:08:41 +00:00
|
|
|
#include "util.h"
|
2004-05-04 18:17:28 +00:00
|
|
|
#include "log.h"
|
2002-09-03 18:43:50 +00:00
|
|
|
|
2004-05-05 01:01:34 +00:00
|
|
|
#ifndef USE_FAKE_POLL
|
|
|
|
int
|
|
|
|
tor_poll(struct pollfd *ufds, unsigned int nfds, int timeout)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0;i<nfds;++i) {
|
|
|
|
tor_assert(ufds[i].fd >= 0);
|
|
|
|
}
|
|
|
|
return poll(ufds,nfds,timeout);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* by default, windows handles only 64 fd's */
|
|
|
|
#if defined(MS_WINDOWS) && !defined(FD_SETSIZE)
|
|
|
|
#define FD_SETSIZE MAXCONNECTIONS
|
|
|
|
#endif
|
|
|
|
|
2002-09-03 18:43:50 +00:00
|
|
|
int
|
2004-02-20 23:41:45 +00:00
|
|
|
tor_poll(struct pollfd *ufds, unsigned int nfds, int timeout)
|
2002-09-03 18:43:50 +00:00
|
|
|
{
|
2004-03-12 13:02:16 +00:00
|
|
|
unsigned int idx;
|
2004-03-10 06:57:57 +00:00
|
|
|
int maxfd, fd;
|
2003-12-17 21:14:13 +00:00
|
|
|
int r;
|
2003-08-14 17:13:52 +00:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
int any_fds_set = 0;
|
|
|
|
#endif
|
2003-12-17 21:14:13 +00:00
|
|
|
fd_set readfds, writefds, exceptfds;
|
2003-08-12 08:18:13 +00:00
|
|
|
#ifdef USING_FAKE_TIMEVAL
|
|
|
|
#undef timeval
|
|
|
|
#undef tv_sec
|
|
|
|
#undef tv_usec
|
|
|
|
#endif
|
2003-12-17 21:14:13 +00:00
|
|
|
struct timeval _timeout;
|
|
|
|
_timeout.tv_sec = timeout/1000;
|
|
|
|
_timeout.tv_usec = (timeout%1000)*1000;
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_ZERO(&writefds);
|
|
|
|
FD_ZERO(&exceptfds);
|
2002-09-03 18:43:50 +00:00
|
|
|
|
2003-12-17 21:14:13 +00:00
|
|
|
maxfd = -1;
|
|
|
|
for (idx = 0; idx < nfds; ++idx) {
|
2003-10-07 21:16:04 +00:00
|
|
|
ufds[idx].revents = 0;
|
2003-12-17 21:14:13 +00:00
|
|
|
fd = ufds[idx].fd;
|
2004-05-02 20:27:48 +00:00
|
|
|
tor_assert(fd >= 0);
|
2003-10-07 21:16:04 +00:00
|
|
|
if (fd > maxfd) {
|
|
|
|
maxfd = fd;
|
2003-08-14 17:13:52 +00:00
|
|
|
#ifdef MS_WINDOWS
|
2003-10-07 21:16:04 +00:00
|
|
|
any_fds_set = 1;
|
2003-08-14 17:13:52 +00:00
|
|
|
#endif
|
|
|
|
}
|
2003-12-17 21:14:13 +00:00
|
|
|
if (ufds[idx].events & POLLIN)
|
|
|
|
FD_SET(fd, &readfds);
|
|
|
|
if (ufds[idx].events & POLLOUT)
|
|
|
|
FD_SET(fd, &writefds);
|
|
|
|
FD_SET(fd, &exceptfds);
|
|
|
|
}
|
2003-08-14 17:13:52 +00:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
if (!any_fds_set) {
|
2003-08-14 17:51:36 +00:00
|
|
|
Sleep(timeout);
|
2003-08-14 17:13:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2003-12-17 21:14:13 +00:00
|
|
|
r = select(maxfd+1, &readfds, &writefds, &exceptfds,
|
|
|
|
timeout == -1 ? NULL : &_timeout);
|
|
|
|
if (r <= 0)
|
|
|
|
return r;
|
|
|
|
r = 0;
|
|
|
|
for (idx = 0; idx < nfds; ++idx) {
|
|
|
|
fd = ufds[idx].fd;
|
|
|
|
if (FD_ISSET(fd, &readfds))
|
|
|
|
ufds[idx].revents |= POLLIN;
|
|
|
|
if (FD_ISSET(fd, &writefds))
|
|
|
|
ufds[idx].revents |= POLLOUT;
|
|
|
|
if (FD_ISSET(fd, &exceptfds))
|
|
|
|
ufds[idx].revents |= POLLERR;
|
|
|
|
if (ufds[idx].revents)
|
|
|
|
++r;
|
|
|
|
}
|
|
|
|
return r;
|
2002-09-03 18:43:50 +00:00
|
|
|
}
|
|
|
|
#endif
|
2004-04-06 03:44:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Local Variables:
|
|
|
|
mode:c
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
c-basic-offset:2
|
|
|
|
End:
|
|
|
|
*/
|