3266d5435a169fe1e67fe8ee6308f680f6ed6b20
[dragonfly.git] / contrib / libevent / event_tagging.c
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
28 #include <sys/types.h>
29 #include <sys/param.h>
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
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
42
43 #include <sys/tree.h>
44 #include <sys/queue.h>
45 #ifdef HAVE_SYS_TIME_H
46 #include <sys/time.h>
47 #endif
48
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>
57
58 #include "event.h"
59 #include "log.h"
60
61 int decode_int(uint32_t *pnumber, struct evbuffer *evbuf);
62
63 static struct evbuffer *_buf;   /* not thread safe */
64
65 void
66 evtag_init()
67 {
68         if (_buf != NULL)
69                 return;
70
71         if ((_buf = evbuffer_new()) == NULL)
72                 event_err(1, "%s: malloc", __func__);
73 }
74
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.
79  */
80
81 void
82 encode_int(struct evbuffer *evbuf, uint32_t number)
83 {
84         int off = 1, nibbles = 0;
85         uint8_t data[5];
86
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++;
96         }
97
98         if (off > 2)
99                 nibbles = off - 2;
100
101         /* Off - 1 is the number of encoded nibbles */
102         data[0] = (data[0] & 0x0f) | ((nibbles & 0x0f) << 4);
103
104         evbuffer_add(evbuf, data, (off + 1) / 2);
105 }
106
107 /*
108  * Marshal a data type, the general format is as follows:
109  *
110  * tag number: one byte; length: var bytes; payload: var bytes
111  */
112
113 void
114 evtag_marshal(struct evbuffer *evbuf, uint8_t tag,
115     const void *data, uint32_t len)
116 {
117         evbuffer_add(evbuf, &tag, sizeof(tag));
118         encode_int(evbuf, len);
119         evbuffer_add(evbuf, (void *)data, len);
120 }
121
122 /* Marshaling for integers */
123 void
124 evtag_marshal_int(struct evbuffer *evbuf, uint8_t tag, uint32_t integer)
125 {
126         evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
127         encode_int(_buf, integer);
128
129         evbuffer_add(evbuf, &tag, sizeof(tag));
130         encode_int(evbuf, EVBUFFER_LENGTH(_buf));
131         evbuffer_add_buffer(evbuf, _buf);
132 }
133
134 void
135 evtag_marshal_string(struct evbuffer *buf, uint8_t tag, const char *string)
136 {
137         evtag_marshal(buf, tag, string, strlen(string));
138 }
139
140 void
141 evtag_marshal_timeval(struct evbuffer *evbuf, uint8_t tag, struct timeval *tv)
142 {
143         evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
144
145         encode_int(_buf, tv->tv_sec);
146         encode_int(_buf, tv->tv_usec);
147
148         evtag_marshal(evbuf, tag, EVBUFFER_DATA(_buf),
149             EVBUFFER_LENGTH(_buf));
150 }
151
152 static int
153 decode_int_internal(uint32_t *pnumber, struct evbuffer *evbuf, int dodrain)
154 {
155         uint32_t number = 0;
156         uint8_t *data = EVBUFFER_DATA(evbuf);
157         int len = EVBUFFER_LENGTH(evbuf);
158         int nibbles = 0, off;
159
160         if (!len)
161                 return (-1);
162
163         nibbles = ((data[0] & 0xf0) >> 4) + 1;
164         if (nibbles > 8 || (nibbles >> 1) > len - 1)
165                 return (-1);
166
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--;
175         }
176
177         len = (nibbles >> 1) + 1;
178         if (dodrain)
179                 evbuffer_drain(evbuf, len);
180
181         *pnumber = number;
182
183         return (len);
184 }
185
186 int
187 decode_int(uint32_t *pnumber, struct evbuffer *evbuf)
188 {
189         return (decode_int_internal(pnumber, evbuf, 1) == -1 ? -1 : 0);
190 }
191
192 int
193 evtag_peek(struct evbuffer *evbuf, uint8_t *ptag)
194 {
195         if (EVBUFFER_LENGTH(evbuf) < 2)
196                 return (-1);
197         *ptag = EVBUFFER_DATA(evbuf)[0];
198
199         return (0);
200 }
201
202 int
203 evtag_peek_length(struct evbuffer *evbuf, uint32_t *plength)
204 {
205         struct evbuffer tmp;
206         int res;
207
208         if (EVBUFFER_LENGTH(evbuf) < 2)
209                 return (-1);
210
211         tmp = *evbuf;
212         tmp.buffer += 1;
213         tmp.off -= 1;
214
215         res = decode_int_internal(plength, &tmp, 0);
216         if (res == -1)
217                 return (-1);
218
219         *plength += res + 1;
220
221         return (0);
222 }
223
224 int
225 evtag_payload_length(struct evbuffer *evbuf, uint32_t *plength)
226 {
227         struct evbuffer tmp;
228         int res;
229
230         if (EVBUFFER_LENGTH(evbuf) < 2)
231                 return (-1);
232
233         tmp = *evbuf;
234         tmp.buffer += 1;
235         tmp.off -= 1;
236
237         res = decode_int_internal(plength, &tmp, 0);
238         if (res == -1)
239                 return (-1);
240
241         return (0);
242 }
243
244 int
245 evtag_consume(struct evbuffer *evbuf)
246 {
247         uint32_t len;
248         evbuffer_drain(evbuf, 1);
249         if (decode_int(&len, evbuf) == -1)
250                 return (-1);
251         evbuffer_drain(evbuf, len);
252
253         return (0);
254 }
255
256 /* Reads the data type from an event buffer */
257
258 int
259 evtag_unmarshal(struct evbuffer *src, uint8_t *ptag, struct evbuffer *dst)
260 {
261         uint8_t tag;
262         uint32_t len;
263         uint32_t integer;
264
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;
270
271         if (EVBUFFER_LENGTH(src) < len)
272                 return (-1);
273
274         if (evbuffer_add(dst, EVBUFFER_DATA(src), len) == -1)
275                 return (-1);
276
277         evbuffer_drain(src, len);
278
279         *ptag = tag;
280         return (len);
281 }
282
283 /* Marshaling for integers */
284
285 int
286 evtag_unmarshal_int(struct evbuffer *evbuf, uint8_t need_tag,
287     uint32_t *pinteger)
288 {
289         uint8_t tag;
290         uint32_t len;
291         uint32_t integer;
292
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;
299
300         if (EVBUFFER_LENGTH(evbuf) < len)
301                 return (-1);
302         
303         evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
304         if (evbuffer_add(_buf, EVBUFFER_DATA(evbuf), len) == -1)
305                 return (-1);
306
307         evbuffer_drain(evbuf, len);
308
309         return (decode_int(pinteger, _buf));
310 }
311
312 /* Unmarshal a fixed length tag */
313
314 int
315 evtag_unmarshal_fixed(struct evbuffer *src, uint8_t need_tag, void *data,
316     size_t len)
317 {
318         uint8_t tag;
319
320         /* Initialize this event buffer so that we can read into it */
321         evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
322
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);
326
327         if (EVBUFFER_LENGTH(_buf) != len)
328                 return (-1);
329
330         memcpy(data, EVBUFFER_DATA(_buf), len);
331         return (0);
332 }
333
334 int
335 evtag_unmarshal_string(struct evbuffer *evbuf, uint8_t need_tag,
336     char **pstring)
337 {
338         uint8_t tag;
339
340         evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
341
342         if (evtag_unmarshal(evbuf, &tag, _buf) == -1 || tag != need_tag)
343                 return (-1);
344
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));
349
350         return (0);
351 }
352
353 int
354 evtag_unmarshal_timeval(struct evbuffer *evbuf, uint8_t need_tag,
355     struct timeval *ptv)
356 {
357         uint8_t tag;
358         uint32_t integer;
359
360         evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
361         if (evtag_unmarshal(evbuf, &tag, _buf) == -1 || tag != need_tag)
362                 return (-1);
363
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;
370
371         return (0);
372 }