blob: 636191302dda96567663ead46c981c412d6b7a9e [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
43 *
44 * This function allocates a 'struct msgb' as well as the underlying
45 * memory buffer for the actual message data (size specified by \a size)
46 * using the talloc memory context previously set by \ref msgb_set_talloc_ctx
47 */
Harald Welteec8b4502010-02-20 20:34:29 +010048struct msgb *msgb_alloc(uint16_t size, const char *name)
49{
50 struct msgb *msg;
51
52 msg = _talloc_zero(tall_msgb_ctx, sizeof(*msg) + size, name);
53
54 if (!msg) {
55 //LOGP(DRSL, LOGL_FATAL, "unable to allocate msgb\n");
56 return NULL;
57 }
58
59 msg->data_len = size;
60 msg->len = 0;
61 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +010062 msg->head = msg->_data;
63 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +010064
65 return msg;
66}
67
Harald Welteba6988b2011-08-17 12:46:48 +020068/*! \brief Release given message buffer
69 * \param[in] m Message buffer to be free'd
70 */
Harald Welteec8b4502010-02-20 20:34:29 +010071void msgb_free(struct msgb *m)
72{
73 talloc_free(m);
74}
75
Harald Welteba6988b2011-08-17 12:46:48 +020076/*! \brief Enqueue message buffer to tail of a queue
77 * \param[in] queue linked list header of queue
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +010078 * \param[in] msg message buffer to be added to the queue
Harald Welteba6988b2011-08-17 12:46:48 +020079 *
80 * The function will append the specified message buffer \a msg to the
81 * queue implemented by \ref llist_head \a queue
82 */
Harald Welteec8b4502010-02-20 20:34:29 +010083void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
84{
85 llist_add_tail(&msg->list, queue);
86}
87
Harald Welteba6988b2011-08-17 12:46:48 +020088/*! \brief Dequeue message buffer from head of queue
89 * \param[in] queue linked list header of queue
90 * \returns message buffer (if any) or NULL if queue empty
91 *
92 * The function will remove the first message buffer from the queue
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +010093 * implemented by \ref llist_head \a queue.
Harald Welteba6988b2011-08-17 12:46:48 +020094 */
Harald Welteec8b4502010-02-20 20:34:29 +010095struct msgb *msgb_dequeue(struct llist_head *queue)
96{
97 struct llist_head *lh;
98
99 if (llist_empty(queue))
100 return NULL;
101
102 lh = queue->next;
103 llist_del(lh);
104
105 return llist_entry(lh, struct msgb, list);
106}
107
Harald Welteba6988b2011-08-17 12:46:48 +0200108/*! \brief Re-set all message buffer pointers
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100109 * \param[in] msg message buffer that is to be resetted
Harald Welteba6988b2011-08-17 12:46:48 +0200110 *
111 * This will re-set the various internal pointers into the underlying
112 * message buffer, i.e. remvoe all headroom and treat the msgb as
113 * completely empty. It also initializes the control buffer to zero.
114 */
Harald Welteec8b4502010-02-20 20:34:29 +0100115void msgb_reset(struct msgb *msg)
116{
117 msg->len = 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100118 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +0100119 msg->head = msg->_data;
120 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +0100121
Harald Welteec8b4502010-02-20 20:34:29 +0100122 msg->trx = NULL;
123 msg->lchan = NULL;
124 msg->l2h = NULL;
125 msg->l3h = NULL;
Harald Weltebb77c9d2010-04-30 14:26:12 +0200126 msg->l4h = NULL;
Harald Welte95df5c02010-05-01 23:53:26 +0200127
128 memset(&msg->cb, 0, sizeof(msg->cb));
Harald Welteec8b4502010-02-20 20:34:29 +0100129}
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200130
Harald Welteba6988b2011-08-17 12:46:48 +0200131/*! \brief get pointer to data section of message buffer
132 * \param[in] msg message buffer
133 * \returns pointer to data section of message buffer
134 */
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200135uint8_t *msgb_data(const struct msgb *msg)
136{
137 return msg->data;
138}
139
Harald Welteba6988b2011-08-17 12:46:48 +0200140/*! \brief get length of message buffer
141 * \param[in] msg message buffer
142 * \returns length of data section in message buffer
143 */
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200144uint16_t msgb_length(const struct msgb *msg)
145{
146 return msg->len;
147}
Harald Welte9e1f0602011-06-29 18:46:10 +0200148
Harald Welteba6988b2011-08-17 12:46:48 +0200149/*! \brief Set the talloc context for \ref msgb_alloc
150 * \param[in] ctx talloc context to be used as root for msgb allocations
151 */
Harald Welte9e1f0602011-06-29 18:46:10 +0200152void msgb_set_talloc_ctx(void *ctx)
153{
154 tall_msgb_ctx = ctx;
155}
Harald Welteba6988b2011-08-17 12:46:48 +0200156
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100157/*! \brief Copy an msgb.
158 *
159 * This function allocates a new msgb, copies the data buffer of msg,
160 * and adjusts the pointers (incl l1h-l4h) accordingly. The cb part
161 * is not copied.
162 * \param[in] msg The old msgb object
163 * \param[in] name Human-readable name to be associated with msgb
164 */
165struct msgb *msgb_copy(const struct msgb *msg, const char *name)
166{
167 struct msgb *new_msg;
168
169 new_msg = msgb_alloc(msg->data_len, name);
170 if (!new_msg)
171 return NULL;
172
173 /* copy data */
174 memcpy(new_msg->_data, msg->_data, new_msg->data_len);
175
176 /* copy header */
177 new_msg->len = msg->len;
178 new_msg->data += msg->data - msg->_data;
179 new_msg->head += msg->head - msg->_data;
180 new_msg->tail += msg->tail - msg->_data;
181
182 if (msg->l1h)
183 new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
184 if (msg->l2h)
185 new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
186 if (msg->l3h)
187 new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
188 if (msg->l4h)
189 new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
190
191 return new_msg;
192}
193
194/*! \brief Resize an area within an msgb
195 *
196 * This resizes a sub area of the msgb data and adjusts the pointers (incl
197 * l1h-l4h) accordingly. The cb part is not updated. If the area is extended,
198 * the contents of the extension is undefined. The complete sub area must be a
199 * part of [data,tail].
200 *
201 * \param[inout] msg The msgb object
202 * \param[in] area A pointer to the sub-area
203 * \param[in] old_size The old size of the sub-area
204 * \param[in] new_size The new size of the sub-area
205 * \returns 0 on success, -1 if there is not enough space to extend the area
206 */
207int msgb_resize_area(struct msgb *msg, uint8_t *area,
208 int old_size, int new_size)
209{
210 int rc;
211 uint8_t *post_start = area + old_size;
212 int pre_len = area - msg->data;
213 int post_len = msg->len - old_size - pre_len;
214 int delta_size = new_size - old_size;
215
216 if (old_size < 0 || new_size < 0)
217 MSGB_ABORT(msg, "Negative sizes are not allowed\n");
218 if (area < msg->data || post_start > msg->tail)
219 MSGB_ABORT(msg, "Sub area is not fully contained in the msg data\n");
220
221 if (delta_size == 0)
222 return 0;
223
224 if (delta_size > 0) {
225 rc = msgb_trim(msg, msg->len + delta_size);
226 if (rc < 0)
227 return rc;
228 }
229
230 memmove(area + new_size, area + old_size, post_len);
231
232 if (msg->l1h >= post_start)
233 msg->l1h += delta_size;
234 if (msg->l2h >= post_start)
235 msg->l2h += delta_size;
236 if (msg->l3h >= post_start)
237 msg->l3h += delta_size;
238 if (msg->l4h >= post_start)
239 msg->l4h += delta_size;
240
241 if (delta_size < 0)
242 msgb_trim(msg, msg->len + delta_size);
243
244 return 0;
245}
246
247
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100248/*! \brief Return a (static) buffer containing a hexdump of the msg
249 * \param[in] msg message buffer
250 * \returns a pointer to a static char array
251 */
252const char *msgb_hexdump(const struct msgb *msg)
253{
254 static char buf[4100];
255 int buf_offs = 0;
256 int nchars;
257 const unsigned char *start = msg->data;
258 const unsigned char *lxhs[4];
259 int i;
260
261 lxhs[0] = msg->l1h;
262 lxhs[1] = msg->l2h;
263 lxhs[2] = msg->l3h;
264 lxhs[3] = msg->l4h;
265
266 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
267 if (!lxhs[i])
268 continue;
269
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100270 if (lxhs[i] < msg->head)
271 continue;
272 if (lxhs[i] > msg->head + msg->data_len)
273 continue;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100274 if (lxhs[i] > msg->tail)
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100275 continue;
276 if (lxhs[i] < msg->data || lxhs[i] > msg->tail) {
277 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100278 "(L%d=data%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100279 i+1, lxhs[i] - msg->data);
280 buf_offs += nchars;
281 continue;
282 }
283 if (lxhs[i] < start) {
284 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100285 "(L%d%+" PRIdPTR ") ", i+1,
286 start - lxhs[i]);
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100287 buf_offs += nchars;
288 continue;
289 }
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100290 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
291 "%s[L%d]> ",
292 osmo_hexdump(start, lxhs[i] - start),
293 i+1);
294 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
295 return "ERROR";
296
297 buf_offs += nchars;
298 start = lxhs[i];
299 }
300 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
301 "%s", osmo_hexdump(start, msg->tail - start));
302 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
303 return "ERROR";
304
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100305 buf_offs += nchars;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100306
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100307 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
308 if (!lxhs[i])
309 continue;
310
311 if (lxhs[i] < msg->head || lxhs[i] > msg->head + msg->data_len) {
312 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
313 "(L%d out of range) ", i+1);
314 } else if (lxhs[i] <= msg->data + msg->data_len &&
315 lxhs[i] > msg->tail) {
316 nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100317 "(L%d=tail%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100318 i+1, lxhs[i] - msg->tail);
319 } else
320 continue;
321
322 if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
323 return "ERROR";
324 buf_offs += nchars;
325 }
326
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100327 return buf;
328}
329
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200330/*! @} */