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);
80 if ((_buf = evbuffer_new()) == NULL)
81 event_err(1,
"%s: malloc", __func__);
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);
111 data[0] = (data[0] & 0x0f) | ((nibbles & 0x0f) << 4);
113 evbuffer_add(evbuf, data, (off + 1) / 2);
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 ));
188 evtag_marshal(
struct evbuffer *evbuf, ev_uint32_t tag,
189 const void *data, ev_uint32_t len)
191 evtag_encode_tag(evbuf, tag);
193 evbuffer_add(evbuf, (
void *)data, len);
198 evtag_marshal_int(
struct evbuffer *evbuf, ev_uint32_t tag, ev_uint32_t integer)
200 evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
203 evtag_encode_tag(evbuf, tag);
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));
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 ));
272 evtag_peek_length(
struct evbuffer *evbuf, ev_uint32_t *plength)
277 len = decode_tag_internal(NULL, evbuf, 0 );
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 );
308 res = decode_int_internal(plength, &tmp, 0);
316 evtag_consume(
struct evbuffer *evbuf)
319 if (decode_tag_internal(NULL, evbuf, 1 ) == -1)
321 if (evtag_decode_int(&len, evbuf) == -1)
323 evbuffer_drain(evbuf, len);
331 evtag_unmarshal(
struct evbuffer *src, ev_uint32_t *ptag,
struct evbuffer *dst)
336 if (decode_tag_internal(ptag, src, 1 ) == -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);
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 ) == -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));
386 evtag_unmarshal_fixed(
struct evbuffer *src, ev_uint32_t need_tag,
void *data,
392 evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
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;