event_tagging.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * Copyright (c) 2003, 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 HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #ifdef HAVE_SYS_TYPES_H
  31. #include <sys/types.h>
  32. #endif
  33. #ifdef HAVE_SYS_PARAM_H
  34. #include <sys/param.h>
  35. #endif
  36. #ifdef WIN32
  37. #define WIN32_LEAN_AND_MEAN
  38. #include <winsock2.h>
  39. #include <windows.h>
  40. #undef WIN32_LEAN_AND_MEAN
  41. #else
  42. #include <sys/ioctl.h>
  43. #endif
  44. #include <sys/queue.h>
  45. #ifdef HAVE_SYS_TIME_H
  46. #include <sys/time.h>
  47. #endif
  48. #include <errno.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #ifndef WIN32
  53. #include <syslog.h>
  54. #endif
  55. #ifdef HAVE_UNISTD_H
  56. #include <unistd.h>
  57. #endif
  58. #include "event.h"
  59. #include "evutil.h"
  60. #include "log.h"
  61. int evtag_decode_int(ev_uint32_t *pnumber, struct evbuffer *evbuf);
  62. int evtag_encode_tag(struct evbuffer *evbuf, ev_uint32_t tag);
  63. int evtag_decode_tag(ev_uint32_t *ptag, struct evbuffer *evbuf);
  64. static struct evbuffer *_buf; /* not thread safe */
  65. void
  66. evtag_init(void)
  67. {
  68. if (_buf != NULL)
  69. return;
  70. if ((_buf = evbuffer_new()) == NULL)
  71. event_err(1, "%s: malloc", __func__);
  72. }
  73. /*
  74. * We encode integer's by nibbles; the first nibble contains the number
  75. * of significant nibbles - 1; this allows us to encode up to 64-bit
  76. * integers. This function is byte-order independent.
  77. */
  78. void
  79. encode_int(struct evbuffer *evbuf, ev_uint32_t number)
  80. {
  81. int off = 1, nibbles = 0;
  82. ev_uint8_t data[5];
  83. memset(data, 0, sizeof(ev_uint32_t)+1);
  84. while (number) {
  85. if (off & 0x1)
  86. data[off/2] = (data[off/2] & 0xf0) | (number & 0x0f);
  87. else
  88. data[off/2] = (data[off/2] & 0x0f) |
  89. ((number & 0x0f) << 4);
  90. number >>= 4;
  91. off++;
  92. }
  93. if (off > 2)
  94. nibbles = off - 2;
  95. /* Off - 1 is the number of encoded nibbles */
  96. data[0] = (data[0] & 0x0f) | ((nibbles & 0x0f) << 4);
  97. evbuffer_add(evbuf, data, (off + 1) / 2);
  98. }
  99. /*
  100. * Support variable length encoding of tags; we use the high bit in each
  101. * octet as a continuation signal.
  102. */
  103. int
  104. evtag_encode_tag(struct evbuffer *evbuf, ev_uint32_t tag)
  105. {
  106. int bytes = 0;
  107. ev_uint8_t data[5];
  108. memset(data, 0, sizeof(data));
  109. do {
  110. ev_uint8_t lower = tag & 0x7f;
  111. tag >>= 7;
  112. if (tag)
  113. lower |= 0x80;
  114. data[bytes++] = lower;
  115. } while (tag);
  116. if (evbuf != NULL)
  117. evbuffer_add(evbuf, data, bytes);
  118. return (bytes);
  119. }
  120. static int
  121. decode_tag_internal(ev_uint32_t *ptag, struct evbuffer *evbuf, int dodrain)
  122. {
  123. ev_uint32_t number = 0;
  124. ev_uint8_t *data = EVBUFFER_DATA(evbuf);
  125. int len = EVBUFFER_LENGTH(evbuf);
  126. int count = 0, shift = 0, done = 0;
  127. while (count++ < len) {
  128. ev_uint8_t lower = *data++;
  129. number |= (lower & 0x7f) << shift;
  130. shift += 7;
  131. if (!(lower & 0x80)) {
  132. done = 1;
  133. break;
  134. }
  135. }
  136. if (!done)
  137. return (-1);
  138. if (dodrain)
  139. evbuffer_drain(evbuf, count);
  140. if (ptag != NULL)
  141. *ptag = number;
  142. return (count);
  143. }
  144. int
  145. evtag_decode_tag(ev_uint32_t *ptag, struct evbuffer *evbuf)
  146. {
  147. return (decode_tag_internal(ptag, evbuf, 1 /* dodrain */));
  148. }
  149. /*
  150. * Marshal a data type, the general format is as follows:
  151. *
  152. * tag number: one byte; length: var bytes; payload: var bytes
  153. */
  154. void
  155. evtag_marshal(struct evbuffer *evbuf, ev_uint32_t tag,
  156. const void *data, ev_uint32_t len)
  157. {
  158. evtag_encode_tag(evbuf, tag);
  159. encode_int(evbuf, len);
  160. evbuffer_add(evbuf, (void *)data, len);
  161. }
  162. /* Marshaling for integers */
  163. void
  164. evtag_marshal_int(struct evbuffer *evbuf, ev_uint32_t tag, ev_uint32_t integer)
  165. {
  166. evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
  167. encode_int(_buf, integer);
  168. evtag_encode_tag(evbuf, tag);
  169. encode_int(evbuf, EVBUFFER_LENGTH(_buf));
  170. evbuffer_add_buffer(evbuf, _buf);
  171. }
  172. void
  173. evtag_marshal_string(struct evbuffer *buf, ev_uint32_t tag, const char *string)
  174. {
  175. evtag_marshal(buf, tag, string, strlen(string));
  176. }
  177. void
  178. evtag_marshal_timeval(struct evbuffer *evbuf, ev_uint32_t tag, struct timeval *tv)
  179. {
  180. evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
  181. encode_int(_buf, tv->tv_sec);
  182. encode_int(_buf, tv->tv_usec);
  183. evtag_marshal(evbuf, tag, EVBUFFER_DATA(_buf),
  184. EVBUFFER_LENGTH(_buf));
  185. }
  186. static int
  187. decode_int_internal(ev_uint32_t *pnumber, struct evbuffer *evbuf, int dodrain)
  188. {
  189. ev_uint32_t number = 0;
  190. ev_uint8_t *data = EVBUFFER_DATA(evbuf);
  191. int len = EVBUFFER_LENGTH(evbuf);
  192. int nibbles = 0;
  193. if (!len)
  194. return (-1);
  195. nibbles = ((data[0] & 0xf0) >> 4) + 1;
  196. if (nibbles > 8 || (nibbles >> 1) + 1 > len)
  197. return (-1);
  198. len = (nibbles >> 1) + 1;
  199. while (nibbles > 0) {
  200. number <<= 4;
  201. if (nibbles & 0x1)
  202. number |= data[nibbles >> 1] & 0x0f;
  203. else
  204. number |= (data[nibbles >> 1] & 0xf0) >> 4;
  205. nibbles--;
  206. }
  207. if (dodrain)
  208. evbuffer_drain(evbuf, len);
  209. *pnumber = number;
  210. return (len);
  211. }
  212. int
  213. evtag_decode_int(ev_uint32_t *pnumber, struct evbuffer *evbuf)
  214. {
  215. return (decode_int_internal(pnumber, evbuf, 1) == -1 ? -1 : 0);
  216. }
  217. int
  218. evtag_peek(struct evbuffer *evbuf, ev_uint32_t *ptag)
  219. {
  220. return (decode_tag_internal(ptag, evbuf, 0 /* dodrain */));
  221. }
  222. int
  223. evtag_peek_length(struct evbuffer *evbuf, ev_uint32_t *plength)
  224. {
  225. struct evbuffer tmp;
  226. int res, len;
  227. len = decode_tag_internal(NULL, evbuf, 0 /* dodrain */);
  228. if (len == -1)
  229. return (-1);
  230. tmp = *evbuf;
  231. tmp.buffer += len;
  232. tmp.off -= len;
  233. res = decode_int_internal(plength, &tmp, 0);
  234. if (res == -1)
  235. return (-1);
  236. *plength += res + len;
  237. return (0);
  238. }
  239. int
  240. evtag_payload_length(struct evbuffer *evbuf, ev_uint32_t *plength)
  241. {
  242. struct evbuffer tmp;
  243. int res, len;
  244. len = decode_tag_internal(NULL, evbuf, 0 /* dodrain */);
  245. if (len == -1)
  246. return (-1);
  247. tmp = *evbuf;
  248. tmp.buffer += len;
  249. tmp.off -= len;
  250. res = decode_int_internal(plength, &tmp, 0);
  251. if (res == -1)
  252. return (-1);
  253. return (0);
  254. }
  255. int
  256. evtag_consume(struct evbuffer *evbuf)
  257. {
  258. ev_uint32_t len;
  259. if (decode_tag_internal(NULL, evbuf, 1 /* dodrain */) == -1)
  260. return (-1);
  261. if (evtag_decode_int(&len, evbuf) == -1)
  262. return (-1);
  263. evbuffer_drain(evbuf, len);
  264. return (0);
  265. }
  266. /* Reads the data type from an event buffer */
  267. int
  268. evtag_unmarshal(struct evbuffer *src, ev_uint32_t *ptag, struct evbuffer *dst)
  269. {
  270. ev_uint32_t len;
  271. ev_uint32_t integer;
  272. if (decode_tag_internal(ptag, src, 1 /* dodrain */) == -1)
  273. return (-1);
  274. if (evtag_decode_int(&integer, src) == -1)
  275. return (-1);
  276. len = integer;
  277. if (EVBUFFER_LENGTH(src) < len)
  278. return (-1);
  279. if (evbuffer_add(dst, EVBUFFER_DATA(src), len) == -1)
  280. return (-1);
  281. evbuffer_drain(src, len);
  282. return (len);
  283. }
  284. /* Marshaling for integers */
  285. int
  286. evtag_unmarshal_int(struct evbuffer *evbuf, ev_uint32_t need_tag,
  287. ev_uint32_t *pinteger)
  288. {
  289. ev_uint32_t tag;
  290. ev_uint32_t len;
  291. ev_uint32_t integer;
  292. if (decode_tag_internal(&tag, evbuf, 1 /* dodrain */) == -1)
  293. return (-1);
  294. if (need_tag != tag)
  295. return (-1);
  296. if (evtag_decode_int(&integer, evbuf) == -1)
  297. return (-1);
  298. len = integer;
  299. if (EVBUFFER_LENGTH(evbuf) < len)
  300. return (-1);
  301. evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
  302. if (evbuffer_add(_buf, EVBUFFER_DATA(evbuf), len) == -1)
  303. return (-1);
  304. evbuffer_drain(evbuf, len);
  305. return (evtag_decode_int(pinteger, _buf));
  306. }
  307. /* Unmarshal a fixed length tag */
  308. int
  309. evtag_unmarshal_fixed(struct evbuffer *src, ev_uint32_t need_tag, void *data,
  310. size_t len)
  311. {
  312. ev_uint32_t tag;
  313. /* Initialize this event buffer so that we can read into it */
  314. evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
  315. /* Now unmarshal a tag and check that it matches the tag we want */
  316. if (evtag_unmarshal(src, &tag, _buf) == -1 || tag != need_tag)
  317. return (-1);
  318. if (EVBUFFER_LENGTH(_buf) != len)
  319. return (-1);
  320. memcpy(data, EVBUFFER_DATA(_buf), len);
  321. return (0);
  322. }
  323. int
  324. evtag_unmarshal_string(struct evbuffer *evbuf, ev_uint32_t need_tag,
  325. char **pstring)
  326. {
  327. ev_uint32_t tag;
  328. evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
  329. if (evtag_unmarshal(evbuf, &tag, _buf) == -1 || tag != need_tag)
  330. return (-1);
  331. *pstring = calloc(EVBUFFER_LENGTH(_buf) + 1, 1);
  332. if (*pstring == NULL)
  333. event_err(1, "%s: calloc", __func__);
  334. evbuffer_remove(_buf, *pstring, EVBUFFER_LENGTH(_buf));
  335. return (0);
  336. }
  337. int
  338. evtag_unmarshal_timeval(struct evbuffer *evbuf, ev_uint32_t need_tag,
  339. struct timeval *ptv)
  340. {
  341. ev_uint32_t tag;
  342. ev_uint32_t integer;
  343. evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
  344. if (evtag_unmarshal(evbuf, &tag, _buf) == -1 || tag != need_tag)
  345. return (-1);
  346. if (evtag_decode_int(&integer, _buf) == -1)
  347. return (-1);
  348. ptv->tv_sec = integer;
  349. if (evtag_decode_int(&integer, _buf) == -1)
  350. return (-1);
  351. ptv->tv_usec = integer;
  352. return (0);
  353. }