A clone of btpd with my configuration changes.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

342 строки
11 KiB

  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. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #include <stdarg.h>
  33. #ifdef WIN32
  34. #define WIN32_LEAN_AND_MEAN
  35. #include <windows.h>
  36. #undef WIN32_LEAN_AND_MEAN
  37. typedef unsigned char u_char;
  38. typedef unsigned short u_short;
  39. #endif
  40. #define EVLIST_TIMEOUT 0x01
  41. #define EVLIST_INSERTED 0x02
  42. #define EVLIST_SIGNAL 0x04
  43. #define EVLIST_ACTIVE 0x08
  44. #define EVLIST_INTERNAL 0x10
  45. #define EVLIST_INIT 0x80
  46. /* EVLIST_X_ Private space: 0x1000-0xf000 */
  47. #define EVLIST_ALL (0xf000 | 0x9f)
  48. #define EV_TIMEOUT 0x01
  49. #define EV_READ 0x02
  50. #define EV_WRITE 0x04
  51. #define EV_SIGNAL 0x08
  52. #define EV_PERSIST 0x10 /* Persistant event */
  53. /* Fix so that ppl dont have to run with <sys/queue.h> */
  54. #ifndef TAILQ_ENTRY
  55. #define _EVENT_DEFINED_TQENTRY
  56. #define TAILQ_ENTRY(type) \
  57. struct { \
  58. struct type *tqe_next; /* next element */ \
  59. struct type **tqe_prev; /* address of previous next element */ \
  60. }
  61. #endif /* !TAILQ_ENTRY */
  62. #ifndef RB_ENTRY
  63. #define _EVENT_DEFINED_RBENTRY
  64. #define RB_ENTRY(type) \
  65. struct { \
  66. struct type *rbe_left; /* left element */ \
  67. struct type *rbe_right; /* right element */ \
  68. struct type *rbe_parent; /* parent element */ \
  69. int rbe_color; /* node color */ \
  70. }
  71. #endif /* !RB_ENTRY */
  72. struct event_base;
  73. struct event {
  74. TAILQ_ENTRY (event) ev_next;
  75. TAILQ_ENTRY (event) ev_active_next;
  76. TAILQ_ENTRY (event) ev_signal_next;
  77. RB_ENTRY (event) ev_timeout_node;
  78. struct event_base *ev_base;
  79. int ev_fd;
  80. short ev_events;
  81. short ev_ncalls;
  82. short *ev_pncalls; /* Allows deletes in callback */
  83. struct timeval ev_timeout;
  84. int ev_pri; /* smaller numbers are higher priority */
  85. void (*ev_callback)(int, short, void *arg);
  86. void *ev_arg;
  87. int ev_res; /* result passed to event callback */
  88. int ev_flags;
  89. };
  90. #define EVENT_SIGNAL(ev) (int)(ev)->ev_fd
  91. #define EVENT_FD(ev) (int)(ev)->ev_fd
  92. /*
  93. * Key-Value pairs. Can be used for HTTP headers but also for
  94. * query argument parsing.
  95. */
  96. struct evkeyval {
  97. TAILQ_ENTRY(evkeyval) next;
  98. char *key;
  99. char *value;
  100. };
  101. #ifdef _EVENT_DEFINED_TQENTRY
  102. #undef TAILQ_ENTRY
  103. struct event_list;
  104. struct evkeyvalq;
  105. #undef _EVENT_DEFINED_TQENTRY
  106. #else
  107. TAILQ_HEAD (event_list, event);
  108. TAILQ_HEAD (evkeyvalq, evkeyval);
  109. #endif /* _EVENT_DEFINED_TQENTRY */
  110. #ifdef _EVENT_DEFINED_RBENTRY
  111. #undef RB_ENTRY
  112. #undef _EVENT_DEFINED_RBENTRY
  113. #endif /* _EVENT_DEFINED_RBENTRY */
  114. struct eventop {
  115. char *name;
  116. void *(*init)(void);
  117. int (*add)(void *, struct event *);
  118. int (*del)(void *, struct event *);
  119. int (*recalc)(struct event_base *, void *, int);
  120. int (*dispatch)(struct event_base *, void *, struct timeval *);
  121. void (*dealloc)(void *);
  122. };
  123. #define TIMEOUT_DEFAULT {5, 0}
  124. void *event_init(void);
  125. int event_dispatch(void);
  126. int event_base_dispatch(struct event_base *);
  127. void event_base_free(struct event_base *);
  128. #define _EVENT_LOG_DEBUG 0
  129. #define _EVENT_LOG_MSG 1
  130. #define _EVENT_LOG_WARN 2
  131. #define _EVENT_LOG_ERR 3
  132. typedef void (*event_log_cb)(int severity, const char *msg);
  133. void event_set_log_callback(event_log_cb cb);
  134. /* Associate a different event base with an event */
  135. int event_base_set(struct event_base *, struct event *);
  136. #define EVLOOP_ONCE 0x01
  137. #define EVLOOP_NONBLOCK 0x02
  138. int event_loop(int);
  139. int event_base_loop(struct event_base *, int);
  140. int event_loopexit(struct timeval *); /* Causes the loop to exit */
  141. int event_base_loopexit(struct event_base *, struct timeval *);
  142. #define evtimer_add(ev, tv) event_add(ev, tv)
  143. #define evtimer_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg)
  144. #define evtimer_del(ev) event_del(ev)
  145. #define evtimer_pending(ev, tv) event_pending(ev, EV_TIMEOUT, tv)
  146. #define evtimer_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
  147. #define timeout_add(ev, tv) event_add(ev, tv)
  148. #define timeout_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg)
  149. #define timeout_del(ev) event_del(ev)
  150. #define timeout_pending(ev, tv) event_pending(ev, EV_TIMEOUT, tv)
  151. #define timeout_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
  152. #define signal_add(ev, tv) event_add(ev, tv)
  153. #define signal_set(ev, x, cb, arg) \
  154. event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg)
  155. #define signal_del(ev) event_del(ev)
  156. #define signal_pending(ev, tv) event_pending(ev, EV_SIGNAL, tv)
  157. #define signal_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
  158. void event_set(struct event *, int, short, void (*)(int, short, void *), void *);
  159. int event_once(int, short, void (*)(int, short, void *), void *, struct timeval *);
  160. int event_add(struct event *, struct timeval *);
  161. int event_del(struct event *);
  162. void event_active(struct event *, int, short);
  163. int event_pending(struct event *, short, struct timeval *);
  164. #ifdef WIN32
  165. #define event_initialized(ev) ((ev)->ev_flags & EVLIST_INIT && (ev)->ev_fd != (int)INVALID_HANDLE_VALUE)
  166. #else
  167. #define event_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
  168. #endif
  169. /* Some simple debugging functions */
  170. const char *event_get_version(void);
  171. const char *event_get_method(void);
  172. /* These functions deal with event priorities */
  173. int event_priority_init(int);
  174. int event_base_priority_init(struct event_base *, int);
  175. int event_priority_set(struct event *, int);
  176. /* These functions deal with buffering input and output */
  177. struct evbuffer {
  178. u_char *buffer;
  179. u_char *orig_buffer;
  180. size_t misalign;
  181. size_t totallen;
  182. size_t off;
  183. void (*cb)(struct evbuffer *, size_t, size_t, void *);
  184. void *cbarg;
  185. };
  186. /* Just for error reporting - use other constants otherwise */
  187. #define EVBUFFER_READ 0x01
  188. #define EVBUFFER_WRITE 0x02
  189. #define EVBUFFER_EOF 0x10
  190. #define EVBUFFER_ERROR 0x20
  191. #define EVBUFFER_TIMEOUT 0x40
  192. struct bufferevent;
  193. typedef void (*evbuffercb)(struct bufferevent *, void *);
  194. typedef void (*everrorcb)(struct bufferevent *, short what, void *);
  195. struct event_watermark {
  196. size_t low;
  197. size_t high;
  198. };
  199. struct bufferevent {
  200. struct event ev_read;
  201. struct event ev_write;
  202. struct evbuffer *input;
  203. struct evbuffer *output;
  204. struct event_watermark wm_read;
  205. struct event_watermark wm_write;
  206. evbuffercb readcb;
  207. evbuffercb writecb;
  208. everrorcb errorcb;
  209. void *cbarg;
  210. int timeout_read; /* in seconds */
  211. int timeout_write; /* in seconds */
  212. short enabled; /* events that are currently enabled */
  213. };
  214. struct bufferevent *bufferevent_new(int fd,
  215. evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg);
  216. int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev);
  217. int bufferevent_priority_set(struct bufferevent *bufev, int pri);
  218. void bufferevent_free(struct bufferevent *bufev);
  219. int bufferevent_write(struct bufferevent *bufev, void *data, size_t size);
  220. int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf);
  221. size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size);
  222. int bufferevent_enable(struct bufferevent *bufev, short event);
  223. int bufferevent_disable(struct bufferevent *bufev, short event);
  224. void bufferevent_settimeout(struct bufferevent *bufev,
  225. int timeout_read, int timeout_write);
  226. #define EVBUFFER_LENGTH(x) (x)->off
  227. #define EVBUFFER_DATA(x) (x)->buffer
  228. #define EVBUFFER_INPUT(x) (x)->input
  229. #define EVBUFFER_OUTPUT(x) (x)->output
  230. struct evbuffer *evbuffer_new(void);
  231. void evbuffer_free(struct evbuffer *);
  232. int evbuffer_expand(struct evbuffer *, size_t);
  233. int evbuffer_add(struct evbuffer *, const void *, size_t);
  234. int evbuffer_remove(struct evbuffer *, void *, size_t);
  235. char *evbuffer_readline(struct evbuffer *);
  236. int evbuffer_add_buffer(struct evbuffer *, struct evbuffer *);
  237. int evbuffer_add_printf(struct evbuffer *, const char *fmt, ...);
  238. int evbuffer_add_vprintf(struct evbuffer *, const char *fmt, va_list ap);
  239. void evbuffer_drain(struct evbuffer *, size_t);
  240. int evbuffer_write(struct evbuffer *, int);
  241. int evbuffer_read(struct evbuffer *, int, int);
  242. u_char *evbuffer_find(struct evbuffer *, const u_char *, size_t);
  243. void evbuffer_setcb(struct evbuffer *, void (*)(struct evbuffer *, size_t, size_t, void *), void *);
  244. /*
  245. * Marshaling tagged data - We assume that all tags are inserted in their
  246. * numeric order - so that unknown tags will always be higher than the
  247. * known ones - and we can just ignore the end of an event buffer.
  248. */
  249. void evtag_init(void);
  250. void evtag_marshal(struct evbuffer *evbuf, u_int8_t tag, const void *data,
  251. u_int32_t len);
  252. void encode_int(struct evbuffer *evbuf, u_int32_t number);
  253. void evtag_marshal_int(struct evbuffer *evbuf, u_int8_t tag,
  254. u_int32_t integer);
  255. void evtag_marshal_string(struct evbuffer *buf, u_int8_t tag,
  256. const char *string);
  257. void evtag_marshal_timeval(struct evbuffer *evbuf, u_int8_t tag,
  258. struct timeval *tv);
  259. void evtag_test(void);
  260. int evtag_unmarshal(struct evbuffer *src, u_int8_t *ptag,
  261. struct evbuffer *dst);
  262. int evtag_peek(struct evbuffer *evbuf, u_int8_t *ptag);
  263. int evtag_peek_length(struct evbuffer *evbuf, u_int32_t *plength);
  264. int evtag_payload_length(struct evbuffer *evbuf, u_int32_t *plength);
  265. int evtag_consume(struct evbuffer *evbuf);
  266. int evtag_unmarshal_int(struct evbuffer *evbuf, u_int8_t need_tag,
  267. u_int32_t *pinteger);
  268. int evtag_unmarshal_fixed(struct evbuffer *src, u_int8_t need_tag, void *data,
  269. size_t len);
  270. int evtag_unmarshal_string(struct evbuffer *evbuf, u_int8_t need_tag,
  271. char **pstring);
  272. int evtag_unmarshal_timeval(struct evbuffer *evbuf, u_int8_t need_tag,
  273. struct timeval *ptv);
  274. #ifdef __cplusplus
  275. }
  276. #endif
  277. #endif /* _EVENT_H_ */