[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] Commit initial libevent sandbox to the tor project reposito...



Update of /home/or/cvsroot/libevent/WIN32-Code
In directory moria.mit.edu:/tmp/cvs-serv18110/WIN32-Code

Added Files:
	config.h misc.c misc.h win32.c 
Log Message:
Commit initial libevent sandbox to the tor project repository (version 1.0b)

--- NEW FILE: config.h ---
/* config.h.  Generated by configure.  */
/* config.h.in.  Generated from configure.in by autoheader.  */
/* Define if kqueue works correctly with pipes */
/* #undef HAVE_WORKING_KQUEUE */

/* Define to `unsigned long long' if <sys/types.h> doesn't define.  */
/* #undef u_int64_t */

/* Define to `unsigned int' if <sys/types.h> doesn't define.  */
/* #undef u_int32_t */

/* Define to `unsigned short' if <sys/types.h> doesn't define.  */
/* #undef u_int16_t */

/* Define to `unsigned char' if <sys/types.h> doesn't define.  */
/* #undef u_int8_t */

/* Define if timeradd is defined in <sys/time.h> */
/* #undef HAVE_TIMERADD */
#ifndef HAVE_TIMERADD
#define timeradd(tvp, uvp, vvp)           \
  do {                \
    (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;    \
    (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;       \
    if ((vvp)->tv_usec >= 1000000) {      \
      (vvp)->tv_sec++;        \
      (vvp)->tv_usec -= 1000000;      \
    }             \
  } while (0)
#define timersub(tvp, uvp, vvp)           \
  do {                \
    (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;    \
    (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
    if ((vvp)->tv_usec < 0) {       \
      (vvp)->tv_sec--;        \
      (vvp)->tv_usec += 1000000;      \
    }             \
  } while (0)
#endif /* !HAVE_TIMERADD */

/* Define if TAILQ_FOREACH is defined in <sys/queue.h> */
#define HAVE_TAILQFOREACH 1
#ifndef HAVE_TAILQFOREACH
#define TAILQ_FIRST(head)   ((head)->tqh_first)
#define TAILQ_END(head)     NULL
#define TAILQ_NEXT(elm, field)    ((elm)->field.tqe_next)
#define TAILQ_FOREACH(var, head, field)         \
  for((var) = TAILQ_FIRST(head);          \
      (var) != TAILQ_END(head);         \
      (var) = TAILQ_NEXT(var, field))
#define TAILQ_INSERT_BEFORE(listelm, elm, field) do {     \
  (elm)->field.tqe_prev = (listelm)->field.tqe_prev;    \
  (elm)->field.tqe_next = (listelm);        \
  *(listelm)->field.tqe_prev = (elm);       \
  (listelm)->field.tqe_prev = &(elm)->field.tqe_next;   \
} while (0)
#endif /* TAILQ_FOREACH */
/* Define to 1 if you have the `gettimeofday' function. */
#define HAVE_GETTIMEOFDAY 1

/* Define to 1 if you have the <inttypes.h> header file. */
#define HAVE_INTTYPES_H 1

/* Define to 1 if you have the `kqueue' function. */
/* #undef HAVE_KQUEUE */

/* Define to 1 if you have the <memory.h> header file. */
#define HAVE_MEMORY_H 1

/* Define to 1 if you have the `select' function. */
/* #undef HAVE_SELECT */

/* Define to 1 if you have the <stdint.h> header file. */
/* #undef HAVE_STDINT_H */

/* Define to 1 if you have the <stdlib.h> header file. */
#define HAVE_STDLIB_H 1

/* Define to 1 if you have the <strings.h> header file. */
#define HAVE_STRINGS_H 1

/* Define to 1 if you have the <string.h> header file. */
#define HAVE_STRING_H 1

/* Define to 1 if you have the <sys/event.h> header file. */
#define HAVE_SYS_EVENT_H 1

/* Define to 1 if you have the <sys/queue.h> header file. */
#define HAVE_SYS_QUEUE_H 1

/* Define to 1 if you have the <sys/stat.h> header file. */
#define HAVE_SYS_STAT_H 1

/* Define to 1 if you have the <sys/time.h> header file. */
/* #undef HAVE_SYS_TIME_H */

/* Define to 1 if you have the <sys/types.h> header file. */
#define HAVE_SYS_TYPES_H 1

/* Define to 1 if you have the <unistd.h> header file. */
/* #undef HAVE_UNISTD_H */

/* Define to 1 if you have the `warnx' function. */
#define HAVE_WARNX 1

/* Name of package */
#define PACKAGE "libevent"

/* Define to the address where bug reports for this package should be sent. */
#define PACKAGE_BUGREPORT ""

/* Define to the full name of this package. */
#define PACKAGE_NAME ""

/* Define to the full name and version of this package. */
#define PACKAGE_STRING ""

/* Define to the one symbol short name of this package. */
#define PACKAGE_TARNAME ""

/* Define to the version of this package. */
#define PACKAGE_VERSION ""

/* Define to 1 if you have the ANSI C header files. */
#define STDC_HEADERS 1

/* Define to 1 if you can safely include both <sys/time.h> and <time.h>. */
#define TIME_WITH_SYS_TIME 1

/* Version number of package */
#define VERSION "0.6"

typedef unsigned char u_char;

/* Winsock compatibility */
#define SHUT_WR SD_SEND

struct timeval;
struct timezone;
int gettimeofday(struct timeval *tv, struct timezone *tz);

#define read win_read
#define write win_write

int win_read(int, void *, unsigned int);
int win_write(int, void *, unsigned int);
int socketpair(int d, int type, int protocol, int *sv);

#define __func__ __FILE__
--- NEW FILE: misc.c ---
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <sys/timeb.h>
#include <time.h>

/****************************************************************************
 *
 * Function: gettimeofday(struct timeval *, struct timezone *)
 *
 * Purpose:  Get current time of day.
 *
 * Arguments: tv => Place to store the curent time of day.
 *            tz => Ignored.
 *
 * Returns: 0 => Success.
 *
 ****************************************************************************/

int gettimeofday(struct timeval *tv, struct timezone *tz) {
  struct _timeb tb;

	if(tv == NULL)
		return -1;

	_ftime(&tb);
	tv->tv_sec = tb.time;
	tv->tv_usec = ((int) tb.millitm) * 1000;
	return 0;
}

int
win_read(int fd, void *buf, unsigned int length)
{
	DWORD dwBytesRead;
	int res = ReadFile((HANDLE) fd, buf, length, &dwBytesRead, NULL);
	if (res == 0) {
		DWORD error = GetLastError();
		if (error == ERROR_NO_DATA)
			return (0);
		return (-1);
	} else
		return (dwBytesRead);
}

int
win_write(int fd, void *buf, unsigned int length)
{
	DWORD dwBytesWritten;
	int res = WriteFile((HANDLE) fd, buf, length, &dwBytesWritten, NULL);
	if (res == 0) {
		DWORD error = GetLastError();
		if (error == ERROR_NO_DATA)
			return (0);
		return (-1);
	} else
		return (dwBytesWritten);
}

int
socketpair(int d, int type, int protocol, int *sv)
{
	static int count;
	char buf[64];
	HANDLE fd;
	DWORD dwMode;
	sprintf(buf, "\\\\.\\pipe\\levent-%d", count++);
	/* Create a duplex pipe which will behave like a socket pair */
	fd = CreateNamedPipe(buf, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_NOWAIT, 
		PIPE_UNLIMITED_INSTANCES, 4096, 4096, 0, NULL);
	if (fd == INVALID_HANDLE_VALUE)
		return (-1);
	sv[0] = (int)fd;

	fd = CreateFile(buf, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (fd == INVALID_HANDLE_VALUE)
		return (-1);
	dwMode = PIPE_NOWAIT;
	SetNamedPipeHandleState(fd, &dwMode, NULL, NULL);
	sv[1] = (int)fd;

	return (0);
}
--- NEW FILE: misc.h ---
#ifndef MISC_H
#define MISC_H

int gettimeofday(struct timeval *,struct timezone *);

#endif

--- NEW FILE: win32.c ---
/*
 * Copyright 2000-2002 Niels Provos <provos@xxxxxxxxxxxxxx>
 * Copyright 2003 Michael A. Davis <mike@xxxxxxxxxxxxx>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "config.h"

#include <windows.h>
#include <sys/types.h>
#include <sys/queue.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <err.h>

#ifdef USE_LOG
#include "log.h"
#else
#define LOG_DBG(x)
#define log_error(x)	perror(x)
#endif

#include "event.h"

extern struct event_list timequeue;
extern struct event_list eventqueue;
extern struct event_list addqueue;
extern struct event_list signalqueue;

#define NEVENT		64

int evsigcaught[NSIG];
volatile sig_atomic_t signal_caught = 0;
/* MSDN says this is required to handle SIGFPE */
volatile double SIGFPE_REQ = 0.0f; 

int signal_handler(int sig);
void signal_process(void);
int signal_recalc(void);

void *win32_init	(void);
int win32_insert	(void *, struct event *);
int win32_del	(void *, struct event *);
int win32_recalc	(void *, int);
int win32_dispatch	(void *, struct timeval *);

struct eventop win32ops = {
	"win32",
	win32_init,
	win32_insert,
	win32_del,
	win32_recalc,
	win32_dispatch
};

static int timeval_to_ms(struct timeval *tv)
{
	return ((tv->tv_sec * 1000) + (tv->tv_usec / 1000));
}

void *
win32_init(void)
{
	return (&win32ops);
}

int
win32_recalc(void *arg, int max)
{
	return (signal_recalc());
}

int
win32_insert(struct win32op *wop, struct event *ev)
{
	if (ev->ev_events & EV_SIGNAL) {
		if (ev->ev_events & (EV_READ|EV_WRITE))
			errx(1, "%s: EV_SIGNAL incompatible use",
			    __func__);
		if((int)signal(EVENT_SIGNAL(ev), signal_handler) == -1)
			return (-1);

		return (0);
	}

	return (0);
}

int
win32_dispatch(void *arg, struct timeval *tv)
{
	int res = 0;
	struct win32op *wop = arg;
	struct event *ev;
	int evres;

	TAILQ_FOREACH(ev, &eventqueue, ev_next) {
		res = WaitForSingleObject(ev->ev_fd, timeval_to_ms(tv));

		if(res == WAIT_TIMEOUT || res == WAIT_FAILED) {
			signal_process();
			return (0);
		} else if (signal_caught)
			signal_process();

		evres = 0;
		if(ev->ev_events & EV_READ)
			evres |= EV_READ;

		if(ev->ev_events & EV_WRITE)
			evres |= EV_WRITE;
		if(evres) {
			if(!(ev->ev_events & EV_PERSIST))
				event_del(ev);
			event_active(ev, evres, 1);
		}
	}

	if (signal_recalc() == -1)
		return (-1);

	return (0);
}

int
win32_del(struct win32op *arg, struct event *ev)
{
	return ((int)signal(EVENT_SIGNAL(ev), SIG_IGN));
}

static int signal_handler(int sig)
{
	evsigcaught[sig]++;
	signal_caught = 1;

	return 0;
}

int
signal_recalc(void)
{
	struct event *ev;

	/* Reinstall our signal handler. */
	TAILQ_FOREACH(ev, &signalqueue, ev_signal_next) {
		if((int)signal(EVENT_SIGNAL(ev), signal_handler) == -1)
			return (-1);
	}
	return (0);
}

void
signal_process(void)
{
	struct event *ev;
	short ncalls;

	TAILQ_FOREACH(ev, &signalqueue, ev_signal_next) {
		ncalls = evsigcaught[EVENT_SIGNAL(ev)];
		if (ncalls) {
			if (!(ev->ev_events & EV_PERSIST))
				event_del(ev);
			event_active(ev, EV_SIGNAL, ncalls);
		}
	}

	memset(evsigcaught, 0, sizeof(evsigcaught));
	signal_caught = 0;
}