blob: f09da90d7a58c14e15d781c700c0b196c867a485 [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;
104 llist_del(lh);
105
106 return llist_entry(lh, struct msgb, list);
107}
108
Harald Welteba6988b2011-08-17 12:46:48 +0200109/*! \brief Re-set all message buffer pointers
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100110 * \param[in] msg message buffer that is to be resetted
Harald Welteba6988b2011-08-17 12:46:48 +0200111 *
112 * This will re-set the various internal pointers into the underlying
113 * message buffer, i.e. remvoe all headroom and treat the msgb as
114 * completely empty. It also initializes the control buffer to zero.
115 */
Harald Welteec8b4502010-02-20 20:34:29 +0100116void msgb_reset(struct msgb *msg)
117{
118 msg->len = 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100119 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +0100120 msg->head = msg->_data;
121 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +0100122
Harald Welteec8b4502010-02-20 20:34:29 +0100123 msg->trx = NULL;
124 msg->lchan = NULL;
125 msg->l2h = NULL;
126 msg->l3h = NULL;
Harald Weltebb77c9d2010-04-30 14:26:12 +0200127 msg->l4h = NULL;
Harald Welte95df5c02010-05-01 23:53:26 +0200128
129 memset(&msg->cb, 0, sizeof(msg->cb));
Harald Welteec8b4502010-02-20 20:34:29 +0100130}
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200131
Harald Welteba6988b2011-08-17 12:46:48 +0200132/*! \brief get pointer to data section of message buffer
133 * \param[in] msg message buffer
134 * \returns pointer to data section of message buffer
135 */
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200136uint8_t *msgb_data(const struct msgb *msg)
137{
138 return msg->data;
139}
140
Harald Welteba6988b2011-08-17 12:46:48 +0200141/*! \brief get length of message buffer
142 * \param[in] msg message buffer
143 * \returns length of data section in message buffer
144 */
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200145uint16_t msgb_length(const struct msgb *msg)
146{
147 return msg->len;
148}
Harald Welte9e1f0602011-06-29 18:46:10 +0200149
Harald Welteba6988b2011-08-17 12:46:48 +0200150/*! \brief Set the talloc context for \ref msgb_alloc
151 * \param[in] ctx talloc context to be used as root for msgb allocations
152 */
Harald Welte9e1f0602011-06-29 18:46:10 +0200153void msgb_set_talloc_ctx(void *ctx)
154{
155 tall_msgb_ctx = ctx;
156}
Harald Welteba6988b2011-08-17 12:46:48 +0200157
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100158/*! \brief Copy an msgb.
159 *
160 * This function allocates a new msgb, copies the data buffer of msg,
161 * and adjusts the pointers (incl l1h-l4h) accordingly. The cb part
162 * is not copied.
163 * \param[in] msg The old msgb object
164 * \param[in] name Human-readable name to be associated with msgb
165 */
166struct msgb *msgb_copy(const struct msgb *msg, const char *name)
167{
168 struct msgb *new_msg;
169
170 new_msg = msgb_alloc(msg->data_len, name);
171 if (!new_msg)
172 return NULL;
173
174 /* copy data */
175 memcpy(new_msg->_data, msg->_data, new_msg->data_len);
176
177 /* copy header */
178 new_msg->len = msg->len;
179 new_msg->data += msg->data - msg->_data;
180 new_msg->head += msg->head - msg->_data;
181 new_msg->tail += msg->tail - msg->_data;
182
183 if (msg->l1h)
184 new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
185 if (msg->l2h)
186 new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
187 if (msg->l3h)
188 new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
189 if (msg->l4h)
190 new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
191
192 return new_msg;
193}
194
195/*! \brief Resize an area within an msgb
196 *
197 * This resizes a sub area of the msgb data and adjusts the pointers (incl
198 * l1h-l4h) accordingly. The cb part is not updated. If the area is extended,
199 * the contents of the extension is undefined. The complete sub area must be a
200 * part of [data,tail].
201 *
202 * \param[inout] msg The msgb object
203 * \param[in] area A pointer to the sub-area
204 * \param[in] old_size The old size of the sub-area
205 * \param[in] new_size The new size of the sub-area
206 * \returns 0 on success, -1 if there is not enough space to extend the area
207 */
208int msgb_resize_area(struct msgb *msg, uint8_t *area,
209 int old_size, int new_size)
210{
211 int rc;
212 uint8_t *post_start = area + old_size;
213 int pre_len = area - msg->data;
214 int post_len = msg->len - old_size - pre_len;
215 int delta_size = new_size - old_size;
216
217 if (old_size < 0 || new_size < 0)
218 MSGB_ABORT(msg, "Negative sizes are not allowed\n");
219 if (area < msg->data || post_start > msg->tail)
220 MSGB_ABORT(msg, "Sub area is not fully contained in the msg data\n");
221
222 if (delta_size == 0)
223 return 0;
224
225 if (delta_size > 0) {
226 rc = msgb_trim(msg, msg->len + delta_size);
227 if (rc < 0)
228 return rc;
229 }
230
231 memmove(area + new_size, area + old_size, post_len);
232
233 if (msg->l1h >= post_start)
234 msg->l1h += delta_size;
235 if (msg->l2h >= post_start)
236 msg->l2h += delta_size;
237 if (msg->l3h >= post_start)
238 msg->l3h += delta_size;
239 if (msg->l4h >= post_start)
240 msg->l4h += delta_size;
241
242 if (delta_size < 0)
243 msgb_trim(msg, msg->len + delta_size);
244
245 return 0;
246}
247
248
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100249/*! \brief Return a (static) buffer containing a hexdump of the msg
250 * \param[in] msg message buffer
251 * \returns a pointer to a static char array
252 */
253const char *msgb_hexdump(const struct msgb *msg)
254{
255 static char buf[4100];
256 int buf_offs = 0;
257 int nchars;
258 const unsigned char *start = msg->data;
259 const unsigned char *lxhs[4];
260 int i;
261
262 lxhs[0] = msg->l1h;
263 lxhs[1] = msg->l2h;
264 lxhs[2] = msg->l3h;
265 lxhs[3] = msg->l4h;
266
267 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
268 if (!lxhs[i])
269 continue;
270
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100271 if (lxhs[i] < msg->head)
272 continue;
273 if (lxhs[i] > msg->head + msg->data_len)
274 continue;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100275 if (lxhs[i] > msg->tail)
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100276 continue;
277 if (lxhs[i] < msg->data || lxhs[i] > msg->tail) {
278 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100279 "(L%d=data%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100280 i+1, lxhs[i] - msg->data);
281 buf_offs += nchars;
282 continue;
283 }
284 if (lxhs[i] < start) {
285 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100286 "(L%d%+" PRIdPTR ") ", i+1,
287 start - lxhs[i]);
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100288 buf_offs += nchars;
289 continue;
290 }
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100291 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
292 "%s[L%d]> ",
293 osmo_hexdump(start, lxhs[i] - start),
294 i+1);
295 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
296 return "ERROR";
297
298 buf_offs += nchars;
299 start = lxhs[i];
300 }
301 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
302 "%s", osmo_hexdump(start, msg->tail - start));
303 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
304 return "ERROR";
305
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100306 buf_offs += nchars;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100307
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100308 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
309 if (!lxhs[i])
310 continue;
311
312 if (lxhs[i] < msg->head || lxhs[i] > msg->head + msg->data_len) {
313 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
314 "(L%d out of range) ", i+1);
315 } else if (lxhs[i] <= msg->data + msg->data_len &&
316 lxhs[i] > msg->tail) {
317 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100318 "(L%d=tail%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100319 i+1, lxhs[i] - msg->tail);
320 } else
321 continue;
322
323 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
324 return "ERROR";
325 buf_offs += nchars;
326 }
327
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100328 return buf;
329}
330
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200331/*! @} */