2 * Copyright (c) 2003, 2004 Niels Provos <provos@citi.umich.edu>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
32 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h>
35 #ifdef HAVE_SYS_PARAM_H
36 #include <sys/param.h>
40 #define WIN32_LEAN_AND_MEAN
43 #undef WIN32_LEAN_AND_MEAN
45 #include <sys/ioctl.h>
48 #include <sys/queue.h>
49 #ifdef HAVE_SYS_TIME_H
68 int evtag_decode_int(ev_uint32_t
*pnumber
, struct evbuffer
*evbuf
);
69 int evtag_encode_tag(struct evbuffer
*evbuf
, ev_uint32_t tag
);
70 int evtag_decode_tag(ev_uint32_t
*ptag
, struct evbuffer
*evbuf
);
72 static struct evbuffer
*_buf
; /* not thread safe */
80 if ((_buf
= evbuffer_new()) == NULL
)
81 event_err(1, "%s: malloc", __func__
);
85 * We encode integer's by nibbles; the first nibble contains the number
86 * of significant nibbles - 1; this allows us to encode up to 64-bit
87 * integers. This function is byte-order independent.
91 encode_int(struct evbuffer
*evbuf
, ev_uint32_t number
)
93 int off
= 1, nibbles
= 0;
96 memset(data
, 0, sizeof(ev_uint32_t
)+1);
99 data
[off
/2] = (data
[off
/2] & 0xf0) | (number
& 0x0f);
101 data
[off
/2] = (data
[off
/2] & 0x0f) |
102 ((number
& 0x0f) << 4);
110 /* Off - 1 is the number of encoded nibbles */
111 data
[0] = (data
[0] & 0x0f) | ((nibbles
& 0x0f) << 4);
113 evbuffer_add(evbuf
, data
, (off
+ 1) / 2);
117 * Support variable length encoding of tags; we use the high bit in each
118 * octet as a continuation signal.
122 evtag_encode_tag(struct evbuffer
*evbuf
, ev_uint32_t tag
)
127 memset(data
, 0, sizeof(data
));
129 ev_uint8_t lower
= tag
& 0x7f;
135 data
[bytes
++] = lower
;
139 evbuffer_add(evbuf
, data
, bytes
);
145 decode_tag_internal(ev_uint32_t
*ptag
, struct evbuffer
*evbuf
, int dodrain
)
147 ev_uint32_t number
= 0;
148 ev_uint8_t
*data
= EVBUFFER_DATA(evbuf
);
149 int len
= EVBUFFER_LENGTH(evbuf
);
150 int count
= 0, shift
= 0, done
= 0;
152 while (count
++ < len
) {
153 ev_uint8_t lower
= *data
++;
154 number
|= (lower
& 0x7f) << shift
;
157 if (!(lower
& 0x80)) {
167 evbuffer_drain(evbuf
, count
);
176 evtag_decode_tag(ev_uint32_t
*ptag
, struct evbuffer
*evbuf
)
178 return (decode_tag_internal(ptag
, evbuf
, 1 /* dodrain */));
182 * Marshal a data type, the general format is as follows:
184 * tag number: one byte; length: var bytes; payload: var bytes
188 evtag_marshal(struct evbuffer
*evbuf
, ev_uint32_t tag
,
189 const void *data
, ev_uint32_t len
)
191 evtag_encode_tag(evbuf
, tag
);
192 encode_int(evbuf
, len
);
193 evbuffer_add(evbuf
, (void *)data
, len
);
196 /* Marshaling for integers */
198 evtag_marshal_int(struct evbuffer
*evbuf
, ev_uint32_t tag
, ev_uint32_t integer
)
200 evbuffer_drain(_buf
, EVBUFFER_LENGTH(_buf
));
201 encode_int(_buf
, integer
);
203 evtag_encode_tag(evbuf
, tag
);
204 encode_int(evbuf
, EVBUFFER_LENGTH(_buf
));
205 evbuffer_add_buffer(evbuf
, _buf
);
209 evtag_marshal_string(struct evbuffer
*buf
, ev_uint32_t tag
, const char *string
)
211 evtag_marshal(buf
, tag
, string
, strlen(string
));
215 evtag_marshal_timeval(struct evbuffer
*evbuf
, ev_uint32_t tag
, struct timeval
*tv
)
217 evbuffer_drain(_buf
, EVBUFFER_LENGTH(_buf
));
219 encode_int(_buf
, tv
->tv_sec
);
220 encode_int(_buf
, tv
->tv_usec
);
222 evtag_marshal(evbuf
, tag
, EVBUFFER_DATA(_buf
),
223 EVBUFFER_LENGTH(_buf
));
227 decode_int_internal(ev_uint32_t
*pnumber
, struct evbuffer
*evbuf
, int dodrain
)
229 ev_uint32_t number
= 0;
230 ev_uint8_t
*data
= EVBUFFER_DATA(evbuf
);
231 int len
= EVBUFFER_LENGTH(evbuf
);
237 nibbles
= ((data
[0] & 0xf0) >> 4) + 1;
238 if (nibbles
> 8 || (nibbles
>> 1) + 1 > len
)
240 len
= (nibbles
>> 1) + 1;
242 while (nibbles
> 0) {
245 number
|= data
[nibbles
>> 1] & 0x0f;
247 number
|= (data
[nibbles
>> 1] & 0xf0) >> 4;
252 evbuffer_drain(evbuf
, len
);
260 evtag_decode_int(ev_uint32_t
*pnumber
, struct evbuffer
*evbuf
)
262 return (decode_int_internal(pnumber
, evbuf
, 1) == -1 ? -1 : 0);
266 evtag_peek(struct evbuffer
*evbuf
, ev_uint32_t
*ptag
)
268 return (decode_tag_internal(ptag
, evbuf
, 0 /* dodrain */));
272 evtag_peek_length(struct evbuffer
*evbuf
, ev_uint32_t
*plength
)
277 len
= decode_tag_internal(NULL
, evbuf
, 0 /* dodrain */);
285 res
= decode_int_internal(plength
, &tmp
, 0);
289 *plength
+= res
+ len
;
295 evtag_payload_length(struct evbuffer
*evbuf
, ev_uint32_t
*plength
)
300 len
= decode_tag_internal(NULL
, evbuf
, 0 /* dodrain */);
308 res
= decode_int_internal(plength
, &tmp
, 0);
316 evtag_consume(struct evbuffer
*evbuf
)
319 if (decode_tag_internal(NULL
, evbuf
, 1 /* dodrain */) == -1)
321 if (evtag_decode_int(&len
, evbuf
) == -1)
323 evbuffer_drain(evbuf
, len
);
328 /* Reads the data type from an event buffer */
331 evtag_unmarshal(struct evbuffer
*src
, ev_uint32_t
*ptag
, struct evbuffer
*dst
)
336 if (decode_tag_internal(ptag
, src
, 1 /* dodrain */) == -1)
338 if (evtag_decode_int(&integer
, src
) == -1)
342 if (EVBUFFER_LENGTH(src
) < len
)
345 if (evbuffer_add(dst
, EVBUFFER_DATA(src
), len
) == -1)
348 evbuffer_drain(src
, len
);
353 /* Marshaling for integers */
356 evtag_unmarshal_int(struct evbuffer
*evbuf
, ev_uint32_t need_tag
,
357 ev_uint32_t
*pinteger
)
363 if (decode_tag_internal(&tag
, evbuf
, 1 /* dodrain */) == -1)
367 if (evtag_decode_int(&integer
, evbuf
) == -1)
371 if (EVBUFFER_LENGTH(evbuf
) < len
)
374 evbuffer_drain(_buf
, EVBUFFER_LENGTH(_buf
));
375 if (evbuffer_add(_buf
, EVBUFFER_DATA(evbuf
), len
) == -1)
378 evbuffer_drain(evbuf
, len
);
380 return (evtag_decode_int(pinteger
, _buf
));
383 /* Unmarshal a fixed length tag */
386 evtag_unmarshal_fixed(struct evbuffer
*src
, ev_uint32_t need_tag
, void *data
,
391 /* Initialize this event buffer so that we can read into it */
392 evbuffer_drain(_buf
, EVBUFFER_LENGTH(_buf
));
394 /* Now unmarshal a tag and check that it matches the tag we want */
395 if (evtag_unmarshal(src
, &tag
, _buf
) == -1 || tag
!= need_tag
)
398 if (EVBUFFER_LENGTH(_buf
) != len
)
401 memcpy(data
, EVBUFFER_DATA(_buf
), len
);
406 evtag_unmarshal_string(struct evbuffer
*evbuf
, ev_uint32_t need_tag
,
411 evbuffer_drain(_buf
, EVBUFFER_LENGTH(_buf
));
413 if (evtag_unmarshal(evbuf
, &tag
, _buf
) == -1 || tag
!= need_tag
)
416 *pstring
= calloc(EVBUFFER_LENGTH(_buf
) + 1, 1);
417 if (*pstring
== NULL
)
418 event_err(1, "%s: calloc", __func__
);
419 evbuffer_remove(_buf
, *pstring
, EVBUFFER_LENGTH(_buf
));
425 evtag_unmarshal_timeval(struct evbuffer
*evbuf
, ev_uint32_t need_tag
,
431 evbuffer_drain(_buf
, EVBUFFER_LENGTH(_buf
));
432 if (evtag_unmarshal(evbuf
, &tag
, _buf
) == -1 || tag
!= need_tag
)
435 if (evtag_decode_int(&integer
, _buf
) == -1)
437 ptv
->tv_sec
= integer
;
438 if (evtag_decode_int(&integer
, _buf
) == -1)
440 ptv
->tv_usec
= integer
;