Merge commit 'crater/master'
[dragonfly.git] / contrib / libevent / event_tagging.c
blob3266d5435a169fe1e67fe8ee6308f680f6ed6b20
1 /*
2 * Copyright (c) 2003, 2004 Niels Provos <provos@citi.umich.edu>
3 * All rights reserved.
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.
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.
28 #include <sys/types.h>
29 #include <sys/param.h>
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
35 #ifdef WIN32
36 #define WIN32_LEAN_AND_MEAN
37 #include <windows.h>
38 #undef WIN32_LEAN_AND_MEAN
39 #else
40 #include <sys/ioctl.h>
41 #endif
43 #include <sys/tree.h>
44 #include <sys/queue.h>
45 #ifdef HAVE_SYS_TIME_H
46 #include <sys/time.h>
47 #endif
49 #include <errno.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #ifndef WIN32
54 #include <syslog.h>
55 #endif
56 #include <unistd.h>
58 #include "event.h"
59 #include "log.h"
61 int decode_int(uint32_t *pnumber, struct evbuffer *evbuf);
63 static struct evbuffer *_buf; /* not thread safe */
65 void
66 evtag_init()
68 if (_buf != NULL)
69 return;
71 if ((_buf = evbuffer_new()) == NULL)
72 event_err(1, "%s: malloc", __func__);
75 /*
76 * We encode integer's by nibbles; the first nibble contains the number
77 * of significant nibbles - 1; this allows us to encode up to 64-bit
78 * integers. This function is byte-order independent.
81 void
82 encode_int(struct evbuffer *evbuf, uint32_t number)
84 int off = 1, nibbles = 0;
85 uint8_t data[5];
87 memset(data, 0, sizeof(data));
88 while (number) {
89 if (off & 0x1)
90 data[off/2] = (data[off/2] & 0xf0) | (number & 0x0f);
91 else
92 data[off/2] = (data[off/2] & 0x0f) |
93 ((number & 0x0f) << 4);
94 number >>= 4;
95 off++;
98 if (off > 2)
99 nibbles = off - 2;
101 /* Off - 1 is the number of encoded nibbles */
102 data[0] = (data[0] & 0x0f) | ((nibbles & 0x0f) << 4);
104 evbuffer_add(evbuf, data, (off + 1) / 2);
108 * Marshal a data type, the general format is as follows:
110 * tag number: one byte; length: var bytes; payload: var bytes
113 void
114 evtag_marshal(struct evbuffer *evbuf, uint8_t tag,
115 const void *data, uint32_t len)
117 evbuffer_add(evbuf, &tag, sizeof(tag));
118 encode_int(evbuf, len);
119 evbuffer_add(evbuf, (void *)data, len);
122 /* Marshaling for integers */
123 void
124 evtag_marshal_int(struct evbuffer *evbuf, uint8_t tag, uint32_t integer)
126 evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
127 encode_int(_buf, integer);
129 evbuffer_add(evbuf, &tag, sizeof(tag));
130 encode_int(evbuf, EVBUFFER_LENGTH(_buf));
131 evbuffer_add_buffer(evbuf, _buf);
134 void
135 evtag_marshal_string(struct evbuffer *buf, uint8_t tag, const char *string)
137 evtag_marshal(buf, tag, string, strlen(string));
140 void
141 evtag_marshal_timeval(struct evbuffer *evbuf, uint8_t tag, struct timeval *tv)
143 evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
145 encode_int(_buf, tv->tv_sec);
146 encode_int(_buf, tv->tv_usec);
148 evtag_marshal(evbuf, tag, EVBUFFER_DATA(_buf),
149 EVBUFFER_LENGTH(_buf));
152 static int
153 decode_int_internal(uint32_t *pnumber, struct evbuffer *evbuf, int dodrain)
155 uint32_t number = 0;
156 uint8_t *data = EVBUFFER_DATA(evbuf);
157 int len = EVBUFFER_LENGTH(evbuf);
158 int nibbles = 0, off;
160 if (!len)
161 return (-1);
163 nibbles = ((data[0] & 0xf0) >> 4) + 1;
164 if (nibbles > 8 || (nibbles >> 1) > len - 1)
165 return (-1);
167 off = nibbles;
168 while (off > 0) {
169 number <<= 4;
170 if (off & 0x1)
171 number |= data[off >> 1] & 0x0f;
172 else
173 number |= (data[off >> 1] & 0xf0) >> 4;
174 off--;
177 len = (nibbles >> 1) + 1;
178 if (dodrain)
179 evbuffer_drain(evbuf, len);
181 *pnumber = number;
183 return (len);
187 decode_int(uint32_t *pnumber, struct evbuffer *evbuf)
189 return (decode_int_internal(pnumber, evbuf, 1) == -1 ? -1 : 0);
193 evtag_peek(struct evbuffer *evbuf, uint8_t *ptag)
195 if (EVBUFFER_LENGTH(evbuf) < 2)
196 return (-1);
197 *ptag = EVBUFFER_DATA(evbuf)[0];
199 return (0);
203 evtag_peek_length(struct evbuffer *evbuf, uint32_t *plength)
205 struct evbuffer tmp;
206 int res;
208 if (EVBUFFER_LENGTH(evbuf) < 2)
209 return (-1);
211 tmp = *evbuf;
212 tmp.buffer += 1;
213 tmp.off -= 1;
215 res = decode_int_internal(plength, &tmp, 0);
216 if (res == -1)
217 return (-1);
219 *plength += res + 1;
221 return (0);
225 evtag_payload_length(struct evbuffer *evbuf, uint32_t *plength)
227 struct evbuffer tmp;
228 int res;
230 if (EVBUFFER_LENGTH(evbuf) < 2)
231 return (-1);
233 tmp = *evbuf;
234 tmp.buffer += 1;
235 tmp.off -= 1;
237 res = decode_int_internal(plength, &tmp, 0);
238 if (res == -1)
239 return (-1);
241 return (0);
245 evtag_consume(struct evbuffer *evbuf)
247 uint32_t len;
248 evbuffer_drain(evbuf, 1);
249 if (decode_int(&len, evbuf) == -1)
250 return (-1);
251 evbuffer_drain(evbuf, len);
253 return (0);
256 /* Reads the data type from an event buffer */
259 evtag_unmarshal(struct evbuffer *src, uint8_t *ptag, struct evbuffer *dst)
261 uint8_t tag;
262 uint32_t len;
263 uint32_t integer;
265 if (evbuffer_remove(src, &tag, sizeof(tag)) != sizeof(tag))
266 return (-1);
267 if (decode_int(&integer, src) == -1)
268 return (-1);
269 len = integer;
271 if (EVBUFFER_LENGTH(src) < len)
272 return (-1);
274 if (evbuffer_add(dst, EVBUFFER_DATA(src), len) == -1)
275 return (-1);
277 evbuffer_drain(src, len);
279 *ptag = tag;
280 return (len);
283 /* Marshaling for integers */
286 evtag_unmarshal_int(struct evbuffer *evbuf, uint8_t need_tag,
287 uint32_t *pinteger)
289 uint8_t tag;
290 uint32_t len;
291 uint32_t integer;
293 if (evbuffer_remove(evbuf, &tag, sizeof(tag)) != sizeof(tag) ||
294 tag != need_tag)
295 return (-1);
296 if (decode_int(&integer, evbuf) == -1)
297 return (-1);
298 len = integer;
300 if (EVBUFFER_LENGTH(evbuf) < len)
301 return (-1);
303 evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
304 if (evbuffer_add(_buf, EVBUFFER_DATA(evbuf), len) == -1)
305 return (-1);
307 evbuffer_drain(evbuf, len);
309 return (decode_int(pinteger, _buf));
312 /* Unmarshal a fixed length tag */
315 evtag_unmarshal_fixed(struct evbuffer *src, uint8_t need_tag, void *data,
316 size_t len)
318 uint8_t tag;
320 /* Initialize this event buffer so that we can read into it */
321 evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
323 /* Now unmarshal a tag and check that it matches the tag we want */
324 if (evtag_unmarshal(src, &tag, _buf) == -1 || tag != need_tag)
325 return (-1);
327 if (EVBUFFER_LENGTH(_buf) != len)
328 return (-1);
330 memcpy(data, EVBUFFER_DATA(_buf), len);
331 return (0);
335 evtag_unmarshal_string(struct evbuffer *evbuf, uint8_t need_tag,
336 char **pstring)
338 uint8_t tag;
340 evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
342 if (evtag_unmarshal(evbuf, &tag, _buf) == -1 || tag != need_tag)
343 return (-1);
345 *pstring = calloc(EVBUFFER_LENGTH(_buf) + 1, 1);
346 if (*pstring == NULL)
347 event_err(1, "%s: calloc", __func__);
348 evbuffer_remove(_buf, *pstring, EVBUFFER_LENGTH(_buf));
350 return (0);
354 evtag_unmarshal_timeval(struct evbuffer *evbuf, uint8_t need_tag,
355 struct timeval *ptv)
357 uint8_t tag;
358 uint32_t integer;
360 evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
361 if (evtag_unmarshal(evbuf, &tag, _buf) == -1 || tag != need_tag)
362 return (-1);
364 if (decode_int(&integer, _buf) == -1)
365 return (-1);
366 ptv->tv_sec = integer;
367 if (decode_int(&integer, _buf) == -1)
368 return (-1);
369 ptv->tv_usec = integer;
371 return (0);