blob: a27100c652ca9a0d201b0920f839af224af806a7 [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 * @{
23 */
24
25/*! \file msgb.c
26 */
Harald Welteec8b4502010-02-20 20:34:29 +010027
28#include <unistd.h>
29#include <string.h>
30#include <stdlib.h>
Neels Hofmeyr42fff582015-12-23 15:12:40 +010031#include <inttypes.h>
Harald Welteec8b4502010-02-20 20:34:29 +010032
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010033#include <osmocom/core/msgb.h>
Harald Welteec8b4502010-02-20 20:34:29 +010034//#include <openbsc/gsm_data.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010035#include <osmocom/core/talloc.h>
Harald Welteec8b4502010-02-20 20:34:29 +010036//#include <openbsc/debug.h>
37
Neels Hofmeyrf45334b2016-09-16 00:15:56 +020038void *tall_msgb_ctx = NULL;
Harald Welteec8b4502010-02-20 20:34:29 +010039
Harald Welteba6988b2011-08-17 12:46:48 +020040/*! \brief Allocate a new message buffer
41 * \param[in] size Length in octets, including headroom
42 * \param[in] name Human-readable name to be associated with msgb
Harald Welte2d2e2cc2016-04-25 12:11:20 +020043 * \returns dynamically-allocated \ref msgb
Harald Welteba6988b2011-08-17 12:46:48 +020044 *
45 * This function allocates a 'struct msgb' as well as the underlying
46 * memory buffer for the actual message data (size specified by \a size)
47 * using the talloc memory context previously set by \ref msgb_set_talloc_ctx
48 */
Harald Welteec8b4502010-02-20 20:34:29 +010049struct msgb *msgb_alloc(uint16_t size, const char *name)
50{
51 struct msgb *msg;
52
53 msg = _talloc_zero(tall_msgb_ctx, sizeof(*msg) + size, name);
54
55 if (!msg) {
56 //LOGP(DRSL, LOGL_FATAL, "unable to allocate msgb\n");
57 return NULL;
58 }
59
60 msg->data_len = size;
61 msg->len = 0;
62 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +010063 msg->head = msg->_data;
64 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +010065
66 return msg;
67}
68
Harald Welteba6988b2011-08-17 12:46:48 +020069/*! \brief Release given message buffer
70 * \param[in] m Message buffer to be free'd
71 */
Harald Welteec8b4502010-02-20 20:34:29 +010072void msgb_free(struct msgb *m)
73{
74 talloc_free(m);
75}
76
Harald Welteba6988b2011-08-17 12:46:48 +020077/*! \brief Enqueue message buffer to tail of a queue
78 * \param[in] queue linked list header of queue
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +010079 * \param[in] msg message buffer to be added to the queue
Harald Welteba6988b2011-08-17 12:46:48 +020080 *
81 * The function will append the specified message buffer \a msg to the
82 * queue implemented by \ref llist_head \a queue
83 */
Harald Welteec8b4502010-02-20 20:34:29 +010084void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
85{
86 llist_add_tail(&msg->list, queue);
87}
88
Harald Welteba6988b2011-08-17 12:46:48 +020089/*! \brief Dequeue message buffer from head of queue
90 * \param[in] queue linked list header of queue
91 * \returns message buffer (if any) or NULL if queue empty
92 *
93 * The function will remove the first message buffer from the queue
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +010094 * implemented by \ref llist_head \a queue.
Harald Welteba6988b2011-08-17 12:46:48 +020095 */
Harald Welteec8b4502010-02-20 20:34:29 +010096struct msgb *msgb_dequeue(struct llist_head *queue)
97{
98 struct llist_head *lh;
99
100 if (llist_empty(queue))
101 return NULL;
102
103 lh = queue->next;
Maxd826f172016-06-23 13:14:02 +0200104
105 if (lh) {
106 llist_del(lh);
107 return llist_entry(lh, struct msgb, list);
108 } else
109 return NULL;
Harald Welteec8b4502010-02-20 20:34:29 +0100110}
111
Harald Welteba6988b2011-08-17 12:46:48 +0200112/*! \brief Re-set all message buffer pointers
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100113 * \param[in] msg message buffer that is to be resetted
Harald Welteba6988b2011-08-17 12:46:48 +0200114 *
115 * This will re-set the various internal pointers into the underlying
116 * message buffer, i.e. remvoe all headroom and treat the msgb as
117 * completely empty. It also initializes the control buffer to zero.
118 */
Harald Welteec8b4502010-02-20 20:34:29 +0100119void msgb_reset(struct msgb *msg)
120{
121 msg->len = 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100122 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +0100123 msg->head = msg->_data;
124 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +0100125
Harald Welteec8b4502010-02-20 20:34:29 +0100126 msg->trx = NULL;
127 msg->lchan = NULL;
128 msg->l2h = NULL;
129 msg->l3h = NULL;
Harald Weltebb77c9d2010-04-30 14:26:12 +0200130 msg->l4h = NULL;
Harald Welte95df5c02010-05-01 23:53:26 +0200131
132 memset(&msg->cb, 0, sizeof(msg->cb));
Harald Welteec8b4502010-02-20 20:34:29 +0100133}
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200134
Harald Welteba6988b2011-08-17 12:46:48 +0200135/*! \brief get pointer to data section of message buffer
136 * \param[in] msg message buffer
137 * \returns pointer to data section of message buffer
138 */
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200139uint8_t *msgb_data(const struct msgb *msg)
140{
141 return msg->data;
142}
143
Harald Welteba6988b2011-08-17 12:46:48 +0200144/*! \brief get length of message buffer
145 * \param[in] msg message buffer
146 * \returns length of data section in message buffer
147 */
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200148uint16_t msgb_length(const struct msgb *msg)
149{
150 return msg->len;
151}
Harald Welte9e1f0602011-06-29 18:46:10 +0200152
Harald Welteba6988b2011-08-17 12:46:48 +0200153/*! \brief Set the talloc context for \ref msgb_alloc
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200154 * Deprecated, use msgb_talloc_ctx_init() instead.
Harald Welteba6988b2011-08-17 12:46:48 +0200155 * \param[in] ctx talloc context to be used as root for msgb allocations
156 */
Harald Welte9e1f0602011-06-29 18:46:10 +0200157void msgb_set_talloc_ctx(void *ctx)
158{
159 tall_msgb_ctx = ctx;
160}
Harald Welteba6988b2011-08-17 12:46:48 +0200161
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200162/*! \brief Initialize a msgb talloc context for \ref msgb_alloc.
163 * Create a talloc context called "msgb". If \a pool_size is 0, create a named
164 * const as msgb talloc context. If \a pool_size is nonzero, create a talloc
165 * pool, possibly for faster msgb allocations (see talloc_pool()).
166 * \param[in] root_ctx talloc context used as parent for the new "msgb" ctx.
167 * \param[in] pool_size if nonzero, create a talloc pool of this size.
168 * \returns the new msgb talloc context, e.g. for reporting
169 */
170void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size)
171{
172 if (!pool_size)
173 tall_msgb_ctx = talloc_size(root_ctx, 0);
174 else
175 tall_msgb_ctx = talloc_pool(root_ctx, pool_size);
176 talloc_set_name_const(tall_msgb_ctx, "msgb");
177 return tall_msgb_ctx;
178}
179
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100180/*! \brief Copy an msgb.
181 *
182 * This function allocates a new msgb, copies the data buffer of msg,
183 * and adjusts the pointers (incl l1h-l4h) accordingly. The cb part
184 * is not copied.
185 * \param[in] msg The old msgb object
186 * \param[in] name Human-readable name to be associated with msgb
187 */
188struct msgb *msgb_copy(const struct msgb *msg, const char *name)
189{
190 struct msgb *new_msg;
191
192 new_msg = msgb_alloc(msg->data_len, name);
193 if (!new_msg)
194 return NULL;
195
196 /* copy data */
197 memcpy(new_msg->_data, msg->_data, new_msg->data_len);
198
199 /* copy header */
200 new_msg->len = msg->len;
201 new_msg->data += msg->data - msg->_data;
202 new_msg->head += msg->head - msg->_data;
203 new_msg->tail += msg->tail - msg->_data;
204
205 if (msg->l1h)
206 new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
207 if (msg->l2h)
208 new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
209 if (msg->l3h)
210 new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
211 if (msg->l4h)
212 new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
213
214 return new_msg;
215}
216
217/*! \brief Resize an area within an msgb
218 *
219 * This resizes a sub area of the msgb data and adjusts the pointers (incl
220 * l1h-l4h) accordingly. The cb part is not updated. If the area is extended,
221 * the contents of the extension is undefined. The complete sub area must be a
222 * part of [data,tail].
223 *
224 * \param[inout] msg The msgb object
225 * \param[in] area A pointer to the sub-area
226 * \param[in] old_size The old size of the sub-area
227 * \param[in] new_size The new size of the sub-area
228 * \returns 0 on success, -1 if there is not enough space to extend the area
229 */
230int msgb_resize_area(struct msgb *msg, uint8_t *area,
231 int old_size, int new_size)
232{
233 int rc;
234 uint8_t *post_start = area + old_size;
235 int pre_len = area - msg->data;
236 int post_len = msg->len - old_size - pre_len;
237 int delta_size = new_size - old_size;
238
239 if (old_size < 0 || new_size < 0)
240 MSGB_ABORT(msg, "Negative sizes are not allowed\n");
241 if (area < msg->data || post_start > msg->tail)
242 MSGB_ABORT(msg, "Sub area is not fully contained in the msg data\n");
243
244 if (delta_size == 0)
245 return 0;
246
247 if (delta_size > 0) {
248 rc = msgb_trim(msg, msg->len + delta_size);
249 if (rc < 0)
250 return rc;
251 }
252
253 memmove(area + new_size, area + old_size, post_len);
254
255 if (msg->l1h >= post_start)
256 msg->l1h += delta_size;
257 if (msg->l2h >= post_start)
258 msg->l2h += delta_size;
259 if (msg->l3h >= post_start)
260 msg->l3h += delta_size;
261 if (msg->l4h >= post_start)
262 msg->l4h += delta_size;
263
264 if (delta_size < 0)
265 msgb_trim(msg, msg->len + delta_size);
266
267 return 0;
268}
269
270
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100271/*! \brief Return a (static) buffer containing a hexdump of the msg
272 * \param[in] msg message buffer
273 * \returns a pointer to a static char array
274 */
275const char *msgb_hexdump(const struct msgb *msg)
276{
277 static char buf[4100];
278 int buf_offs = 0;
279 int nchars;
280 const unsigned char *start = msg->data;
281 const unsigned char *lxhs[4];
282 int i;
283
284 lxhs[0] = msg->l1h;
285 lxhs[1] = msg->l2h;
286 lxhs[2] = msg->l3h;
287 lxhs[3] = msg->l4h;
288
289 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
290 if (!lxhs[i])
291 continue;
292
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100293 if (lxhs[i] < msg->head)
294 continue;
295 if (lxhs[i] > msg->head + msg->data_len)
296 continue;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100297 if (lxhs[i] > msg->tail)
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100298 continue;
299 if (lxhs[i] < msg->data || lxhs[i] > msg->tail) {
300 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100301 "(L%d=data%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100302 i+1, lxhs[i] - msg->data);
303 buf_offs += nchars;
304 continue;
305 }
306 if (lxhs[i] < start) {
307 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100308 "(L%d%+" PRIdPTR ") ", i+1,
309 start - lxhs[i]);
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100310 buf_offs += nchars;
311 continue;
312 }
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100313 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
314 "%s[L%d]> ",
315 osmo_hexdump(start, lxhs[i] - start),
316 i+1);
317 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
318 return "ERROR";
319
320 buf_offs += nchars;
321 start = lxhs[i];
322 }
323 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
324 "%s", osmo_hexdump(start, msg->tail - start));
325 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
326 return "ERROR";
327
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100328 buf_offs += nchars;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100329
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100330 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
331 if (!lxhs[i])
332 continue;
333
334 if (lxhs[i] < msg->head || lxhs[i] > msg->head + msg->data_len) {
335 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
336 "(L%d out of range) ", i+1);
337 } else if (lxhs[i] <= msg->data + msg->data_len &&
338 lxhs[i] > msg->tail) {
339 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100340 "(L%d=tail%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100341 i+1, lxhs[i] - msg->tail);
342 } else
343 continue;
344
345 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
346 return "ERROR";
347 buf_offs += nchars;
348 }
349
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100350 return buf;
351}
352
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200353/*! @} */