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