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.
28 #include <sys/types.h>
29 #include <sys/param.h>
36 #define WIN32_LEAN_AND_MEAN
38 #undef WIN32_LEAN_AND_MEAN
40 #include <sys/ioctl.h>
44 #include <sys/queue.h>
45 #ifdef HAVE_SYS_TIME_H
61 int decode_int(uint32_t *pnumber
, struct evbuffer
*evbuf
);
63 static struct evbuffer
*_buf
; /* not thread safe */
71 if ((_buf
= evbuffer_new()) == NULL
)
72 event_err(1, "%s: malloc", __func__
);
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.
82 encode_int(struct evbuffer
*evbuf
, uint32_t number
)
84 int off
= 1, nibbles
= 0;
87 memset(data
, 0, sizeof(data
));
90 data
[off
/2] = (data
[off
/2] & 0xf0) | (number
& 0x0f);
92 data
[off
/2] = (data
[off
/2] & 0x0f) |
93 ((number
& 0x0f) << 4);
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
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 */
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
);
135 evtag_marshal_string(struct evbuffer
*buf
, uint8_t tag
, const char *string
)
137 evtag_marshal(buf
, tag
, string
, strlen(string
));
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
));
153 decode_int_internal(uint32_t *pnumber
, struct evbuffer
*evbuf
, int dodrain
)
156 uint8_t *data
= EVBUFFER_DATA(evbuf
);
157 int len
= EVBUFFER_LENGTH(evbuf
);
158 int nibbles
= 0, off
;
163 nibbles
= ((data
[0] & 0xf0) >> 4) + 1;
164 if (nibbles
> 8 || (nibbles
>> 1) > len
- 1)
171 number
|= data
[off
>> 1] & 0x0f;
173 number
|= (data
[off
>> 1] & 0xf0) >> 4;
177 len
= (nibbles
>> 1) + 1;
179 evbuffer_drain(evbuf
, 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)
197 *ptag
= EVBUFFER_DATA(evbuf
)[0];
203 evtag_peek_length(struct evbuffer
*evbuf
, uint32_t *plength
)
208 if (EVBUFFER_LENGTH(evbuf
) < 2)
215 res
= decode_int_internal(plength
, &tmp
, 0);
225 evtag_payload_length(struct evbuffer
*evbuf
, uint32_t *plength
)
230 if (EVBUFFER_LENGTH(evbuf
) < 2)
237 res
= decode_int_internal(plength
, &tmp
, 0);
245 evtag_consume(struct evbuffer
*evbuf
)
248 evbuffer_drain(evbuf
, 1);
249 if (decode_int(&len
, evbuf
) == -1)
251 evbuffer_drain(evbuf
, len
);
256 /* Reads the data type from an event buffer */
259 evtag_unmarshal(struct evbuffer
*src
, uint8_t *ptag
, struct evbuffer
*dst
)
265 if (evbuffer_remove(src
, &tag
, sizeof(tag
)) != sizeof(tag
))
267 if (decode_int(&integer
, src
) == -1)
271 if (EVBUFFER_LENGTH(src
) < len
)
274 if (evbuffer_add(dst
, EVBUFFER_DATA(src
), len
) == -1)
277 evbuffer_drain(src
, len
);
283 /* Marshaling for integers */
286 evtag_unmarshal_int(struct evbuffer
*evbuf
, uint8_t need_tag
,
293 if (evbuffer_remove(evbuf
, &tag
, sizeof(tag
)) != sizeof(tag
) ||
296 if (decode_int(&integer
, evbuf
) == -1)
300 if (EVBUFFER_LENGTH(evbuf
) < len
)
303 evbuffer_drain(_buf
, EVBUFFER_LENGTH(_buf
));
304 if (evbuffer_add(_buf
, EVBUFFER_DATA(evbuf
), len
) == -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
,
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
)
327 if (EVBUFFER_LENGTH(_buf
) != len
)
330 memcpy(data
, EVBUFFER_DATA(_buf
), len
);
335 evtag_unmarshal_string(struct evbuffer
*evbuf
, uint8_t need_tag
,
340 evbuffer_drain(_buf
, EVBUFFER_LENGTH(_buf
));
342 if (evtag_unmarshal(evbuf
, &tag
, _buf
) == -1 || tag
!= need_tag
)
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
));
354 evtag_unmarshal_timeval(struct evbuffer
*evbuf
, uint8_t need_tag
,
360 evbuffer_drain(_buf
, EVBUFFER_LENGTH(_buf
));
361 if (evtag_unmarshal(evbuf
, &tag
, _buf
) == -1 || tag
!= need_tag
)
364 if (decode_int(&integer
, _buf
) == -1)
366 ptv
->tv_sec
= integer
;
367 if (decode_int(&integer
, _buf
) == -1)
369 ptv
->tv_usec
= integer
;