blob: 9efaea567f201a5774a6392fc6341bdb38723cb5 [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 *
Harald Weltee08da972017-11-13 01:00:26 +09005 * SPDX-License-Identifier: GPL-2.0+
6 *
Harald Welteec8b4502010-02-20 20:34:29 +01007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Harald Welteba6988b2011-08-17 12:46:48 +020023/*! \addtogroup msgb
24 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020025 *
Neels Hofmeyr87e45502017-06-20 00:17:59 +020026 * libosmocore message buffers, inspired by Linux kernel skbuff
Harald Welte96e2a002017-06-12 21:44:18 +020027 *
28 * Inspired by the 'struct skbuff' of the Linux kernel, we implement a
29 * 'struct msgb' which we use for handling network
30 * packets aka messages aka PDUs.
31 *
32 * A msgb consists of
33 * * a header with some metadata, such as
34 * * a linked list header for message queues or the like
35 * * pointers to the headers of various protocol layers inside
36 * the packet
37 * * a data section consisting of
38 * * headroom, i.e. space in front of the message, to allow
39 * for additional headers being pushed in front of the current
40 * data
Vadim Yanitskiy8c8e6d72018-03-08 21:10:23 +070041 * * the currently occupied data for the message
Harald Welte96e2a002017-06-12 21:44:18 +020042 * * tailroom, i.e. space at the end of the message, to
43 * allow more data to be added after the end of the current
44 * data
45 *
46 * We have plenty of utility functions around the \ref msgb:
47 * * allocation / release
48 * * enqueue / dequeue from/to message queues
49 * * prepending (pushing) and appending (putting) data
50 * * copying / resizing
51 * * hex-dumping to a string for debug purposes
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020052 *
53 * \file msgb.c
Harald Welteba6988b2011-08-17 12:46:48 +020054 */
55
Harald Welteec8b4502010-02-20 20:34:29 +010056#include <unistd.h>
57#include <string.h>
58#include <stdlib.h>
Neels Hofmeyr42fff582015-12-23 15:12:40 +010059#include <inttypes.h>
Philipp Maierc5b47cc2017-10-10 16:53:21 +020060#include <stdarg.h>
61#include <errno.h>
62
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010063#include <osmocom/core/msgb.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010064#include <osmocom/core/talloc.h>
Harald Welteec8b4502010-02-20 20:34:29 +010065
Neels Hofmeyrf45334b2016-09-16 00:15:56 +020066void *tall_msgb_ctx = NULL;
Harald Welteec8b4502010-02-20 20:34:29 +010067
Neels Hofmeyr87e45502017-06-20 00:17:59 +020068/*! Allocate a new message buffer
Harald Welteba6988b2011-08-17 12:46:48 +020069 * \param[in] size Length in octets, including headroom
70 * \param[in] name Human-readable name to be associated with msgb
Harald Welte2d2e2cc2016-04-25 12:11:20 +020071 * \returns dynamically-allocated \ref msgb
Harald Welteba6988b2011-08-17 12:46:48 +020072 *
73 * This function allocates a 'struct msgb' as well as the underlying
74 * memory buffer for the actual message data (size specified by \a size)
75 * using the talloc memory context previously set by \ref msgb_set_talloc_ctx
76 */
Harald Welteec8b4502010-02-20 20:34:29 +010077struct msgb *msgb_alloc(uint16_t size, const char *name)
78{
79 struct msgb *msg;
80
81 msg = _talloc_zero(tall_msgb_ctx, sizeof(*msg) + size, name);
82
83 if (!msg) {
84 //LOGP(DRSL, LOGL_FATAL, "unable to allocate msgb\n");
85 return NULL;
86 }
87
88 msg->data_len = size;
89 msg->len = 0;
90 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +010091 msg->head = msg->_data;
92 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +010093
94 return msg;
95}
96
Neels Hofmeyr87e45502017-06-20 00:17:59 +020097/*! Release given message buffer
Vadim Yanitskiy8c8e6d72018-03-08 21:10:23 +070098 * \param[in] m Message buffer to be freed
Harald Welteba6988b2011-08-17 12:46:48 +020099 */
Harald Welteec8b4502010-02-20 20:34:29 +0100100void msgb_free(struct msgb *m)
101{
102 talloc_free(m);
103}
104
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200105/*! Enqueue message buffer to tail of a queue
Harald Welteba6988b2011-08-17 12:46:48 +0200106 * \param[in] queue linked list header of queue
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100107 * \param[in] msg message buffer to be added to the queue
Harald Welteba6988b2011-08-17 12:46:48 +0200108 *
109 * The function will append the specified message buffer \a msg to the
110 * queue implemented by \ref llist_head \a queue
111 */
Harald Welteec8b4502010-02-20 20:34:29 +0100112void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
113{
114 llist_add_tail(&msg->list, queue);
115}
116
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200117/*! Dequeue message buffer from head of queue
Harald Welteba6988b2011-08-17 12:46:48 +0200118 * \param[in] queue linked list header of queue
119 * \returns message buffer (if any) or NULL if queue empty
120 *
121 * The function will remove the first message buffer from the queue
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100122 * implemented by \ref llist_head \a queue.
Harald Welteba6988b2011-08-17 12:46:48 +0200123 */
Harald Welteec8b4502010-02-20 20:34:29 +0100124struct msgb *msgb_dequeue(struct llist_head *queue)
125{
126 struct llist_head *lh;
127
128 if (llist_empty(queue))
129 return NULL;
130
131 lh = queue->next;
Maxd826f172016-06-23 13:14:02 +0200132
133 if (lh) {
134 llist_del(lh);
135 return llist_entry(lh, struct msgb, list);
136 } else
137 return NULL;
Harald Welteec8b4502010-02-20 20:34:29 +0100138}
139
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200140/*! Re-set all message buffer pointers
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100141 * \param[in] msg message buffer that is to be resetted
Harald Welteba6988b2011-08-17 12:46:48 +0200142 *
143 * This will re-set the various internal pointers into the underlying
Vadim Yanitskiy8c8e6d72018-03-08 21:10:23 +0700144 * message buffer, i.e. remove all headroom and treat the msgb as
Harald Welteba6988b2011-08-17 12:46:48 +0200145 * completely empty. It also initializes the control buffer to zero.
146 */
Harald Welteec8b4502010-02-20 20:34:29 +0100147void msgb_reset(struct msgb *msg)
148{
149 msg->len = 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100150 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +0100151 msg->head = msg->_data;
152 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +0100153
Harald Welteec8b4502010-02-20 20:34:29 +0100154 msg->trx = NULL;
155 msg->lchan = NULL;
156 msg->l2h = NULL;
157 msg->l3h = NULL;
Harald Weltebb77c9d2010-04-30 14:26:12 +0200158 msg->l4h = NULL;
Harald Welte95df5c02010-05-01 23:53:26 +0200159
160 memset(&msg->cb, 0, sizeof(msg->cb));
Harald Welteec8b4502010-02-20 20:34:29 +0100161}
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200162
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200163/*! get pointer to data section of message buffer
Harald Welteba6988b2011-08-17 12:46:48 +0200164 * \param[in] msg message buffer
165 * \returns pointer to data section of message buffer
166 */
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200167uint8_t *msgb_data(const struct msgb *msg)
168{
169 return msg->data;
170}
171
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200172/*! get length of message buffer
Harald Welteba6988b2011-08-17 12:46:48 +0200173 * \param[in] msg message buffer
174 * \returns length of data section in message buffer
175 */
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200176uint16_t msgb_length(const struct msgb *msg)
177{
178 return msg->len;
179}
Harald Welte9e1f0602011-06-29 18:46:10 +0200180
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200181/*! Set the talloc context for \ref msgb_alloc
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200182 * Deprecated, use msgb_talloc_ctx_init() instead.
Harald Welteba6988b2011-08-17 12:46:48 +0200183 * \param[in] ctx talloc context to be used as root for msgb allocations
184 */
Harald Welte9e1f0602011-06-29 18:46:10 +0200185void msgb_set_talloc_ctx(void *ctx)
186{
187 tall_msgb_ctx = ctx;
188}
Harald Welteba6988b2011-08-17 12:46:48 +0200189
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200190/*! Initialize a msgb talloc context for \ref msgb_alloc.
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200191 * Create a talloc context called "msgb". If \a pool_size is 0, create a named
192 * const as msgb talloc context. If \a pool_size is nonzero, create a talloc
193 * pool, possibly for faster msgb allocations (see talloc_pool()).
194 * \param[in] root_ctx talloc context used as parent for the new "msgb" ctx.
195 * \param[in] pool_size if nonzero, create a talloc pool of this size.
196 * \returns the new msgb talloc context, e.g. for reporting
197 */
198void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size)
199{
200 if (!pool_size)
201 tall_msgb_ctx = talloc_size(root_ctx, 0);
202 else
203 tall_msgb_ctx = talloc_pool(root_ctx, pool_size);
204 talloc_set_name_const(tall_msgb_ctx, "msgb");
205 return tall_msgb_ctx;
206}
207
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200208/*! Copy an msgb.
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100209 *
210 * This function allocates a new msgb, copies the data buffer of msg,
211 * and adjusts the pointers (incl l1h-l4h) accordingly. The cb part
212 * is not copied.
213 * \param[in] msg The old msgb object
214 * \param[in] name Human-readable name to be associated with msgb
215 */
216struct msgb *msgb_copy(const struct msgb *msg, const char *name)
217{
218 struct msgb *new_msg;
219
220 new_msg = msgb_alloc(msg->data_len, name);
221 if (!new_msg)
222 return NULL;
223
224 /* copy data */
225 memcpy(new_msg->_data, msg->_data, new_msg->data_len);
226
227 /* copy header */
228 new_msg->len = msg->len;
229 new_msg->data += msg->data - msg->_data;
230 new_msg->head += msg->head - msg->_data;
231 new_msg->tail += msg->tail - msg->_data;
232
233 if (msg->l1h)
234 new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
235 if (msg->l2h)
236 new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
237 if (msg->l3h)
238 new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
239 if (msg->l4h)
240 new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
241
242 return new_msg;
243}
244
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200245/*! Resize an area within an msgb
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100246 *
247 * This resizes a sub area of the msgb data and adjusts the pointers (incl
248 * l1h-l4h) accordingly. The cb part is not updated. If the area is extended,
249 * the contents of the extension is undefined. The complete sub area must be a
250 * part of [data,tail].
251 *
252 * \param[inout] msg The msgb object
253 * \param[in] area A pointer to the sub-area
254 * \param[in] old_size The old size of the sub-area
255 * \param[in] new_size The new size of the sub-area
256 * \returns 0 on success, -1 if there is not enough space to extend the area
257 */
258int msgb_resize_area(struct msgb *msg, uint8_t *area,
259 int old_size, int new_size)
260{
261 int rc;
262 uint8_t *post_start = area + old_size;
263 int pre_len = area - msg->data;
264 int post_len = msg->len - old_size - pre_len;
265 int delta_size = new_size - old_size;
266
267 if (old_size < 0 || new_size < 0)
268 MSGB_ABORT(msg, "Negative sizes are not allowed\n");
269 if (area < msg->data || post_start > msg->tail)
270 MSGB_ABORT(msg, "Sub area is not fully contained in the msg data\n");
271
272 if (delta_size == 0)
273 return 0;
274
275 if (delta_size > 0) {
276 rc = msgb_trim(msg, msg->len + delta_size);
277 if (rc < 0)
278 return rc;
279 }
280
281 memmove(area + new_size, area + old_size, post_len);
282
283 if (msg->l1h >= post_start)
284 msg->l1h += delta_size;
285 if (msg->l2h >= post_start)
286 msg->l2h += delta_size;
287 if (msg->l3h >= post_start)
288 msg->l3h += delta_size;
289 if (msg->l4h >= post_start)
290 msg->l4h += delta_size;
291
292 if (delta_size < 0)
293 msgb_trim(msg, msg->len + delta_size);
294
295 return 0;
296}
297
298
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200299/*! Return a (static) buffer containing a hexdump of the msg
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100300 * \param[in] msg message buffer
301 * \returns a pointer to a static char array
302 */
303const char *msgb_hexdump(const struct msgb *msg)
304{
305 static char buf[4100];
306 int buf_offs = 0;
307 int nchars;
308 const unsigned char *start = msg->data;
309 const unsigned char *lxhs[4];
310 int i;
311
312 lxhs[0] = msg->l1h;
313 lxhs[1] = msg->l2h;
314 lxhs[2] = msg->l3h;
315 lxhs[3] = msg->l4h;
316
317 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
318 if (!lxhs[i])
319 continue;
320
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100321 if (lxhs[i] < msg->head)
322 continue;
323 if (lxhs[i] > msg->head + msg->data_len)
324 continue;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100325 if (lxhs[i] > msg->tail)
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100326 continue;
327 if (lxhs[i] < msg->data || lxhs[i] > msg->tail) {
328 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100329 "(L%d=data%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100330 i+1, lxhs[i] - msg->data);
331 buf_offs += nchars;
332 continue;
333 }
334 if (lxhs[i] < start) {
335 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100336 "(L%d%+" PRIdPTR ") ", i+1,
337 start - lxhs[i]);
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100338 buf_offs += nchars;
339 continue;
340 }
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100341 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
342 "%s[L%d]> ",
343 osmo_hexdump(start, lxhs[i] - start),
344 i+1);
345 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
346 return "ERROR";
347
348 buf_offs += nchars;
349 start = lxhs[i];
350 }
351 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
352 "%s", osmo_hexdump(start, msg->tail - start));
353 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
354 return "ERROR";
355
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100356 buf_offs += nchars;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100357
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100358 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
359 if (!lxhs[i])
360 continue;
361
362 if (lxhs[i] < msg->head || lxhs[i] > msg->head + msg->data_len) {
363 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
364 "(L%d out of range) ", i+1);
365 } else if (lxhs[i] <= msg->data + msg->data_len &&
366 lxhs[i] > msg->tail) {
367 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100368 "(L%d=tail%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100369 i+1, lxhs[i] - msg->tail);
370 } else
371 continue;
372
373 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
374 return "ERROR";
375 buf_offs += nchars;
376 }
377
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100378 return buf;
379}
380
Philipp Maierc5b47cc2017-10-10 16:53:21 +0200381
382/*! Print a string to the end of message buffer.
383 * \param[in] msg message buffer
384 * \returns 0 on success, -EINVAL on error
385 *
386 * The resulting string is printed to the msgb without a trailing nul
387 * character. A nul following the data tail may be written as an implementation
388 * detail, but a trailing nul is never part of the msgb data in terms of
389 * msgb_length().
390 *
391 * Note: the tailroom must always be one byte longer than the string to be
392 * written. The msgb is filled only up to tailroom=1. This is an implementation
393 * detail that allows leaving a nul character behind the valid data.
394 *
395 * In case of error, the msgb remains unchanged, though data may have been
396 * written to the (unused) memory after the tail pointer.
397 */
398int msgb_printf(struct msgb *msgb, const char *format, ...)
399{
400 va_list args;
401 int str_len;
402 int rc = 0;
403
404 OSMO_ASSERT(msgb);
405 OSMO_ASSERT(format);
406
407 /* Regardless of what we plan to add to the buffer, we must at least
408 * be able to store a string terminator (nullstring) */
409 if (msgb_tailroom(msgb) < 1)
410 return -EINVAL;
411
412 va_start(args, format);
413
414 str_len =
415 vsnprintf((char *)msgb->tail, msgb_tailroom(msgb), format, args);
416
417 if (str_len >= msgb_tailroom(msgb) || str_len < 0) {
418 rc = -EINVAL;
419 } else
420 msgb_put(msgb, str_len);
421
422 va_end(args);
423 return rc;
424}
425
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200426/*! @} */