blob: ea8dc827f1ef7ee299ab11d43a657dfa084ecb59 [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
38void *tall_msgb_ctx;
39
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
154 * \param[in] ctx talloc context to be used as root for msgb allocations
155 */
Harald Welte9e1f0602011-06-29 18:46:10 +0200156void msgb_set_talloc_ctx(void *ctx)
157{
158 tall_msgb_ctx = ctx;
159}
Harald Welteba6988b2011-08-17 12:46:48 +0200160
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100161/*! \brief Copy an msgb.
162 *
163 * This function allocates a new msgb, copies the data buffer of msg,
164 * and adjusts the pointers (incl l1h-l4h) accordingly. The cb part
165 * is not copied.
166 * \param[in] msg The old msgb object
167 * \param[in] name Human-readable name to be associated with msgb
168 */
169struct msgb *msgb_copy(const struct msgb *msg, const char *name)
170{
171 struct msgb *new_msg;
172
173 new_msg = msgb_alloc(msg->data_len, name);
174 if (!new_msg)
175 return NULL;
176
177 /* copy data */
178 memcpy(new_msg->_data, msg->_data, new_msg->data_len);
179
180 /* copy header */
181 new_msg->len = msg->len;
182 new_msg->data += msg->data - msg->_data;
183 new_msg->head += msg->head - msg->_data;
184 new_msg->tail += msg->tail - msg->_data;
185
186 if (msg->l1h)
187 new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
188 if (msg->l2h)
189 new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
190 if (msg->l3h)
191 new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
192 if (msg->l4h)
193 new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
194
195 return new_msg;
196}
197
198/*! \brief Resize an area within an msgb
199 *
200 * This resizes a sub area of the msgb data and adjusts the pointers (incl
201 * l1h-l4h) accordingly. The cb part is not updated. If the area is extended,
202 * the contents of the extension is undefined. The complete sub area must be a
203 * part of [data,tail].
204 *
205 * \param[inout] msg The msgb object
206 * \param[in] area A pointer to the sub-area
207 * \param[in] old_size The old size of the sub-area
208 * \param[in] new_size The new size of the sub-area
209 * \returns 0 on success, -1 if there is not enough space to extend the area
210 */
211int msgb_resize_area(struct msgb *msg, uint8_t *area,
212 int old_size, int new_size)
213{
214 int rc;
215 uint8_t *post_start = area + old_size;
216 int pre_len = area - msg->data;
217 int post_len = msg->len - old_size - pre_len;
218 int delta_size = new_size - old_size;
219
220 if (old_size < 0 || new_size < 0)
221 MSGB_ABORT(msg, "Negative sizes are not allowed\n");
222 if (area < msg->data || post_start > msg->tail)
223 MSGB_ABORT(msg, "Sub area is not fully contained in the msg data\n");
224
225 if (delta_size == 0)
226 return 0;
227
228 if (delta_size > 0) {
229 rc = msgb_trim(msg, msg->len + delta_size);
230 if (rc < 0)
231 return rc;
232 }
233
234 memmove(area + new_size, area + old_size, post_len);
235
236 if (msg->l1h >= post_start)
237 msg->l1h += delta_size;
238 if (msg->l2h >= post_start)
239 msg->l2h += delta_size;
240 if (msg->l3h >= post_start)
241 msg->l3h += delta_size;
242 if (msg->l4h >= post_start)
243 msg->l4h += delta_size;
244
245 if (delta_size < 0)
246 msgb_trim(msg, msg->len + delta_size);
247
248 return 0;
249}
250
251
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100252/*! \brief Return a (static) buffer containing a hexdump of the msg
253 * \param[in] msg message buffer
254 * \returns a pointer to a static char array
255 */
256const char *msgb_hexdump(const struct msgb *msg)
257{
258 static char buf[4100];
259 int buf_offs = 0;
260 int nchars;
261 const unsigned char *start = msg->data;
262 const unsigned char *lxhs[4];
263 int i;
264
265 lxhs[0] = msg->l1h;
266 lxhs[1] = msg->l2h;
267 lxhs[2] = msg->l3h;
268 lxhs[3] = msg->l4h;
269
270 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
271 if (!lxhs[i])
272 continue;
273
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100274 if (lxhs[i] < msg->head)
275 continue;
276 if (lxhs[i] > msg->head + msg->data_len)
277 continue;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100278 if (lxhs[i] > msg->tail)
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100279 continue;
280 if (lxhs[i] < msg->data || lxhs[i] > msg->tail) {
281 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100282 "(L%d=data%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100283 i+1, lxhs[i] - msg->data);
284 buf_offs += nchars;
285 continue;
286 }
287 if (lxhs[i] < start) {
288 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100289 "(L%d%+" PRIdPTR ") ", i+1,
290 start - lxhs[i]);
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100291 buf_offs += nchars;
292 continue;
293 }
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100294 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
295 "%s[L%d]> ",
296 osmo_hexdump(start, lxhs[i] - start),
297 i+1);
298 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
299 return "ERROR";
300
301 buf_offs += nchars;
302 start = lxhs[i];
303 }
304 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
305 "%s", osmo_hexdump(start, msg->tail - start));
306 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
307 return "ERROR";
308
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100309 buf_offs += nchars;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100310
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100311 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
312 if (!lxhs[i])
313 continue;
314
315 if (lxhs[i] < msg->head || lxhs[i] > msg->head + msg->data_len) {
316 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
317 "(L%d out of range) ", i+1);
318 } else if (lxhs[i] <= msg->data + msg->data_len &&
319 lxhs[i] > msg->tail) {
320 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100321 "(L%d=tail%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100322 i+1, lxhs[i] - msg->tail);
323 } else
324 continue;
325
326 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
327 return "ERROR";
328 buf_offs += nchars;
329 }
330
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100331 return buf;
332}
333
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200334/*! @} */