blob: 2e9f4a25bdc1a363c05984fd6eef518c914ae258 [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>
Harald Welteec8b4502010-02-20 20:34:29 +010058
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010059#include <osmocom/core/msgb.h>
Harald Welteec8b4502010-02-20 20:34:29 +010060//#include <openbsc/gsm_data.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010061#include <osmocom/core/talloc.h>
Harald Welteec8b4502010-02-20 20:34:29 +010062//#include <openbsc/debug.h>
63
Neels Hofmeyrf45334b2016-09-16 00:15:56 +020064void *tall_msgb_ctx = NULL;
Harald Welteec8b4502010-02-20 20:34:29 +010065
Neels Hofmeyr87e45502017-06-20 00:17:59 +020066/*! Allocate a new message buffer
Harald Welteba6988b2011-08-17 12:46:48 +020067 * \param[in] size Length in octets, including headroom
68 * \param[in] name Human-readable name to be associated with msgb
Harald Welte2d2e2cc2016-04-25 12:11:20 +020069 * \returns dynamically-allocated \ref msgb
Harald Welteba6988b2011-08-17 12:46:48 +020070 *
71 * This function allocates a 'struct msgb' as well as the underlying
72 * memory buffer for the actual message data (size specified by \a size)
73 * using the talloc memory context previously set by \ref msgb_set_talloc_ctx
74 */
Harald Welteec8b4502010-02-20 20:34:29 +010075struct msgb *msgb_alloc(uint16_t size, const char *name)
76{
77 struct msgb *msg;
78
79 msg = _talloc_zero(tall_msgb_ctx, sizeof(*msg) + size, name);
80
81 if (!msg) {
82 //LOGP(DRSL, LOGL_FATAL, "unable to allocate msgb\n");
83 return NULL;
84 }
85
86 msg->data_len = size;
87 msg->len = 0;
88 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +010089 msg->head = msg->_data;
90 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +010091
92 return msg;
93}
94
Neels Hofmeyr87e45502017-06-20 00:17:59 +020095/*! Release given message buffer
Harald Welteba6988b2011-08-17 12:46:48 +020096 * \param[in] m Message buffer to be free'd
97 */
Harald Welteec8b4502010-02-20 20:34:29 +010098void msgb_free(struct msgb *m)
99{
100 talloc_free(m);
101}
102
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200103/*! Enqueue message buffer to tail of a queue
Harald Welteba6988b2011-08-17 12:46:48 +0200104 * \param[in] queue linked list header of queue
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100105 * \param[in] msg message buffer to be added to the queue
Harald Welteba6988b2011-08-17 12:46:48 +0200106 *
107 * The function will append the specified message buffer \a msg to the
108 * queue implemented by \ref llist_head \a queue
109 */
Harald Welteec8b4502010-02-20 20:34:29 +0100110void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
111{
112 llist_add_tail(&msg->list, queue);
113}
114
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200115/*! Dequeue message buffer from head of queue
Harald Welteba6988b2011-08-17 12:46:48 +0200116 * \param[in] queue linked list header of queue
117 * \returns message buffer (if any) or NULL if queue empty
118 *
119 * The function will remove the first message buffer from the queue
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100120 * implemented by \ref llist_head \a queue.
Harald Welteba6988b2011-08-17 12:46:48 +0200121 */
Harald Welteec8b4502010-02-20 20:34:29 +0100122struct msgb *msgb_dequeue(struct llist_head *queue)
123{
124 struct llist_head *lh;
125
126 if (llist_empty(queue))
127 return NULL;
128
129 lh = queue->next;
Maxd826f172016-06-23 13:14:02 +0200130
131 if (lh) {
132 llist_del(lh);
133 return llist_entry(lh, struct msgb, list);
134 } else
135 return NULL;
Harald Welteec8b4502010-02-20 20:34:29 +0100136}
137
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200138/*! Re-set all message buffer pointers
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100139 * \param[in] msg message buffer that is to be resetted
Harald Welteba6988b2011-08-17 12:46:48 +0200140 *
141 * This will re-set the various internal pointers into the underlying
142 * message buffer, i.e. remvoe all headroom and treat the msgb as
143 * completely empty. It also initializes the control buffer to zero.
144 */
Harald Welteec8b4502010-02-20 20:34:29 +0100145void msgb_reset(struct msgb *msg)
146{
147 msg->len = 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100148 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +0100149 msg->head = msg->_data;
150 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +0100151
Harald Welteec8b4502010-02-20 20:34:29 +0100152 msg->trx = NULL;
153 msg->lchan = NULL;
154 msg->l2h = NULL;
155 msg->l3h = NULL;
Harald Weltebb77c9d2010-04-30 14:26:12 +0200156 msg->l4h = NULL;
Harald Welte95df5c02010-05-01 23:53:26 +0200157
158 memset(&msg->cb, 0, sizeof(msg->cb));
Harald Welteec8b4502010-02-20 20:34:29 +0100159}
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200160
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200161/*! get pointer to data section of message buffer
Harald Welteba6988b2011-08-17 12:46:48 +0200162 * \param[in] msg message buffer
163 * \returns pointer to data section of message buffer
164 */
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200165uint8_t *msgb_data(const struct msgb *msg)
166{
167 return msg->data;
168}
169
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200170/*! get length of message buffer
Harald Welteba6988b2011-08-17 12:46:48 +0200171 * \param[in] msg message buffer
172 * \returns length of data section in message buffer
173 */
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200174uint16_t msgb_length(const struct msgb *msg)
175{
176 return msg->len;
177}
Harald Welte9e1f0602011-06-29 18:46:10 +0200178
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200179/*! Set the talloc context for \ref msgb_alloc
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200180 * Deprecated, use msgb_talloc_ctx_init() instead.
Harald Welteba6988b2011-08-17 12:46:48 +0200181 * \param[in] ctx talloc context to be used as root for msgb allocations
182 */
Harald Welte9e1f0602011-06-29 18:46:10 +0200183void msgb_set_talloc_ctx(void *ctx)
184{
185 tall_msgb_ctx = ctx;
186}
Harald Welteba6988b2011-08-17 12:46:48 +0200187
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200188/*! Initialize a msgb talloc context for \ref msgb_alloc.
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200189 * Create a talloc context called "msgb". If \a pool_size is 0, create a named
190 * const as msgb talloc context. If \a pool_size is nonzero, create a talloc
191 * pool, possibly for faster msgb allocations (see talloc_pool()).
192 * \param[in] root_ctx talloc context used as parent for the new "msgb" ctx.
193 * \param[in] pool_size if nonzero, create a talloc pool of this size.
194 * \returns the new msgb talloc context, e.g. for reporting
195 */
196void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size)
197{
198 if (!pool_size)
199 tall_msgb_ctx = talloc_size(root_ctx, 0);
200 else
201 tall_msgb_ctx = talloc_pool(root_ctx, pool_size);
202 talloc_set_name_const(tall_msgb_ctx, "msgb");
203 return tall_msgb_ctx;
204}
205
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200206/*! Copy an msgb.
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100207 *
208 * This function allocates a new msgb, copies the data buffer of msg,
209 * and adjusts the pointers (incl l1h-l4h) accordingly. The cb part
210 * is not copied.
211 * \param[in] msg The old msgb object
212 * \param[in] name Human-readable name to be associated with msgb
213 */
214struct msgb *msgb_copy(const struct msgb *msg, const char *name)
215{
216 struct msgb *new_msg;
217
218 new_msg = msgb_alloc(msg->data_len, name);
219 if (!new_msg)
220 return NULL;
221
222 /* copy data */
223 memcpy(new_msg->_data, msg->_data, new_msg->data_len);
224
225 /* copy header */
226 new_msg->len = msg->len;
227 new_msg->data += msg->data - msg->_data;
228 new_msg->head += msg->head - msg->_data;
229 new_msg->tail += msg->tail - msg->_data;
230
231 if (msg->l1h)
232 new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
233 if (msg->l2h)
234 new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
235 if (msg->l3h)
236 new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
237 if (msg->l4h)
238 new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
239
240 return new_msg;
241}
242
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200243/*! Resize an area within an msgb
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100244 *
245 * This resizes a sub area of the msgb data and adjusts the pointers (incl
246 * l1h-l4h) accordingly. The cb part is not updated. If the area is extended,
247 * the contents of the extension is undefined. The complete sub area must be a
248 * part of [data,tail].
249 *
250 * \param[inout] msg The msgb object
251 * \param[in] area A pointer to the sub-area
252 * \param[in] old_size The old size of the sub-area
253 * \param[in] new_size The new size of the sub-area
254 * \returns 0 on success, -1 if there is not enough space to extend the area
255 */
256int msgb_resize_area(struct msgb *msg, uint8_t *area,
257 int old_size, int new_size)
258{
259 int rc;
260 uint8_t *post_start = area + old_size;
261 int pre_len = area - msg->data;
262 int post_len = msg->len - old_size - pre_len;
263 int delta_size = new_size - old_size;
264
265 if (old_size < 0 || new_size < 0)
266 MSGB_ABORT(msg, "Negative sizes are not allowed\n");
267 if (area < msg->data || post_start > msg->tail)
268 MSGB_ABORT(msg, "Sub area is not fully contained in the msg data\n");
269
270 if (delta_size == 0)
271 return 0;
272
273 if (delta_size > 0) {
274 rc = msgb_trim(msg, msg->len + delta_size);
275 if (rc < 0)
276 return rc;
277 }
278
279 memmove(area + new_size, area + old_size, post_len);
280
281 if (msg->l1h >= post_start)
282 msg->l1h += delta_size;
283 if (msg->l2h >= post_start)
284 msg->l2h += delta_size;
285 if (msg->l3h >= post_start)
286 msg->l3h += delta_size;
287 if (msg->l4h >= post_start)
288 msg->l4h += delta_size;
289
290 if (delta_size < 0)
291 msgb_trim(msg, msg->len + delta_size);
292
293 return 0;
294}
295
296
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200297/*! Return a (static) buffer containing a hexdump of the msg
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100298 * \param[in] msg message buffer
299 * \returns a pointer to a static char array
300 */
301const char *msgb_hexdump(const struct msgb *msg)
302{
303 static char buf[4100];
304 int buf_offs = 0;
305 int nchars;
306 const unsigned char *start = msg->data;
307 const unsigned char *lxhs[4];
308 int i;
309
310 lxhs[0] = msg->l1h;
311 lxhs[1] = msg->l2h;
312 lxhs[2] = msg->l3h;
313 lxhs[3] = msg->l4h;
314
315 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
316 if (!lxhs[i])
317 continue;
318
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100319 if (lxhs[i] < msg->head)
320 continue;
321 if (lxhs[i] > msg->head + msg->data_len)
322 continue;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100323 if (lxhs[i] > msg->tail)
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100324 continue;
325 if (lxhs[i] < msg->data || lxhs[i] > msg->tail) {
326 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100327 "(L%d=data%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100328 i+1, lxhs[i] - msg->data);
329 buf_offs += nchars;
330 continue;
331 }
332 if (lxhs[i] < start) {
333 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100334 "(L%d%+" PRIdPTR ") ", i+1,
335 start - lxhs[i]);
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100336 buf_offs += nchars;
337 continue;
338 }
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100339 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
340 "%s[L%d]> ",
341 osmo_hexdump(start, lxhs[i] - start),
342 i+1);
343 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
344 return "ERROR";
345
346 buf_offs += nchars;
347 start = lxhs[i];
348 }
349 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
350 "%s", osmo_hexdump(start, msg->tail - start));
351 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
352 return "ERROR";
353
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100354 buf_offs += nchars;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100355
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100356 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
357 if (!lxhs[i])
358 continue;
359
360 if (lxhs[i] < msg->head || lxhs[i] > msg->head + msg->data_len) {
361 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
362 "(L%d out of range) ", i+1);
363 } else if (lxhs[i] <= msg->data + msg->data_len &&
364 lxhs[i] > msg->tail) {
365 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100366 "(L%d=tail%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100367 i+1, lxhs[i] - msg->tail);
368 } else
369 continue;
370
371 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
372 return "ERROR";
373 buf_offs += nchars;
374 }
375
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100376 return buf;
377}
378
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200379/*! @} */