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 |
#ifdef __cplusplus |
28 |
extern "C" { |
29 |
#endif |
30 |
|
31 |
#ifdef _WIN32 |
32 |
# define WIN32_LEAN_AND_MEAN |
33 |
# include <windows.h> |
34 |
# undef WIN32_LEAN_AND_MEAN |
35 |
typedef unsigned char u_char; |
36 |
typedef unsigned short u_short; |
37 |
#else |
38 |
# include <sys/types.h> |
39 |
# include <sys/time.h> |
40 |
# include <inttypes.h> |
41 |
#endif |
42 |
|
43 |
#include <stdarg.h> |
44 |
|
45 |
/* Fix so that ppl dont have to run with <sys/queue.h> */ |
46 |
#ifndef TAILQ_ENTRY |
47 |
#define _EVENT_DEFINED_TQENTRY |
48 |
#define TAILQ_ENTRY(type) \ |
49 |
struct { \ |
50 |
struct type *tqe_next; /* next element */ \ |
51 |
struct type **tqe_prev; /* address of previous next element */ \ |
52 |
} |
53 |
#endif /* !TAILQ_ENTRY */ |
54 |
#ifndef RB_ENTRY |
55 |
#define _EVENT_DEFINED_RBENTRY |
56 |
#define RB_ENTRY(type) \ |
57 |
struct { \ |
58 |
struct type *rbe_left; /* left element */ \ |
59 |
struct type *rbe_right; /* right element */ \ |
60 |
struct type *rbe_parent; /* parent element */ \ |
61 |
int rbe_color; /* node color */ \ |
62 |
} |
63 |
#endif /* !RB_ENTRY */ |
64 |
|
65 |
/* |
66 |
* Key-Value pairs. Can be used for HTTP headers but also for |
67 |
* query argument parsing. |
68 |
*/ |
69 |
struct evkeyval { |
70 |
TAILQ_ENTRY(evkeyval) next; |
71 |
|
72 |
char *key; |
73 |
char *value; |
74 |
}; |
75 |
|
76 |
#ifdef _EVENT_DEFINED_TQENTRY |
77 |
#undef TAILQ_ENTRY |
78 |
struct event_list; |
79 |
struct evkeyvalq; |
80 |
#undef _EVENT_DEFINED_TQENTRY |
81 |
#else |
82 |
TAILQ_HEAD (event_list, event); |
83 |
TAILQ_HEAD (evkeyvalq, evkeyval); |
84 |
#endif /* _EVENT_DEFINED_TQENTRY */ |
85 |
#ifdef _EVENT_DEFINED_RBENTRY |
86 |
#undef RB_ENTRY |
87 |
#undef _EVENT_DEFINED_RBENTRY |
88 |
#endif /* _EVENT_DEFINED_RBENTRY */ |
89 |
|
90 |
struct eventop { |
91 |
char *name; |
92 |
void *(*init)(struct event_base *); |
93 |
int (*add)(void *, struct event *); |
94 |
int (*del)(void *, struct event *); |
95 |
int (*recalc)(struct event_base *, void *, int); |
96 |
int (*dispatch)(struct event_base *, void *, struct timeval *); |
97 |
void (*dealloc)(struct event_base *, void *); |
98 |
}; |
99 |
|
100 |
/* These functions deal with buffering input and output */ |
101 |
|
102 |
struct evbuffer { |
103 |
u_char *buffer; |
104 |
u_char *orig_buffer; |
105 |
|
106 |
size_t misalign; |
107 |
size_t totallen; |
108 |
size_t off; |
109 |
|
110 |
void (*cb)(struct evbuffer *, size_t, size_t, void *); |
111 |
void *cbarg; |
112 |
}; |
113 |
|
114 |
/* Just for error reporting - use other constants otherwise */ |
115 |
#define EVBUFFER_READ 0x01 |
116 |
#define EVBUFFER_WRITE 0x02 |
117 |
#define EVBUFFER_EOF 0x10 |
118 |
#define EVBUFFER_ERROR 0x20 |
119 |
#define EVBUFFER_TIMEOUT 0x40 |
120 |
|
121 |
struct bufferevent; |
122 |
typedef void (*evbuffercb)(struct bufferevent *, void *); |
123 |
typedef void (*everrorcb)(struct bufferevent *, short what, void *); |
124 |
|
125 |
struct event_watermark { |
126 |
size_t low; |
127 |
size_t high; |
128 |
}; |
129 |
|
130 |
struct bufferevent { |
131 |
struct event ev_read; |
132 |
struct event ev_write; |
133 |
|
134 |
struct evbuffer *input; |
135 |
struct evbuffer *output; |
136 |
|
137 |
struct event_watermark wm_read; |
138 |
struct event_watermark wm_write; |
139 |
|
140 |
evbuffercb readcb; |
141 |
evbuffercb writecb; |
142 |
everrorcb errorcb; |
143 |
void *cbarg; |
144 |
|
145 |
int timeout_read; /* in seconds */ |
146 |
int timeout_write; /* in seconds */ |
147 |
|
148 |
short enabled; /* events that are currently enabled */ |
149 |
}; |
150 |
|
151 |
struct bufferevent *bufferevent_new(int fd, |
152 |
evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg); |
153 |
int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev); |
154 |
int bufferevent_priority_set(struct bufferevent *bufev, int pri); |
155 |
void bufferevent_free(struct bufferevent *bufev); |
156 |
int bufferevent_write(struct bufferevent *bufev, void *data, size_t size); |
157 |
int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf); |
158 |
size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size); |
159 |
int bufferevent_enable(struct bufferevent *bufev, short event); |
160 |
int bufferevent_disable(struct bufferevent *bufev, short event); |
161 |
void bufferevent_settimeout(struct bufferevent *bufev, |
162 |
int timeout_read, int timeout_write); |
163 |
|
164 |
#define EVBUFFER_LENGTH(x) (x)->off |
165 |
#define EVBUFFER_DATA(x) (x)->buffer |
166 |
#define EVBUFFER_INPUT(x) (x)->input |
167 |
#define EVBUFFER_OUTPUT(x) (x)->output |
168 |
|
169 |
struct evbuffer *evbuffer_new(void); |
170 |
void evbuffer_free(struct evbuffer *); |
171 |
int evbuffer_expand(struct evbuffer *, size_t); |
172 |
int evbuffer_add(struct evbuffer *, const void *, size_t); |
173 |
int evbuffer_remove(struct evbuffer *, void *, size_t); |
174 |
char *evbuffer_readline(struct evbuffer *); |
175 |
int evbuffer_add_buffer(struct evbuffer *, struct evbuffer *); |
176 |
int evbuffer_add_printf(struct evbuffer *, const char *fmt, ...); |
177 |
int evbuffer_add_vprintf(struct evbuffer *, const char *fmt, va_list ap); |
178 |
void evbuffer_drain(struct evbuffer *, size_t); |
179 |
int evbuffer_write(struct evbuffer *, int); |
180 |
int evbuffer_read(struct evbuffer *, int, int); |
181 |
u_char *evbuffer_find(struct evbuffer *, const u_char *, size_t); |
182 |
void evbuffer_setcb(struct evbuffer *, void (*)(struct evbuffer *, size_t, size_t, void *), void *); |
183 |
|
184 |
/* |
185 |
* Marshaling tagged data - We assume that all tags are inserted in their |
186 |
* numeric order - so that unknown tags will always be higher than the |
187 |
* known ones - and we can just ignore the end of an event buffer. |
188 |
*/ |
189 |
|
190 |
void evtag_init(void); |
191 |
|
192 |
void evtag_marshal(struct evbuffer *evbuf, uint8_t tag, const void *data, |
193 |
uint32_t len); |
194 |
|
195 |
void encode_int(struct evbuffer *evbuf, uint32_t number); |
196 |
|
197 |
void evtag_marshal_int(struct evbuffer *evbuf, uint8_t tag, uint32_t integer); |
198 |
|
199 |
void evtag_marshal_string(struct evbuffer *buf, uint8_t tag, |
200 |
const char *string); |
201 |
|
202 |
void evtag_marshal_timeval(struct evbuffer *evbuf, uint8_t tag, |
203 |
struct timeval *tv); |
204 |
|
205 |
void evtag_test(void); |
206 |
|
207 |
int evtag_unmarshal(struct evbuffer *src, uint8_t *ptag, struct evbuffer *dst); |
208 |
int evtag_peek(struct evbuffer *evbuf, uint8_t *ptag); |
209 |
int evtag_peek_length(struct evbuffer *evbuf, uint32_t *plength); |
210 |
int evtag_payload_length(struct evbuffer *evbuf, uint32_t *plength); |
211 |
int evtag_consume(struct evbuffer *evbuf); |
212 |
|
213 |
int evtag_unmarshal_int(struct evbuffer *evbuf, uint8_t need_tag, |
214 |
uint32_t *pinteger); |
215 |
|
216 |
int evtag_unmarshal_fixed(struct evbuffer *src, uint8_t need_tag, void *data, |
217 |
size_t len); |
218 |
|
219 |
int evtag_unmarshal_string(struct evbuffer *evbuf, uint8_t need_tag, |
220 |
char **pstring); |
221 |
|
222 |
int evtag_unmarshal_timeval(struct evbuffer *evbuf, uint8_t need_tag, |
223 |
struct timeval *ptv); |
224 |
|
225 |
#ifdef __cplusplus |
226 |
} |
227 |
#endif |