1 /* 2 * Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 #ifndef _EVENT_H_ 28 #define _EVENT_H_ 29 30 #ifdef __cplusplus 31 extern "C" { 32 #endif 33 34 #include <stdarg.h> 35 36 #ifdef WIN32 37 #define WIN32_LEAN_AND_MEAN 38 #include <windows.h> 39 #undef WIN32_LEAN_AND_MEAN 40 typedef unsigned char u_char; 41 typedef unsigned short u_short; 42 #endif 43 44 #define EVLIST_TIMEOUT 0x01 45 #define EVLIST_INSERTED 0x02 46 #define EVLIST_SIGNAL 0x04 47 #define EVLIST_ACTIVE 0x08 48 #define EVLIST_INTERNAL 0x10 49 #define EVLIST_INIT 0x80 50 51 /* EVLIST_X_ Private space: 0x1000-0xf000 */ 52 #define EVLIST_ALL (0xf000 | 0x9f) 53 54 #define EV_TIMEOUT 0x01 55 #define EV_READ 0x02 56 #define EV_WRITE 0x04 57 #define EV_SIGNAL 0x08 58 #define EV_PERSIST 0x10 /* Persistant event */ 59 60 /* Fix so that ppl dont have to run with <sys/queue.h> */ 61 #ifndef TAILQ_ENTRY 62 #define _EVENT_DEFINED_TQENTRY 63 #define TAILQ_ENTRY(type) \ 64 struct { \ 65 struct type *tqe_next; /* next element */ \ 66 struct type **tqe_prev; /* address of previous next element */ \ 67 } 68 #endif /* !TAILQ_ENTRY */ 69 #ifndef RB_ENTRY 70 #define _EVENT_DEFINED_RBENTRY 71 #define RB_ENTRY(type) \ 72 struct { \ 73 struct type *rbe_left; /* left element */ \ 74 struct type *rbe_right; /* right element */ \ 75 struct type *rbe_parent; /* parent element */ \ 76 } 77 #endif /* !RB_ENTRY */ 78 79 struct event_base; 80 struct event { 81 TAILQ_ENTRY (event) ev_next; 82 TAILQ_ENTRY (event) ev_active_next; 83 TAILQ_ENTRY (event) ev_signal_next; 84 RB_ENTRY (event) ev_timeout_node; 85 86 struct event_base *ev_base; 87 int ev_fd; 88 short ev_events; 89 short ev_ncalls; 90 short *ev_pncalls; /* Allows deletes in callback */ 91 92 struct timeval ev_timeout; 93 94 int ev_pri; /* smaller numbers are higher priority */ 95 96 void (*ev_callback)(int, short, void *arg); 97 void *ev_arg; 98 99 int ev_res; /* result passed to event callback */ 100 int ev_flags; 101 }; 102 103 #define EVENT_SIGNAL(ev) (int)(ev)->ev_fd 104 #define EVENT_FD(ev) (int)(ev)->ev_fd 105 106 /* 107 * Key-Value pairs. Can be used for HTTP headers but also for 108 * query argument parsing. 109 */ 110 struct evkeyval { 111 TAILQ_ENTRY(evkeyval) next; 112 113 char *key; 114 char *value; 115 }; 116 117 #ifdef _EVENT_DEFINED_TQENTRY 118 #undef TAILQ_ENTRY 119 struct event_list; 120 struct evkeyvalq; 121 #undef _EVENT_DEFINED_TQENTRY 122 #else 123 TAILQ_HEAD (event_list, event); 124 TAILQ_HEAD (evkeyvalq, evkeyval); 125 #endif /* _EVENT_DEFINED_TQENTRY */ 126 #ifdef _EVENT_DEFINED_RBENTRY 127 #undef RB_ENTRY 128 #undef _EVENT_DEFINED_RBENTRY 129 #endif /* _EVENT_DEFINED_RBENTRY */ 130 131 struct eventop { 132 char *name; 133 void *(*init)(void); 134 int (*add)(void *, struct event *); 135 int (*del)(void *, struct event *); 136 int (*recalc)(struct event_base *, void *, int); 137 int (*dispatch)(struct event_base *, void *, struct timeval *); 138 void (*dealloc)(void *); 139 }; 140 141 #define TIMEOUT_DEFAULT {5, 0} 142 143 void *event_init(void); 144 int event_dispatch(void); 145 int event_base_dispatch(struct event_base *); 146 void event_base_free(struct event_base *); 147 148 #define _EVENT_LOG_DEBUG 0 149 #define _EVENT_LOG_MSG 1 150 #define _EVENT_LOG_WARN 2 151 #define _EVENT_LOG_ERR 3 152 typedef void (*event_log_cb)(int severity, const char *msg); 153 void event_set_log_callback(event_log_cb cb); 154 155 /* Associate a different event base with an event */ 156 int event_base_set(struct event_base *, struct event *); 157 158 #define EVLOOP_ONCE 0x01 159 #define EVLOOP_NONBLOCK 0x02 160 int event_loop(int); 161 int event_base_loop(struct event_base *, int); 162 int event_loopexit(struct timeval *); /* Causes the loop to exit */ 163 int event_base_loopexit(struct event_base *, struct timeval *); 164 165 #define evtimer_add(ev, tv) event_add(ev, tv) 166 #define evtimer_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg) 167 #define evtimer_del(ev) event_del(ev) 168 #define evtimer_pending(ev, tv) event_pending(ev, EV_TIMEOUT, tv) 169 #define evtimer_initialized(ev) ((ev)->ev_flags & EVLIST_INIT) 170 171 #define timeout_add(ev, tv) event_add(ev, tv) 172 #define timeout_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg) 173 #define timeout_del(ev) event_del(ev) 174 #define timeout_pending(ev, tv) event_pending(ev, EV_TIMEOUT, tv) 175 #define timeout_initialized(ev) ((ev)->ev_flags & EVLIST_INIT) 176 177 #define signal_add(ev, tv) event_add(ev, tv) 178 #define signal_set(ev, x, cb, arg) \ 179 event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg) 180 #define signal_del(ev) event_del(ev) 181 #define signal_pending(ev, tv) event_pending(ev, EV_SIGNAL, tv) 182 #define signal_initialized(ev) ((ev)->ev_flags & EVLIST_INIT) 183 184 void event_set(struct event *, int, short, void (*)(int, short, void *), void *); 185 int event_once(int, short, void (*)(int, short, void *), void *, struct timeval *); 186 187 int event_add(struct event *, struct timeval *); 188 int event_del(struct event *); 189 void event_active(struct event *, int, short); 190 191 int event_pending(struct event *, short, struct timeval *); 192 193 #ifdef WIN32 194 #define event_initialized(ev) ((ev)->ev_flags & EVLIST_INIT && (ev)->ev_fd != (int)INVALID_HANDLE_VALUE) 195 #else 196 #define event_initialized(ev) ((ev)->ev_flags & EVLIST_INIT) 197 #endif 198 199 /* Some simple debugging functions */ 200 const char *event_get_version(void); 201 const char *event_get_method(void); 202 203 /* These functions deal with event priorities */ 204 205 int event_priority_init(int); 206 int event_base_priority_init(struct event_base *, int); 207 int event_priority_set(struct event *, int); 208 209 /* These functions deal with buffering input and output */ 210 211 struct evbuffer { 212 u_char *buffer; 213 u_char *orig_buffer; 214 215 size_t misalign; 216 size_t totallen; 217 size_t off; 218 219 void (*cb)(struct evbuffer *, size_t, size_t, void *); 220 void *cbarg; 221 }; 222 223 /* Just for error reporting - use other constants otherwise */ 224 #define EVBUFFER_READ 0x01 225 #define EVBUFFER_WRITE 0x02 226 #define EVBUFFER_EOF 0x10 227 #define EVBUFFER_ERROR 0x20 228 #define EVBUFFER_TIMEOUT 0x40 229 230 struct bufferevent; 231 typedef void (*evbuffercb)(struct bufferevent *, void *); 232 typedef void (*everrorcb)(struct bufferevent *, short what, void *); 233 234 struct event_watermark { 235 size_t low; 236 size_t high; 237 }; 238 239 struct bufferevent { 240 struct event ev_read; 241 struct event ev_write; 242 243 struct evbuffer *input; 244 struct evbuffer *output; 245 246 struct event_watermark wm_read; 247 struct event_watermark wm_write; 248 249 evbuffercb readcb; 250 evbuffercb writecb; 251 everrorcb errorcb; 252 void *cbarg; 253 254 int timeout_read; /* in seconds */ 255 int timeout_write; /* in seconds */ 256 257 short enabled; /* events that are currently enabled */ 258 }; 259 260 struct bufferevent *bufferevent_new(int fd, 261 evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg); 262 int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev); 263 int bufferevent_priority_set(struct bufferevent *bufev, int pri); 264 void bufferevent_free(struct bufferevent *bufev); 265 int bufferevent_write(struct bufferevent *bufev, void *data, size_t size); 266 int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf); 267 size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size); 268 int bufferevent_enable(struct bufferevent *bufev, short event); 269 int bufferevent_disable(struct bufferevent *bufev, short event); 270 void bufferevent_settimeout(struct bufferevent *bufev, 271 int timeout_read, int timeout_write); 272 273 #define EVBUFFER_LENGTH(x) (x)->off 274 #define EVBUFFER_DATA(x) (x)->buffer 275 #define EVBUFFER_INPUT(x) (x)->input 276 #define EVBUFFER_OUTPUT(x) (x)->output 277 278 struct evbuffer *evbuffer_new(void); 279 void evbuffer_free(struct evbuffer *); 280 int evbuffer_expand(struct evbuffer *, size_t); 281 int evbuffer_add(struct evbuffer *, const void *, size_t); 282 int evbuffer_remove(struct evbuffer *, void *, size_t); 283 char *evbuffer_readline(struct evbuffer *); 284 int evbuffer_add_buffer(struct evbuffer *, struct evbuffer *); 285 int evbuffer_add_printf(struct evbuffer *, const char *fmt, ...); 286 int evbuffer_add_vprintf(struct evbuffer *, const char *fmt, va_list ap); 287 void evbuffer_drain(struct evbuffer *, size_t); 288 int evbuffer_write(struct evbuffer *, int); 289 int evbuffer_read(struct evbuffer *, int, int); 290 u_char *evbuffer_find(struct evbuffer *, const u_char *, size_t); 291 void evbuffer_setcb(struct evbuffer *, void (*)(struct evbuffer *, size_t, size_t, void *), void *); 292 293 /* 294 * Marshaling tagged data - We assume that all tags are inserted in their 295 * numeric order - so that unknown tags will always be higher than the 296 * known ones - and we can just ignore the end of an event buffer. 297 */ 298 299 void evtag_init(void); 300 301 void evtag_marshal(struct evbuffer *evbuf, u_int8_t tag, const void *data, 302 u_int32_t len); 303 304 void encode_int(struct evbuffer *evbuf, u_int32_t number); 305 306 void evtag_marshal_int(struct evbuffer *evbuf, u_int8_t tag, 307 u_int32_t integer); 308 309 void evtag_marshal_string(struct evbuffer *buf, u_int8_t tag, 310 const char *string); 311 312 void evtag_marshal_timeval(struct evbuffer *evbuf, u_int8_t tag, 313 struct timeval *tv); 314 315 void evtag_test(void); 316 317 int evtag_unmarshal(struct evbuffer *src, u_int8_t *ptag, 318 struct evbuffer *dst); 319 int evtag_peek(struct evbuffer *evbuf, u_int8_t *ptag); 320 int evtag_peek_length(struct evbuffer *evbuf, u_int32_t *plength); 321 int evtag_payload_length(struct evbuffer *evbuf, u_int32_t *plength); 322 int evtag_consume(struct evbuffer *evbuf); 323 324 int evtag_unmarshal_int(struct evbuffer *evbuf, u_int8_t need_tag, 325 u_int32_t *pinteger); 326 327 int evtag_unmarshal_fixed(struct evbuffer *src, u_int8_t need_tag, void *data, 328 size_t len); 329 330 int evtag_unmarshal_string(struct evbuffer *evbuf, u_int8_t need_tag, 331 char **pstring); 332 333 int evtag_unmarshal_timeval(struct evbuffer *evbuf, u_int8_t need_tag, 334 struct timeval *ptv); 335 336 #ifdef __cplusplus 337 } 338 #endif 339 340 #endif /* _EVENT_H_ */ 341