mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-12-06 19:41:15 +01:00
Make Tor use Niels Provos's libevent instead of it's current
poll-but-sometimes-select mess. This will let us use faster async cores (like epoll, kpoll, and /dev/poll), and hopefully work better on Windows too. There are some fairly nasty changes to main.c here; this will almost certainly break something. But hey, that's what alphas are for. svn:r3341
This commit is contained in:
@@ -3,7 +3,7 @@ noinst_LIBRARIES = libor.a libor-crypto.a
|
||||
|
||||
#CFLAGS = -Wall -Wpointer-arith -O2
|
||||
|
||||
libor_a_SOURCES = log.c fakepoll.c util.c compat.c container.c
|
||||
libor_a_SOURCES = log.c util.c compat.c container.c
|
||||
libor_crypto_a_SOURCES = crypto.c aes.c tortls.c torgzip.c
|
||||
|
||||
noinst_HEADERS = log.h crypto.h fakepoll.h test.h util.h compat.h aes.h torint.h tortls.h strlcpy.c strlcat.c torgzip.h container.h
|
||||
noinst_HEADERS = log.h crypto.h test.h util.h compat.h aes.h torint.h tortls.h strlcpy.c strlcat.c torgzip.h container.h
|
||||
|
||||
@@ -8,7 +8,6 @@ const char compat_c_id[] = "$Id$";
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include "orconfig.h"
|
||||
#include "fakepoll.h"
|
||||
#include "compat.h"
|
||||
|
||||
#ifdef MS_WINDOWS
|
||||
|
||||
@@ -115,6 +115,10 @@ int replace_file(const char *from, const char *to);
|
||||
#define tor_close_socket(s) close(s)
|
||||
#endif
|
||||
|
||||
/* Now that we use libevent, all real sockets are safe for polling ... or
|
||||
* if they aren't, libevent will help us. */
|
||||
#define SOCKET_IS_POLLABLE(fd) ((fd)>=0)
|
||||
|
||||
struct in_addr;
|
||||
int tor_inet_aton(const char *cp, struct in_addr *addr);
|
||||
int tor_lookup_hostname(const char *name, uint32_t *addr);
|
||||
|
||||
@@ -1,108 +0,0 @@
|
||||
/* Copyright 2002,2003 Nick Mathewson, Roger Dingledine */
|
||||
/* See LICENSE for licensing information */
|
||||
/* $Id$ */
|
||||
const char fakepoll_c_id[] = "$Id$";
|
||||
|
||||
/**
|
||||
* \file fakepoll.c
|
||||
*
|
||||
* \brief On systems where poll() doesn't exist, fake it with select().
|
||||
**/
|
||||
|
||||
#include "orconfig.h"
|
||||
#include "fakepoll.h"
|
||||
|
||||
#ifdef HAVE_SYS_TYPES_H
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
#ifdef HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#ifdef HAVE_STRING_H
|
||||
#include <string.h>
|
||||
#endif
|
||||
#ifdef HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include "util.h"
|
||||
#include "log.h"
|
||||
|
||||
#ifndef USE_FAKE_POLL
|
||||
int
|
||||
tor_poll(struct pollfd *ufds, unsigned int nfds, int timeout)
|
||||
{
|
||||
unsigned int i;
|
||||
for (i=0;i<nfds;++i) {
|
||||
tor_assert(ufds[i].fd >= 0);
|
||||
}
|
||||
return poll(ufds,nfds,timeout);
|
||||
}
|
||||
#else
|
||||
int
|
||||
tor_poll(struct pollfd *ufds, unsigned int nfds, int timeout)
|
||||
{
|
||||
unsigned int idx;
|
||||
int maxfd, fd;
|
||||
int r;
|
||||
#ifdef MS_WINDOWS
|
||||
int any_fds_set = 0;
|
||||
#endif
|
||||
fd_set readfds, writefds, exceptfds;
|
||||
#ifdef USING_FAKE_TIMEVAL
|
||||
#undef timeval
|
||||
#undef tv_sec
|
||||
#undef tv_usec
|
||||
#endif
|
||||
struct timeval _timeout;
|
||||
_timeout.tv_sec = timeout/1000;
|
||||
_timeout.tv_usec = (timeout%1000)*1000;
|
||||
FD_ZERO(&readfds);
|
||||
FD_ZERO(&writefds);
|
||||
FD_ZERO(&exceptfds);
|
||||
|
||||
maxfd = -1;
|
||||
for (idx = 0; idx < nfds; ++idx) {
|
||||
ufds[idx].revents = 0;
|
||||
fd = ufds[idx].fd;
|
||||
tor_assert(SOCKET_IS_POLLABLE(fd));
|
||||
if (fd > maxfd) {
|
||||
maxfd = fd;
|
||||
#ifdef MS_WINDOWS
|
||||
any_fds_set = 1;
|
||||
#endif
|
||||
}
|
||||
if (ufds[idx].events & POLLIN)
|
||||
FD_SET(fd, &readfds);
|
||||
if (ufds[idx].events & POLLOUT)
|
||||
FD_SET(fd, &writefds);
|
||||
FD_SET(fd, &exceptfds);
|
||||
}
|
||||
#ifdef MS_WINDOWS
|
||||
if (!any_fds_set) {
|
||||
Sleep(timeout);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
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;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
/* Copyright 2002,2003 Nick Mathewson, Roger Dingledine. */
|
||||
/* See LICENSE for licensing information */
|
||||
/* $Id$ */
|
||||
|
||||
#ifndef __FAKEPOLL_H
|
||||
#define __FAKEPOLL_H
|
||||
#define FAKEPOLL_H_ID "$Id$"
|
||||
|
||||
/**
|
||||
* \file fakepoll.h
|
||||
* \brief Headers for fakepoll.c
|
||||
*/
|
||||
|
||||
#include "orconfig.h"
|
||||
|
||||
#define POLL_NO_WARN
|
||||
|
||||
#if defined(HAVE_POLL_H)
|
||||
#include <poll.h>
|
||||
#elif defined(HAVE_SYS_POLL_H)
|
||||
#include <sys/poll.h>
|
||||
#endif
|
||||
|
||||
/* If _POLL_EMUL_H_ is defined, then poll is just a just a thin wrapper around
|
||||
* select. On Mac OS 10.3, this wrapper is kinda flaky, and we should
|
||||
* use our own.
|
||||
*/
|
||||
#if !(defined(HAVE_POLL_H)||defined(HAVE_SYS_POLL_H))&&!defined(_POLL_EMUL_H_)
|
||||
#define USE_FAKE_POLL
|
||||
#endif
|
||||
|
||||
#if defined USE_FAKE_POLL && !defined(_POLL_EMUL_H_)
|
||||
struct pollfd {
|
||||
int fd;
|
||||
short events;
|
||||
short revents;
|
||||
};
|
||||
|
||||
#define POLLIN 0x0001
|
||||
#define POLLPRI 0x0002
|
||||
#define POLLOUT 0x0004
|
||||
#define POLLERR 0x0008
|
||||
#define POLLHUP 0x0010
|
||||
#define POLLNVAL 0x0020
|
||||
#endif
|
||||
|
||||
#ifdef MS_WINDOWS
|
||||
#define MAXCONNECTIONS 10000 /* XXXX copied from or.h */
|
||||
/* This trick makes winsock resize fd_set, which defaults to the insanely low
|
||||
* 64. */
|
||||
#define FD_SETSIZE MAXCONNECTIONS
|
||||
/* XXXX But Windows FD_SET and FD_CLR are tremendously ugly, and linear in
|
||||
* the total number of sockets set! Perhaps we should eventually use
|
||||
* WSAEventSelect and WSAWaitForMultipleEvents instead of select? */
|
||||
#endif
|
||||
|
||||
#if defined(MS_WINDOWS) || ! defined(USE_FAKE_POLL)
|
||||
/* If we're using poll, we can poll as many sockets as we want.
|
||||
* If we're on Windows, having too many sockets is harmless, since
|
||||
* select stupidly uses an array of sockets rather than a bitfield. */
|
||||
#define SOCKET_IS_POLLABLE(fd) ((fd) >= 0)
|
||||
#else
|
||||
/* If we're using a real Posix select, then in order to be pollable, a socket
|
||||
* must
|
||||
* a) be valid (>= 0)
|
||||
* b) be < FD_SETSIZE.
|
||||
*/
|
||||
#define SOCKET_IS_POLLABLE(fd) ((fd) >= 0 && (fd) < FD_SETSIZE)
|
||||
#endif
|
||||
|
||||
int tor_poll(struct pollfd *ufds, unsigned int nfds, int timeout);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user