xref: /freebsd/contrib/pf/libevent/event.h (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
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 	int rbe_color;			/* node color */		\
77 }
78 #endif /* !RB_ENTRY */
79 
80 struct event_base;
81 struct event {
82 	TAILQ_ENTRY (event) ev_next;
83 	TAILQ_ENTRY (event) ev_active_next;
84 	TAILQ_ENTRY (event) ev_signal_next;
85 	RB_ENTRY (event) ev_timeout_node;
86 
87 	struct event_base *ev_base;
88 	int ev_fd;
89 	short ev_events;
90 	short ev_ncalls;
91 	short *ev_pncalls;	/* Allows deletes in callback */
92 
93 	struct timeval ev_timeout;
94 
95 	int ev_pri;		/* smaller numbers are higher priority */
96 
97 	void (*ev_callback)(int, short, void *arg);
98 	void *ev_arg;
99 
100 	int ev_res;		/* result passed to event callback */
101 	int ev_flags;
102 };
103 
104 #define EVENT_SIGNAL(ev)	(int)(ev)->ev_fd
105 #define EVENT_FD(ev)		(int)(ev)->ev_fd
106 
107 /*
108  * Key-Value pairs.  Can be used for HTTP headers but also for
109  * query argument parsing.
110  */
111 struct evkeyval {
112 	TAILQ_ENTRY(evkeyval) next;
113 
114 	char *key;
115 	char *value;
116 };
117 
118 #ifdef _EVENT_DEFINED_TQENTRY
119 #undef TAILQ_ENTRY
120 struct event_list;
121 struct evkeyvalq;
122 #undef _EVENT_DEFINED_TQENTRY
123 #else
124 TAILQ_HEAD (event_list, event);
125 TAILQ_HEAD (evkeyvalq, evkeyval);
126 #endif /* _EVENT_DEFINED_TQENTRY */
127 #ifdef _EVENT_DEFINED_RBENTRY
128 #undef RB_ENTRY
129 #undef _EVENT_DEFINED_RBENTRY
130 #endif /* _EVENT_DEFINED_RBENTRY */
131 
132 struct eventop {
133 	char *name;
134 	void *(*init)(void);
135 	int (*add)(void *, struct event *);
136 	int (*del)(void *, struct event *);
137 	int (*recalc)(struct event_base *, void *, int);
138 	int (*dispatch)(struct event_base *, void *, struct timeval *);
139 	void (*dealloc)(void *);
140 };
141 
142 #define TIMEOUT_DEFAULT	{5, 0}
143 
144 void *event_init(void);
145 int event_dispatch(void);
146 int event_base_dispatch(struct event_base *);
147 void event_base_free(struct event_base *);
148 
149 #define _EVENT_LOG_DEBUG 0
150 #define _EVENT_LOG_MSG   1
151 #define _EVENT_LOG_WARN  2
152 #define _EVENT_LOG_ERR   3
153 typedef void (*event_log_cb)(int severity, const char *msg);
154 void event_set_log_callback(event_log_cb cb);
155 
156 /* Associate a different event base with an event */
157 int event_base_set(struct event_base *, struct event *);
158 
159 #define EVLOOP_ONCE	0x01
160 #define EVLOOP_NONBLOCK	0x02
161 int event_loop(int);
162 int event_base_loop(struct event_base *, int);
163 int event_loopexit(struct timeval *);	/* Causes the loop to exit */
164 int event_base_loopexit(struct event_base *, struct timeval *);
165 
166 #define evtimer_add(ev, tv)		event_add(ev, tv)
167 #define evtimer_set(ev, cb, arg)	event_set(ev, -1, 0, cb, arg)
168 #define evtimer_del(ev)			event_del(ev)
169 #define evtimer_pending(ev, tv)		event_pending(ev, EV_TIMEOUT, tv)
170 #define evtimer_initialized(ev)		((ev)->ev_flags & EVLIST_INIT)
171 
172 #define timeout_add(ev, tv)		event_add(ev, tv)
173 #define timeout_set(ev, cb, arg)	event_set(ev, -1, 0, cb, arg)
174 #define timeout_del(ev)			event_del(ev)
175 #define timeout_pending(ev, tv)		event_pending(ev, EV_TIMEOUT, tv)
176 #define timeout_initialized(ev)		((ev)->ev_flags & EVLIST_INIT)
177 
178 #define signal_add(ev, tv)		event_add(ev, tv)
179 #define signal_set(ev, x, cb, arg)	\
180 	event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg)
181 #define signal_del(ev)			event_del(ev)
182 #define signal_pending(ev, tv)		event_pending(ev, EV_SIGNAL, tv)
183 #define signal_initialized(ev)		((ev)->ev_flags & EVLIST_INIT)
184 
185 void event_set(struct event *, int, short, void (*)(int, short, void *), void *);
186 int event_once(int, short, void (*)(int, short, void *), void *, struct timeval *);
187 
188 int event_add(struct event *, struct timeval *);
189 int event_del(struct event *);
190 void event_active(struct event *, int, short);
191 
192 int event_pending(struct event *, short, struct timeval *);
193 
194 #ifdef WIN32
195 #define event_initialized(ev)		((ev)->ev_flags & EVLIST_INIT && (ev)->ev_fd != (int)INVALID_HANDLE_VALUE)
196 #else
197 #define event_initialized(ev)		((ev)->ev_flags & EVLIST_INIT)
198 #endif
199 
200 /* Some simple debugging functions */
201 const char *event_get_version(void);
202 const char *event_get_method(void);
203 
204 /* These functions deal with event priorities */
205 
206 int	event_priority_init(int);
207 int	event_base_priority_init(struct event_base *, int);
208 int	event_priority_set(struct event *, int);
209 
210 /* These functions deal with buffering input and output */
211 
212 struct evbuffer {
213 	u_char *buffer;
214 	u_char *orig_buffer;
215 
216 	size_t misalign;
217 	size_t totallen;
218 	size_t off;
219 
220 	void (*cb)(struct evbuffer *, size_t, size_t, void *);
221 	void *cbarg;
222 };
223 
224 /* Just for error reporting - use other constants otherwise */
225 #define EVBUFFER_READ		0x01
226 #define EVBUFFER_WRITE		0x02
227 #define EVBUFFER_EOF		0x10
228 #define EVBUFFER_ERROR		0x20
229 #define EVBUFFER_TIMEOUT	0x40
230 
231 struct bufferevent;
232 typedef void (*evbuffercb)(struct bufferevent *, void *);
233 typedef void (*everrorcb)(struct bufferevent *, short what, void *);
234 
235 struct event_watermark {
236 	size_t low;
237 	size_t high;
238 };
239 
240 struct bufferevent {
241 	struct event ev_read;
242 	struct event ev_write;
243 
244 	struct evbuffer *input;
245 	struct evbuffer *output;
246 
247 	struct event_watermark wm_read;
248 	struct event_watermark wm_write;
249 
250 	evbuffercb readcb;
251 	evbuffercb writecb;
252 	everrorcb errorcb;
253 	void *cbarg;
254 
255 	int timeout_read;	/* in seconds */
256 	int timeout_write;	/* in seconds */
257 
258 	short enabled;	/* events that are currently enabled */
259 };
260 
261 struct bufferevent *bufferevent_new(int fd,
262     evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg);
263 int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev);
264 int bufferevent_priority_set(struct bufferevent *bufev, int pri);
265 void bufferevent_free(struct bufferevent *bufev);
266 int bufferevent_write(struct bufferevent *bufev, void *data, size_t size);
267 int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf);
268 size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size);
269 int bufferevent_enable(struct bufferevent *bufev, short event);
270 int bufferevent_disable(struct bufferevent *bufev, short event);
271 void bufferevent_settimeout(struct bufferevent *bufev,
272     int timeout_read, int timeout_write);
273 
274 #define EVBUFFER_LENGTH(x)	(x)->off
275 #define EVBUFFER_DATA(x)	(x)->buffer
276 #define EVBUFFER_INPUT(x)	(x)->input
277 #define EVBUFFER_OUTPUT(x)	(x)->output
278 
279 struct evbuffer *evbuffer_new(void);
280 void evbuffer_free(struct evbuffer *);
281 int evbuffer_expand(struct evbuffer *, size_t);
282 int evbuffer_add(struct evbuffer *, const void *, size_t);
283 int evbuffer_remove(struct evbuffer *, void *, size_t);
284 char *evbuffer_readline(struct evbuffer *);
285 int evbuffer_add_buffer(struct evbuffer *, struct evbuffer *);
286 int evbuffer_add_printf(struct evbuffer *, const char *fmt, ...);
287 int evbuffer_add_vprintf(struct evbuffer *, const char *fmt, va_list ap);
288 void evbuffer_drain(struct evbuffer *, size_t);
289 int evbuffer_write(struct evbuffer *, int);
290 int evbuffer_read(struct evbuffer *, int, int);
291 u_char *evbuffer_find(struct evbuffer *, const u_char *, size_t);
292 void evbuffer_setcb(struct evbuffer *, void (*)(struct evbuffer *, size_t, size_t, void *), void *);
293 
294 /*
295  * Marshaling tagged data - We assume that all tags are inserted in their
296  * numeric order - so that unknown tags will always be higher than the
297  * known ones - and we can just ignore the end of an event buffer.
298  */
299 
300 void evtag_init(void);
301 
302 void evtag_marshal(struct evbuffer *evbuf, u_int8_t tag, const void *data,
303     u_int32_t len);
304 
305 void encode_int(struct evbuffer *evbuf, u_int32_t number);
306 
307 void evtag_marshal_int(struct evbuffer *evbuf, u_int8_t tag,
308     u_int32_t integer);
309 
310 void evtag_marshal_string(struct evbuffer *buf, u_int8_t tag,
311     const char *string);
312 
313 void evtag_marshal_timeval(struct evbuffer *evbuf, u_int8_t tag,
314     struct timeval *tv);
315 
316 void evtag_test(void);
317 
318 int evtag_unmarshal(struct evbuffer *src, u_int8_t *ptag,
319     struct evbuffer *dst);
320 int evtag_peek(struct evbuffer *evbuf, u_int8_t *ptag);
321 int evtag_peek_length(struct evbuffer *evbuf, u_int32_t *plength);
322 int evtag_payload_length(struct evbuffer *evbuf, u_int32_t *plength);
323 int evtag_consume(struct evbuffer *evbuf);
324 
325 int evtag_unmarshal_int(struct evbuffer *evbuf, u_int8_t need_tag,
326     u_int32_t *pinteger);
327 
328 int evtag_unmarshal_fixed(struct evbuffer *src, u_int8_t need_tag, void *data,
329     size_t len);
330 
331 int evtag_unmarshal_string(struct evbuffer *evbuf, u_int8_t need_tag,
332     char **pstring);
333 
334 int evtag_unmarshal_timeval(struct evbuffer *evbuf, u_int8_t need_tag,
335     struct timeval *ptv);
336 
337 #ifdef __cplusplus
338 }
339 #endif
340 
341 #endif /* _EVENT_H_ */
342