blob: 6fcbe53a111ff417b7b72f17b3e96fd7309eefb5 [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +02002 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welteec8b4502010-02-20 20:34:29 +01003 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
Harald Welteba6988b2011-08-17 12:46:48 +020021/*! \addtogroup msgb
22 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020023 *
Neels Hofmeyr87e45502017-06-20 00:17:59 +020024 * libosmocore message buffers, inspired by Linux kernel skbuff
Harald Welte96e2a002017-06-12 21:44:18 +020025 *
26 * Inspired by the 'struct skbuff' of the Linux kernel, we implement a
27 * 'struct msgb' which we use for handling network
28 * packets aka messages aka PDUs.
29 *
30 * A msgb consists of
31 * * a header with some metadata, such as
32 * * a linked list header for message queues or the like
33 * * pointers to the headers of various protocol layers inside
34 * the packet
35 * * a data section consisting of
36 * * headroom, i.e. space in front of the message, to allow
37 * for additional headers being pushed in front of the current
38 * data
39 * * the curently occupied data for the message
40 * * tailroom, i.e. space at the end of the message, to
41 * allow more data to be added after the end of the current
42 * data
43 *
44 * We have plenty of utility functions around the \ref msgb:
45 * * allocation / release
46 * * enqueue / dequeue from/to message queues
47 * * prepending (pushing) and appending (putting) data
48 * * copying / resizing
49 * * hex-dumping to a string for debug purposes
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020050 *
51 * \file msgb.c
Harald Welteba6988b2011-08-17 12:46:48 +020052 */
53
Harald Welteec8b4502010-02-20 20:34:29 +010054#include <unistd.h>
55#include <string.h>
56#include <stdlib.h>
Neels Hofmeyr42fff582015-12-23 15:12:40 +010057#include <inttypes.h>
Philipp Maierc5b47cc2017-10-10 16:53:21 +020058#include <stdarg.h>
59#include <errno.h>
60
Harald Welteec8b4502010-02-20 20:34:29 +010061
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010062#include <osmocom/core/msgb.h>
Harald Welteec8b4502010-02-20 20:34:29 +010063//#include <openbsc/gsm_data.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010064#include <osmocom/core/talloc.h>
Harald Welteec8b4502010-02-20 20:34:29 +010065//#include <openbsc/debug.h>
66
Neels Hofmeyrf45334b2016-09-16 00:15:56 +020067void *tall_msgb_ctx = NULL;
Harald Welteec8b4502010-02-20 20:34:29 +010068
Neels Hofmeyr87e45502017-06-20 00:17:59 +020069/*! Allocate a new message buffer
Harald Welteba6988b2011-08-17 12:46:48 +020070 * \param[in] size Length in octets, including headroom
71 * \param[in] name Human-readable name to be associated with msgb
Harald Welte2d2e2cc2016-04-25 12:11:20 +020072 * \returns dynamically-allocated \ref msgb
Harald Welteba6988b2011-08-17 12:46:48 +020073 *
74 * This function allocates a 'struct msgb' as well as the underlying
75 * memory buffer for the actual message data (size specified by \a size)
76 * using the talloc memory context previously set by \ref msgb_set_talloc_ctx
77 */
Harald Welteec8b4502010-02-20 20:34:29 +010078struct msgb *msgb_alloc(uint16_t size, const char *name)
79{
80 struct msgb *msg;
81
82 msg = _talloc_zero(tall_msgb_ctx, sizeof(*msg) + size, name);
83
84 if (!msg) {
85 //LOGP(DRSL, LOGL_FATAL, "unable to allocate msgb\n");
86 return NULL;
87 }
88
89 msg->data_len = size;
90 msg->len = 0;
91 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +010092 msg->head = msg->_data;
93 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +010094
95 return msg;
96}
97
Neels Hofmeyr87e45502017-06-20 00:17:59 +020098/*! Release given message buffer
Harald Welteba6988b2011-08-17 12:46:48 +020099 * \param[in] m Message buffer to be free'd
100 */
Harald Welteec8b4502010-02-20 20:34:29 +0100101void msgb_free(struct msgb *m)
102{
103 talloc_free(m);
104}
105
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200106/*! Enqueue message buffer to tail of a queue
Harald Welteba6988b2011-08-17 12:46:48 +0200107 * \param[in] queue linked list header of queue
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100108 * \param[in] msg message buffer to be added to the queue
Harald Welteba6988b2011-08-17 12:46:48 +0200109 *
110 * The function will append the specified message buffer \a msg to the
111 * queue implemented by \ref llist_head \a queue
112 */
Harald Welteec8b4502010-02-20 20:34:29 +0100113void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
114{
115 llist_add_tail(&msg->list, queue);
116}
117
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200118/*! Dequeue message buffer from head of queue
Harald Welteba6988b2011-08-17 12:46:48 +0200119 * \param[in] queue linked list header of queue
120 * \returns message buffer (if any) or NULL if queue empty
121 *
122 * The function will remove the first message buffer from the queue
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100123 * implemented by \ref llist_head \a queue.
Harald Welteba6988b2011-08-17 12:46:48 +0200124 */
Harald Welteec8b4502010-02-20 20:34:29 +0100125struct msgb *msgb_dequeue(struct llist_head *queue)
126{
127 struct llist_head *lh;
128
129 if (llist_empty(queue))
130 return NULL;
131
132 lh = queue->next;
Maxd826f172016-06-23 13:14:02 +0200133
134 if (lh) {
135 llist_del(lh);
136 return llist_entry(lh, struct msgb, list);
137 } else
138 return NULL;
Harald Welteec8b4502010-02-20 20:34:29 +0100139}
140
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200141/*! Re-set all message buffer pointers
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100142 * \param[in] msg message buffer that is to be resetted
Harald Welteba6988b2011-08-17 12:46:48 +0200143 *
144 * This will re-set the various internal pointers into the underlying
145 * message buffer, i.e. remvoe all headroom and treat the msgb as
146 * completely empty. It also initializes the control buffer to zero.
147 */
Harald Welteec8b4502010-02-20 20:34:29 +0100148void msgb_reset(struct msgb *msg)
149{
150 msg->len = 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100151 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +0100152 msg->head = msg->_data;
153 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +0100154
Harald Welteec8b4502010-02-20 20:34:29 +0100155 msg->trx = NULL;
156 msg->lchan = NULL;
157 msg->l2h = NULL;
158 msg->l3h = NULL;
Harald Weltebb77c9d2010-04-30 14:26:12 +0200159 msg->l4h = NULL;
Harald Welte95df5c02010-05-01 23:53:26 +0200160
161 memset(&msg->cb, 0, sizeof(msg->cb));
Harald Welteec8b4502010-02-20 20:34:29 +0100162}
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200163
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200164/*! get pointer to data section of message buffer
Harald Welteba6988b2011-08-17 12:46:48 +0200165 * \param[in] msg message buffer
166 * \returns pointer to data section of message buffer
167 */
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200168uint8_t *msgb_data(const struct msgb *msg)
169{
170 return msg->data;
171}
172
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200173/*! get length of message buffer
Harald Welteba6988b2011-08-17 12:46:48 +0200174 * \param[in] msg message buffer
175 * \returns length of data section in message buffer
176 */
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200177uint16_t msgb_length(const struct msgb *msg)
178{
179 return msg->len;
180}
Harald Welte9e1f0602011-06-29 18:46:10 +0200181
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200182/*! Set the talloc context for \ref msgb_alloc
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200183 * Deprecated, use msgb_talloc_ctx_init() instead.
Harald Welteba6988b2011-08-17 12:46:48 +0200184 * \param[in] ctx talloc context to be used as root for msgb allocations
185 */
Harald Welte9e1f0602011-06-29 18:46:10 +0200186void msgb_set_talloc_ctx(void *ctx)
187{
188 tall_msgb_ctx = ctx;
189}
Harald Welteba6988b2011-08-17 12:46:48 +0200190
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200191/*! Initialize a msgb talloc context for \ref msgb_alloc.
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200192 * Create a talloc context called "msgb". If \a pool_size is 0, create a named
193 * const as msgb talloc context. If \a pool_size is nonzero, create a talloc
194 * pool, possibly for faster msgb allocations (see talloc_pool()).
195 * \param[in] root_ctx talloc context used as parent for the new "msgb" ctx.
196 * \param[in] pool_size if nonzero, create a talloc pool of this size.
197 * \returns the new msgb talloc context, e.g. for reporting
198 */
199void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size)
200{
201 if (!pool_size)
202 tall_msgb_ctx = talloc_size(root_ctx, 0);
203 else
204 tall_msgb_ctx = talloc_pool(root_ctx, pool_size);
205 talloc_set_name_const(tall_msgb_ctx, "msgb");
206 return tall_msgb_ctx;
207}
208
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200209/*! Copy an msgb.
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100210 *
211 * This function allocates a new msgb, copies the data buffer of msg,
212 * and adjusts the pointers (incl l1h-l4h) accordingly. The cb part
213 * is not copied.
214 * \param[in] msg The old msgb object
215 * \param[in] name Human-readable name to be associated with msgb
216 */
217struct msgb *msgb_copy(const struct msgb *msg, const char *name)
218{
219 struct msgb *new_msg;
220
221 new_msg = msgb_alloc(msg->data_len, name);
222 if (!new_msg)
223 return NULL;
224
225 /* copy data */
226 memcpy(new_msg->_data, msg->_data, new_msg->data_len);
227
228 /* copy header */
229 new_msg->len = msg->len;
230 new_msg->data += msg->data - msg->_data;
231 new_msg->head += msg->head - msg->_data;
232 new_msg->tail += msg->tail - msg->_data;
233
234 if (msg->l1h)
235 new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
236 if (msg->l2h)
237 new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
238 if (msg->l3h)
239 new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
240 if (msg->l4h)
241 new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
242
243 return new_msg;
244}
245
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200246/*! Resize an area within an msgb
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100247 *
248 * This resizes a sub area of the msgb data and adjusts the pointers (incl
249 * l1h-l4h) accordingly. The cb part is not updated. If the area is extended,
250 * the contents of the extension is undefined. The complete sub area must be a
251 * part of [data,tail].
252 *
253 * \param[inout] msg The msgb object
254 * \param[in] area A pointer to the sub-area
255 * \param[in] old_size The old size of the sub-area
256 * \param[in] new_size The new size of the sub-area
257 * \returns 0 on success, -1 if there is not enough space to extend the area
258 */
259int msgb_resize_area(struct msgb *msg, uint8_t *area,
260 int old_size, int new_size)
261{
262 int rc;
263 uint8_t *post_start = area + old_size;
264 int pre_len = area - msg->data;
265 int post_len = msg->len - old_size - pre_len;
266 int delta_size = new_size - old_size;
267
268 if (old_size < 0 || new_size < 0)
269 MSGB_ABORT(msg, "Negative sizes are not allowed\n");
270 if (area < msg->data || post_start > msg->tail)
271 MSGB_ABORT(msg, "Sub area is not fully contained in the msg data\n");
272
273 if (delta_size == 0)
274 return 0;
275
276 if (delta_size > 0) {
277 rc = msgb_trim(msg, msg->len + delta_size);
278 if (rc < 0)
279 return rc;
280 }
281
282 memmove(area + new_size, area + old_size, post_len);
283
284 if (msg->l1h >= post_start)
285 msg->l1h += delta_size;
286 if (msg->l2h >= post_start)
287 msg->l2h += delta_size;
288 if (msg->l3h >= post_start)
289 msg->l3h += delta_size;
290 if (msg->l4h >= post_start)
291 msg->l4h += delta_size;
292
293 if (delta_size < 0)
294 msgb_trim(msg, msg->len + delta_size);
295
296 return 0;
297}
298
299
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200300/*! Return a (static) buffer containing a hexdump of the msg
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100301 * \param[in] msg message buffer
302 * \returns a pointer to a static char array
303 */
304const char *msgb_hexdump(const struct msgb *msg)
305{
306 static char buf[4100];
307 int buf_offs = 0;
308 int nchars;
309 const unsigned char *start = msg->data;
310 const unsigned char *lxhs[4];
311 int i;
312
313 lxhs[0] = msg->l1h;
314 lxhs[1] = msg->l2h;
315 lxhs[2] = msg->l3h;
316 lxhs[3] = msg->l4h;
317
318 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
319 if (!lxhs[i])
320 continue;
321
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100322 if (lxhs[i] < msg->head)
323 continue;
324 if (lxhs[i] > msg->head + msg->data_len)
325 continue;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100326 if (lxhs[i] > msg->tail)
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100327 continue;
328 if (lxhs[i] < msg->data || lxhs[i] > msg->tail) {
329 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100330 "(L%d=data%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100331 i+1, lxhs[i] - msg->data);
332 buf_offs += nchars;
333 continue;
334 }
335 if (lxhs[i] < start) {
336 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100337 "(L%d%+" PRIdPTR ") ", i+1,
338 start - lxhs[i]);
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100339 buf_offs += nchars;
340 continue;
341 }
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100342 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
343 "%s[L%d]> ",
344 osmo_hexdump(start, lxhs[i] - start),
345 i+1);
346 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
347 return "ERROR";
348
349 buf_offs += nchars;
350 start = lxhs[i];
351 }
352 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
353 "%s", osmo_hexdump(start, msg->tail - start));
354 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
355 return "ERROR";
356
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100357 buf_offs += nchars;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100358
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100359 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
360 if (!lxhs[i])
361 continue;
362
363 if (lxhs[i] < msg->head || lxhs[i] > msg->head + msg->data_len) {
364 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
365 "(L%d out of range) ", i+1);
366 } else if (lxhs[i] <= msg->data + msg->data_len &&
367 lxhs[i] > msg->tail) {
368 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100369 "(L%d=tail%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100370 i+1, lxhs[i] - msg->tail);
371 } else
372 continue;
373
374 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
375 return "ERROR";
376 buf_offs += nchars;
377 }
378
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100379 return buf;
380}
381
Philipp Maierc5b47cc2017-10-10 16:53:21 +0200382
383/*! Print a string to the end of message buffer.
384 * \param[in] msg message buffer
385 * \returns 0 on success, -EINVAL on error
386 *
387 * The resulting string is printed to the msgb without a trailing nul
388 * character. A nul following the data tail may be written as an implementation
389 * detail, but a trailing nul is never part of the msgb data in terms of
390 * msgb_length().
391 *
392 * Note: the tailroom must always be one byte longer than the string to be
393 * written. The msgb is filled only up to tailroom=1. This is an implementation
394 * detail that allows leaving a nul character behind the valid data.
395 *
396 * In case of error, the msgb remains unchanged, though data may have been
397 * written to the (unused) memory after the tail pointer.
398 */
399int msgb_printf(struct msgb *msgb, const char *format, ...)
400{
401 va_list args;
402 int str_len;
403 int rc = 0;
404
405 OSMO_ASSERT(msgb);
406 OSMO_ASSERT(format);
407
408 /* Regardless of what we plan to add to the buffer, we must at least
409 * be able to store a string terminator (nullstring) */
410 if (msgb_tailroom(msgb) < 1)
411 return -EINVAL;
412
413 va_start(args, format);
414
415 str_len =
416 vsnprintf((char *)msgb->tail, msgb_tailroom(msgb), format, args);
417
418 if (str_len >= msgb_tailroom(msgb) || str_len < 0) {
419 rc = -EINVAL;
420 } else
421 msgb_put(msgb, str_len);
422
423 va_end(args);
424 return rc;
425}
426
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200427/*! @} */