blob: 136ad8860aa9c95f88582a2c487b63a90decf161 [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 * @{
Harald Welte96e2a002017-06-12 21:44:18 +020023 * \brief libosmocore message buffers, inspired by Linux kernel skbuff
24 *
25 * Inspired by the 'struct skbuff' of the Linux kernel, we implement a
26 * 'struct msgb' which we use for handling network
27 * packets aka messages aka PDUs.
28 *
29 * A msgb consists of
30 * * a header with some metadata, such as
31 * * a linked list header for message queues or the like
32 * * pointers to the headers of various protocol layers inside
33 * the packet
34 * * a data section consisting of
35 * * headroom, i.e. space in front of the message, to allow
36 * for additional headers being pushed in front of the current
37 * data
38 * * the curently occupied data for the message
39 * * tailroom, i.e. space at the end of the message, to
40 * allow more data to be added after the end of the current
41 * data
42 *
43 * We have plenty of utility functions around the \ref msgb:
44 * * allocation / release
45 * * enqueue / dequeue from/to message queues
46 * * prepending (pushing) and appending (putting) data
47 * * copying / resizing
48 * * hex-dumping to a string for debug purposes
Harald Welteba6988b2011-08-17 12:46:48 +020049 */
50
Harald Welte96e2a002017-06-12 21:44:18 +020051/*! \file msgb.c */
Harald Welteec8b4502010-02-20 20:34:29 +010052
53#include <unistd.h>
54#include <string.h>
55#include <stdlib.h>
Neels Hofmeyr42fff582015-12-23 15:12:40 +010056#include <inttypes.h>
Harald Welteec8b4502010-02-20 20:34:29 +010057
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010058#include <osmocom/core/msgb.h>
Harald Welteec8b4502010-02-20 20:34:29 +010059//#include <openbsc/gsm_data.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010060#include <osmocom/core/talloc.h>
Harald Welteec8b4502010-02-20 20:34:29 +010061//#include <openbsc/debug.h>
62
Neels Hofmeyrf45334b2016-09-16 00:15:56 +020063void *tall_msgb_ctx = NULL;
Harald Welteec8b4502010-02-20 20:34:29 +010064
Harald Welteba6988b2011-08-17 12:46:48 +020065/*! \brief Allocate a new message buffer
66 * \param[in] size Length in octets, including headroom
67 * \param[in] name Human-readable name to be associated with msgb
Harald Welte2d2e2cc2016-04-25 12:11:20 +020068 * \returns dynamically-allocated \ref msgb
Harald Welteba6988b2011-08-17 12:46:48 +020069 *
70 * This function allocates a 'struct msgb' as well as the underlying
71 * memory buffer for the actual message data (size specified by \a size)
72 * using the talloc memory context previously set by \ref msgb_set_talloc_ctx
73 */
Harald Welteec8b4502010-02-20 20:34:29 +010074struct msgb *msgb_alloc(uint16_t size, const char *name)
75{
76 struct msgb *msg;
77
78 msg = _talloc_zero(tall_msgb_ctx, sizeof(*msg) + size, name);
79
80 if (!msg) {
81 //LOGP(DRSL, LOGL_FATAL, "unable to allocate msgb\n");
82 return NULL;
83 }
84
85 msg->data_len = size;
86 msg->len = 0;
87 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +010088 msg->head = msg->_data;
89 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +010090
91 return msg;
92}
93
Harald Welteba6988b2011-08-17 12:46:48 +020094/*! \brief Release given message buffer
95 * \param[in] m Message buffer to be free'd
96 */
Harald Welteec8b4502010-02-20 20:34:29 +010097void msgb_free(struct msgb *m)
98{
99 talloc_free(m);
100}
101
Harald Welteba6988b2011-08-17 12:46:48 +0200102/*! \brief Enqueue message buffer to tail of a queue
103 * \param[in] queue linked list header of queue
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100104 * \param[in] msg message buffer to be added to the queue
Harald Welteba6988b2011-08-17 12:46:48 +0200105 *
106 * The function will append the specified message buffer \a msg to the
107 * queue implemented by \ref llist_head \a queue
108 */
Harald Welteec8b4502010-02-20 20:34:29 +0100109void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
110{
111 llist_add_tail(&msg->list, queue);
112}
113
Harald Welteba6988b2011-08-17 12:46:48 +0200114/*! \brief Dequeue message buffer from head of queue
115 * \param[in] queue linked list header of queue
116 * \returns message buffer (if any) or NULL if queue empty
117 *
118 * The function will remove the first message buffer from the queue
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100119 * implemented by \ref llist_head \a queue.
Harald Welteba6988b2011-08-17 12:46:48 +0200120 */
Harald Welteec8b4502010-02-20 20:34:29 +0100121struct msgb *msgb_dequeue(struct llist_head *queue)
122{
123 struct llist_head *lh;
124
125 if (llist_empty(queue))
126 return NULL;
127
128 lh = queue->next;
Maxd826f172016-06-23 13:14:02 +0200129
130 if (lh) {
131 llist_del(lh);
132 return llist_entry(lh, struct msgb, list);
133 } else
134 return NULL;
Harald Welteec8b4502010-02-20 20:34:29 +0100135}
136
Harald Welteba6988b2011-08-17 12:46:48 +0200137/*! \brief Re-set all message buffer pointers
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100138 * \param[in] msg message buffer that is to be resetted
Harald Welteba6988b2011-08-17 12:46:48 +0200139 *
140 * This will re-set the various internal pointers into the underlying
141 * message buffer, i.e. remvoe all headroom and treat the msgb as
142 * completely empty. It also initializes the control buffer to zero.
143 */
Harald Welteec8b4502010-02-20 20:34:29 +0100144void msgb_reset(struct msgb *msg)
145{
146 msg->len = 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100147 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +0100148 msg->head = msg->_data;
149 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +0100150
Harald Welteec8b4502010-02-20 20:34:29 +0100151 msg->trx = NULL;
152 msg->lchan = NULL;
153 msg->l2h = NULL;
154 msg->l3h = NULL;
Harald Weltebb77c9d2010-04-30 14:26:12 +0200155 msg->l4h = NULL;
Harald Welte95df5c02010-05-01 23:53:26 +0200156
157 memset(&msg->cb, 0, sizeof(msg->cb));
Harald Welteec8b4502010-02-20 20:34:29 +0100158}
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200159
Harald Welteba6988b2011-08-17 12:46:48 +0200160/*! \brief get pointer to data section of message buffer
161 * \param[in] msg message buffer
162 * \returns pointer to data section of message buffer
163 */
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200164uint8_t *msgb_data(const struct msgb *msg)
165{
166 return msg->data;
167}
168
Harald Welteba6988b2011-08-17 12:46:48 +0200169/*! \brief get length of message buffer
170 * \param[in] msg message buffer
171 * \returns length of data section in message buffer
172 */
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200173uint16_t msgb_length(const struct msgb *msg)
174{
175 return msg->len;
176}
Harald Welte9e1f0602011-06-29 18:46:10 +0200177
Harald Welteba6988b2011-08-17 12:46:48 +0200178/*! \brief Set the talloc context for \ref msgb_alloc
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200179 * Deprecated, use msgb_talloc_ctx_init() instead.
Harald Welteba6988b2011-08-17 12:46:48 +0200180 * \param[in] ctx talloc context to be used as root for msgb allocations
181 */
Harald Welte9e1f0602011-06-29 18:46:10 +0200182void msgb_set_talloc_ctx(void *ctx)
183{
184 tall_msgb_ctx = ctx;
185}
Harald Welteba6988b2011-08-17 12:46:48 +0200186
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200187/*! \brief Initialize a msgb talloc context for \ref msgb_alloc.
188 * Create a talloc context called "msgb". If \a pool_size is 0, create a named
189 * const as msgb talloc context. If \a pool_size is nonzero, create a talloc
190 * pool, possibly for faster msgb allocations (see talloc_pool()).
191 * \param[in] root_ctx talloc context used as parent for the new "msgb" ctx.
192 * \param[in] pool_size if nonzero, create a talloc pool of this size.
193 * \returns the new msgb talloc context, e.g. for reporting
194 */
195void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size)
196{
197 if (!pool_size)
198 tall_msgb_ctx = talloc_size(root_ctx, 0);
199 else
200 tall_msgb_ctx = talloc_pool(root_ctx, pool_size);
201 talloc_set_name_const(tall_msgb_ctx, "msgb");
202 return tall_msgb_ctx;
203}
204
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100205/*! \brief Copy an msgb.
206 *
207 * This function allocates a new msgb, copies the data buffer of msg,
208 * and adjusts the pointers (incl l1h-l4h) accordingly. The cb part
209 * is not copied.
210 * \param[in] msg The old msgb object
211 * \param[in] name Human-readable name to be associated with msgb
212 */
213struct msgb *msgb_copy(const struct msgb *msg, const char *name)
214{
215 struct msgb *new_msg;
216
217 new_msg = msgb_alloc(msg->data_len, name);
218 if (!new_msg)
219 return NULL;
220
221 /* copy data */
222 memcpy(new_msg->_data, msg->_data, new_msg->data_len);
223
224 /* copy header */
225 new_msg->len = msg->len;
226 new_msg->data += msg->data - msg->_data;
227 new_msg->head += msg->head - msg->_data;
228 new_msg->tail += msg->tail - msg->_data;
229
230 if (msg->l1h)
231 new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
232 if (msg->l2h)
233 new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
234 if (msg->l3h)
235 new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
236 if (msg->l4h)
237 new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
238
239 return new_msg;
240}
241
242/*! \brief Resize an area within an msgb
243 *
244 * This resizes a sub area of the msgb data and adjusts the pointers (incl
245 * l1h-l4h) accordingly. The cb part is not updated. If the area is extended,
246 * the contents of the extension is undefined. The complete sub area must be a
247 * part of [data,tail].
248 *
249 * \param[inout] msg The msgb object
250 * \param[in] area A pointer to the sub-area
251 * \param[in] old_size The old size of the sub-area
252 * \param[in] new_size The new size of the sub-area
253 * \returns 0 on success, -1 if there is not enough space to extend the area
254 */
255int msgb_resize_area(struct msgb *msg, uint8_t *area,
256 int old_size, int new_size)
257{
258 int rc;
259 uint8_t *post_start = area + old_size;
260 int pre_len = area - msg->data;
261 int post_len = msg->len - old_size - pre_len;
262 int delta_size = new_size - old_size;
263
264 if (old_size < 0 || new_size < 0)
265 MSGB_ABORT(msg, "Negative sizes are not allowed\n");
266 if (area < msg->data || post_start > msg->tail)
267 MSGB_ABORT(msg, "Sub area is not fully contained in the msg data\n");
268
269 if (delta_size == 0)
270 return 0;
271
272 if (delta_size > 0) {
273 rc = msgb_trim(msg, msg->len + delta_size);
274 if (rc < 0)
275 return rc;
276 }
277
278 memmove(area + new_size, area + old_size, post_len);
279
280 if (msg->l1h >= post_start)
281 msg->l1h += delta_size;
282 if (msg->l2h >= post_start)
283 msg->l2h += delta_size;
284 if (msg->l3h >= post_start)
285 msg->l3h += delta_size;
286 if (msg->l4h >= post_start)
287 msg->l4h += delta_size;
288
289 if (delta_size < 0)
290 msgb_trim(msg, msg->len + delta_size);
291
292 return 0;
293}
294
295
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100296/*! \brief Return a (static) buffer containing a hexdump of the msg
297 * \param[in] msg message buffer
298 * \returns a pointer to a static char array
299 */
300const char *msgb_hexdump(const struct msgb *msg)
301{
302 static char buf[4100];
303 int buf_offs = 0;
304 int nchars;
305 const unsigned char *start = msg->data;
306 const unsigned char *lxhs[4];
307 int i;
308
309 lxhs[0] = msg->l1h;
310 lxhs[1] = msg->l2h;
311 lxhs[2] = msg->l3h;
312 lxhs[3] = msg->l4h;
313
314 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
315 if (!lxhs[i])
316 continue;
317
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100318 if (lxhs[i] < msg->head)
319 continue;
320 if (lxhs[i] > msg->head + msg->data_len)
321 continue;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100322 if (lxhs[i] > msg->tail)
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100323 continue;
324 if (lxhs[i] < msg->data || lxhs[i] > msg->tail) {
325 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100326 "(L%d=data%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100327 i+1, lxhs[i] - msg->data);
328 buf_offs += nchars;
329 continue;
330 }
331 if (lxhs[i] < start) {
332 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100333 "(L%d%+" PRIdPTR ") ", i+1,
334 start - lxhs[i]);
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100335 buf_offs += nchars;
336 continue;
337 }
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100338 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
339 "%s[L%d]> ",
340 osmo_hexdump(start, lxhs[i] - start),
341 i+1);
342 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
343 return "ERROR";
344
345 buf_offs += nchars;
346 start = lxhs[i];
347 }
348 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
349 "%s", osmo_hexdump(start, msg->tail - start));
350 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
351 return "ERROR";
352
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100353 buf_offs += nchars;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100354
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100355 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
356 if (!lxhs[i])
357 continue;
358
359 if (lxhs[i] < msg->head || lxhs[i] > msg->head + msg->data_len) {
360 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
361 "(L%d out of range) ", i+1);
362 } else if (lxhs[i] <= msg->data + msg->data_len &&
363 lxhs[i] > msg->tail) {
364 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100365 "(L%d=tail%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100366 i+1, lxhs[i] - msg->tail);
367 } else
368 continue;
369
370 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
371 return "ERROR";
372 buf_offs += nchars;
373 }
374
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100375 return buf;
376}
377
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200378/*! @} */