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