From 2c478454e9a3a21bdc62c7f46b02149795888437 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 3 Nov 2007 19:41:55 +0000 Subject: try to programmatically integrate libevent --- ev.c | 27 +++---- ev.h | 2 +- evdns.c | 11 +-- event.c | 10 ++- event.h | 12 ++- event_compat.h | 225 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 261 insertions(+), 26 deletions(-) create mode 100644 event_compat.h diff --git a/ev.c b/ev.c index 3e1e8b4..987b39c 100644 --- a/ev.c +++ b/ev.c @@ -28,7 +28,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#if EV_USE_CONFIG_H +#ifndef EV_STANDALONE # include "config.h" #endif @@ -177,7 +177,7 @@ get_clock (void) typedef struct { - struct ev_io *head; + struct ev_watcher_list *head; unsigned char events; unsigned char reify; } ANFD; @@ -237,7 +237,7 @@ fd_event (int fd, int events) ANFD *anfd = anfds + fd; struct ev_io *w; - for (w = anfd->head; w; w = w->next) + for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) { int ev = w->events & events; @@ -264,7 +264,7 @@ fd_reify (void) int events = 0; - for (w = anfd->head; w; w = w->next) + for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) events |= w->events; anfd->reify = 0; @@ -297,8 +297,7 @@ fd_kill (int fd) { struct ev_io *w; - printf ("killing fd %d\n", fd);//D - while ((w = anfds [fd].head)) + while ((w = (struct ev_io *)anfds [fd].head)) { ev_io_stop (w); event ((W)w, EV_ERROR | EV_READ | EV_WRITE); @@ -385,7 +384,7 @@ downheap (WT *timers, int N, int k) typedef struct { - struct ev_signal *head; + struct ev_watcher_list *head; sig_atomic_t volatile gotsig; } ANSIG; @@ -425,7 +424,7 @@ sighandler (int signum) static void sigcb (struct ev_io *iow, int revents) { - struct ev_signal *w; + struct ev_watcher_list *w; int signum; read (sigpipe [0], &revents, 1); @@ -484,13 +483,12 @@ child_reap (struct ev_signal *sw, int chain, int pid, int status) { struct ev_child *w; - for (w = childs [chain & (PID_HASHSIZE - 1)]; w; w = w->next) + for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) if (w->pid == pid || !w->pid) { w->priority = sw->priority; /* need to do it *now* */ w->rpid = pid; w->rstatus = status; - printf ("rpid %p %d %d\n", w, pid, w->pid);//D event ((W)w, EV_CHILD); } } @@ -500,7 +498,6 @@ childcb (struct ev_signal *sw, int revents) { int pid, status; - printf ("chld %x\n", revents);//D if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) { /* make sure we are called again until all childs have been reaped */ @@ -573,10 +570,10 @@ int ev_init (int methods) return 0; if (methods == EVMETHOD_AUTO) - if (!enable_secure () && getenv ("LIBEV_METHODS")) - methods = atoi (getenv ("LIBEV_METHODS")); - else - methods = EVMETHOD_ANY; + if (!enable_secure () && getenv ("LIBEV_METHODS")) + methods = atoi (getenv ("LIBEV_METHODS")); + else + methods = EVMETHOD_ANY; ev_method = 0; #if EV_USE_KQUEUE diff --git a/ev.h b/ev.h index 1e88f02..bcb18e4 100644 --- a/ev.h +++ b/ev.h @@ -86,7 +86,7 @@ typedef double ev_tstamp; #define EV_WATCHER_LIST(type) \ EV_WATCHER (type); \ - struct type *next /* private */ + struct ev_watcher_list *next /* private */ #define EV_WATCHER_TIME(type) \ EV_WATCHER (type); \ diff --git a/evdns.c b/evdns.c index 78fbd3c..3224cce 100644 --- a/evdns.c +++ b/evdns.c @@ -1,5 +1,4 @@ -#define DNS_USE_GETTIMEOFDAY_FOR_ID 1 -#define HAVE_STRUCT_IN6_ADDR 1 +/* $Id: evdns.c,v 1.6 2007-11-03 19:41:55 root Exp $ */ /* The original version of this module was written by Adam Langley; for * a history of modifications, check out the subversion logs. @@ -41,6 +40,7 @@ #endif #ifdef WIN32 +#include "misc.h" #endif /* #define NDEBUG */ @@ -94,6 +94,7 @@ #include #include "evdns.h" +#include "log.h" #ifdef WIN32 #include #include @@ -2546,7 +2547,7 @@ evdns_resolv_set_defaults(int flags) { #ifndef HAVE_STRTOK_R static char * -fake_strtok_r(char *s, const char *delim, char **state) { +strtok_r(char *s, const char *delim, char **state) { return strtok(s, delim); } #endif @@ -2622,9 +2623,9 @@ static void resolv_conf_parse_line(char *const start, int flags) { char *strtok_state; static const char *const delims = " \t"; -#define NEXT_TOKEN fake_strtok_r(NULL, delims, &strtok_state) +#define NEXT_TOKEN strtok_r(NULL, delims, &strtok_state) - char *const first_token = fake_strtok_r(start, delims, &strtok_state); + char *const first_token = strtok_r(start, delims, &strtok_state); if (!first_token) return; if (!strcmp(first_token, "nameserver") && (flags & DNS_OPTION_NAMESERVERS)) { diff --git a/event.c b/event.c index 490e9c2..0c53e2f 100644 --- a/event.c +++ b/event.c @@ -90,10 +90,12 @@ int event_dispatch (void) return event_base_dispatch (x_cur); } +#ifdef EV_STANDALONE void event_set_log_callback (event_log_cb cb) { /* nop */ } +#endif int event_loop (int flags) { @@ -102,7 +104,7 @@ int event_loop (int flags) int event_loopexit (struct timeval *tv) { - event_base_loopexit (x_cur, tv); + return event_base_loopexit (x_cur, tv); } static void @@ -174,7 +176,7 @@ void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, v int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) { - event_base_once (x_cur, fd, events, cb, arg, tv); + return event_base_once (x_cur, fd, events, cb, arg, tv); } int event_add (struct event *ev, struct timeval *tv) @@ -237,7 +239,7 @@ int event_del (struct event *ev) int event_pending (struct event *ev, short events, struct timeval *tv) { - short revents; + short revents = 0; if (ev->ev_events & EV_SIGNAL) { @@ -309,6 +311,8 @@ int event_base_loopexit (struct event_base *base, struct timeval *tv) ev_tstamp after = tv_get (tv); ev_once (-1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); + + return -1; } struct x_once diff --git a/event.h b/event.h index 3c2f448..1bc88d2 100644 --- a/event.h +++ b/event.h @@ -31,12 +31,12 @@ #ifndef _EVENT_H_ #define _EVENT_H_ +#include "ev.h" + #ifdef __cplusplus extern "C" { #endif -#include "ev.h" - struct event { /* libev watchers we map onto */ @@ -119,5 +119,13 @@ int event_base_dispatch (struct event_base *base); int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv); int event_base_priority_init (struct event_base *base, int fd); +#ifndef EV_STANDALONE +# include "event_compat.h" +#endif + +#ifdef __cplusplus +} +#endif + #endif diff --git a/event_compat.h b/event_compat.h new file mode 100644 index 0000000..258a83f --- /dev/null +++ b/event_compat.h @@ -0,0 +1,225 @@ +/* + * Copyright (c) 2000-2004 Niels Provos + * 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. + */ +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +#ifdef WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#undef WIN32_LEAN_AND_MEAN +typedef unsigned char u_char; +typedef unsigned short u_short; +#endif + +/* Fix so that ppl dont have to run with */ +#ifndef TAILQ_ENTRY +#define _EVENT_DEFINED_TQENTRY +#define TAILQ_ENTRY(type) \ +struct { \ + struct type *tqe_next; /* next element */ \ + struct type **tqe_prev; /* address of previous next element */ \ +} +#endif /* !TAILQ_ENTRY */ +#ifndef RB_ENTRY +#define _EVENT_DEFINED_RBENTRY +#define RB_ENTRY(type) \ +struct { \ + struct type *rbe_left; /* left element */ \ + struct type *rbe_right; /* right element */ \ + struct type *rbe_parent; /* parent element */ \ + int rbe_color; /* node color */ \ +} +#endif /* !RB_ENTRY */ + +/* + * Key-Value pairs. Can be used for HTTP headers but also for + * query argument parsing. + */ +struct evkeyval { + TAILQ_ENTRY(evkeyval) next; + + char *key; + char *value; +}; + +#ifdef _EVENT_DEFINED_TQENTRY +#undef TAILQ_ENTRY +struct event_list; +struct evkeyvalq; +#undef _EVENT_DEFINED_TQENTRY +#else +TAILQ_HEAD (event_list, event); +TAILQ_HEAD (evkeyvalq, evkeyval); +#endif /* _EVENT_DEFINED_TQENTRY */ +#ifdef _EVENT_DEFINED_RBENTRY +#undef RB_ENTRY +#undef _EVENT_DEFINED_RBENTRY +#endif /* _EVENT_DEFINED_RBENTRY */ + +struct eventop { + char *name; + void *(*init)(struct event_base *); + int (*add)(void *, struct event *); + int (*del)(void *, struct event *); + int (*recalc)(struct event_base *, void *, int); + int (*dispatch)(struct event_base *, void *, struct timeval *); + void (*dealloc)(struct event_base *, void *); +}; + +/* These functions deal with buffering input and output */ + +struct evbuffer { + u_char *buffer; + u_char *orig_buffer; + + size_t misalign; + size_t totallen; + size_t off; + + void (*cb)(struct evbuffer *, size_t, size_t, void *); + void *cbarg; +}; + +/* Just for error reporting - use other constants otherwise */ +#define EVBUFFER_READ 0x01 +#define EVBUFFER_WRITE 0x02 +#define EVBUFFER_EOF 0x10 +#define EVBUFFER_ERROR 0x20 +#define EVBUFFER_TIMEOUT 0x40 + +struct bufferevent; +typedef void (*evbuffercb)(struct bufferevent *, void *); +typedef void (*everrorcb)(struct bufferevent *, short what, void *); + +struct event_watermark { + size_t low; + size_t high; +}; + +struct bufferevent { + struct event ev_read; + struct event ev_write; + + struct evbuffer *input; + struct evbuffer *output; + + struct event_watermark wm_read; + struct event_watermark wm_write; + + evbuffercb readcb; + evbuffercb writecb; + everrorcb errorcb; + void *cbarg; + + int timeout_read; /* in seconds */ + int timeout_write; /* in seconds */ + + short enabled; /* events that are currently enabled */ +}; + +struct bufferevent *bufferevent_new(int fd, + evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg); +int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev); +int bufferevent_priority_set(struct bufferevent *bufev, int pri); +void bufferevent_free(struct bufferevent *bufev); +int bufferevent_write(struct bufferevent *bufev, void *data, size_t size); +int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf); +size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size); +int bufferevent_enable(struct bufferevent *bufev, short event); +int bufferevent_disable(struct bufferevent *bufev, short event); +void bufferevent_settimeout(struct bufferevent *bufev, + int timeout_read, int timeout_write); + +#define EVBUFFER_LENGTH(x) (x)->off +#define EVBUFFER_DATA(x) (x)->buffer +#define EVBUFFER_INPUT(x) (x)->input +#define EVBUFFER_OUTPUT(x) (x)->output + +struct evbuffer *evbuffer_new(void); +void evbuffer_free(struct evbuffer *); +int evbuffer_expand(struct evbuffer *, size_t); +int evbuffer_add(struct evbuffer *, const void *, size_t); +int evbuffer_remove(struct evbuffer *, void *, size_t); +char *evbuffer_readline(struct evbuffer *); +int evbuffer_add_buffer(struct evbuffer *, struct evbuffer *); +int evbuffer_add_printf(struct evbuffer *, const char *fmt, ...); +int evbuffer_add_vprintf(struct evbuffer *, const char *fmt, va_list ap); +void evbuffer_drain(struct evbuffer *, size_t); +int evbuffer_write(struct evbuffer *, int); +int evbuffer_read(struct evbuffer *, int, int); +u_char *evbuffer_find(struct evbuffer *, const u_char *, size_t); +void evbuffer_setcb(struct evbuffer *, void (*)(struct evbuffer *, size_t, size_t, void *), void *); + +/* + * Marshaling tagged data - We assume that all tags are inserted in their + * numeric order - so that unknown tags will always be higher than the + * known ones - and we can just ignore the end of an event buffer. + */ + +void evtag_init(void); + +void evtag_marshal(struct evbuffer *evbuf, uint8_t tag, const void *data, + uint32_t len); + +void encode_int(struct evbuffer *evbuf, uint32_t number); + +void evtag_marshal_int(struct evbuffer *evbuf, uint8_t tag, uint32_t integer); + +void evtag_marshal_string(struct evbuffer *buf, uint8_t tag, + const char *string); + +void evtag_marshal_timeval(struct evbuffer *evbuf, uint8_t tag, + struct timeval *tv); + +void evtag_test(void); + +int evtag_unmarshal(struct evbuffer *src, uint8_t *ptag, struct evbuffer *dst); +int evtag_peek(struct evbuffer *evbuf, uint8_t *ptag); +int evtag_peek_length(struct evbuffer *evbuf, uint32_t *plength); +int evtag_payload_length(struct evbuffer *evbuf, uint32_t *plength); +int evtag_consume(struct evbuffer *evbuf); + +int evtag_unmarshal_int(struct evbuffer *evbuf, uint8_t need_tag, + uint32_t *pinteger); + +int evtag_unmarshal_fixed(struct evbuffer *src, uint8_t need_tag, void *data, + size_t len); + +int evtag_unmarshal_string(struct evbuffer *evbuf, uint8_t need_tag, + char **pstring); + +int evtag_unmarshal_timeval(struct evbuffer *evbuf, uint8_t need_tag, + struct timeval *ptv); + +#ifdef __cplusplus +} +#endif -- cgit v1.2.3