blob: 5a154e56bd467885387e467fcfe7f9670b3e97d6 [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 *
Harald Weltee08da972017-11-13 01:00:26 +09005 * SPDX-License-Identifier: GPL-2.0+
6 *
Harald Welteec8b4502010-02-20 20:34:29 +01007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Harald Welteba6988b2011-08-17 12:46:48 +020023/*! \addtogroup msgb
24 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020025 *
Neels Hofmeyr87e45502017-06-20 00:17:59 +020026 * libosmocore message buffers, inspired by Linux kernel skbuff
Harald Welte96e2a002017-06-12 21:44:18 +020027 *
28 * Inspired by the 'struct skbuff' of the Linux kernel, we implement a
29 * 'struct msgb' which we use for handling network
30 * packets aka messages aka PDUs.
31 *
32 * A msgb consists of
33 * * a header with some metadata, such as
34 * * a linked list header for message queues or the like
35 * * pointers to the headers of various protocol layers inside
36 * the packet
37 * * a data section consisting of
38 * * headroom, i.e. space in front of the message, to allow
39 * for additional headers being pushed in front of the current
40 * data
Vadim Yanitskiy8c8e6d72018-03-08 21:10:23 +070041 * * the currently occupied data for the message
Harald Welte96e2a002017-06-12 21:44:18 +020042 * * tailroom, i.e. space at the end of the message, to
43 * allow more data to be added after the end of the current
44 * data
45 *
46 * We have plenty of utility functions around the \ref msgb:
47 * * allocation / release
48 * * enqueue / dequeue from/to message queues
49 * * prepending (pushing) and appending (putting) data
50 * * copying / resizing
51 * * hex-dumping to a string for debug purposes
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020052 *
53 * \file msgb.c
Harald Welteba6988b2011-08-17 12:46:48 +020054 */
55
Harald Welteec8b4502010-02-20 20:34:29 +010056#include <unistd.h>
57#include <string.h>
58#include <stdlib.h>
Neels Hofmeyr42fff582015-12-23 15:12:40 +010059#include <inttypes.h>
Philipp Maierc5b47cc2017-10-10 16:53:21 +020060#include <stdarg.h>
61#include <errno.h>
62
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010063#include <osmocom/core/msgb.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010064#include <osmocom/core/talloc.h>
Vadim Yanitskiyb4233052018-03-08 21:35:48 +070065#include <osmocom/core/logging.h>
Harald Welteec8b4502010-02-20 20:34:29 +010066
Harald Welte179f3572019-03-18 18:38:47 +010067/*! Allocate a new message buffer from given talloc cotext
68 * \param[in] ctx talloc context from which to allocate
Harald Welteba6988b2011-08-17 12:46:48 +020069 * \param[in] size Length in octets, including headroom
70 * \param[in] name Human-readable name to be associated with msgb
Harald Welte2d2e2cc2016-04-25 12:11:20 +020071 * \returns dynamically-allocated \ref msgb
Harald Welteba6988b2011-08-17 12:46:48 +020072 *
73 * This function allocates a 'struct msgb' as well as the underlying
74 * memory buffer for the actual message data (size specified by \a size)
75 * using the talloc memory context previously set by \ref msgb_set_talloc_ctx
76 */
Harald Welte179f3572019-03-18 18:38:47 +010077struct msgb *msgb_alloc_c(const void *ctx, uint16_t size, const char *name)
Harald Welteec8b4502010-02-20 20:34:29 +010078{
79 struct msgb *msg;
80
Harald Welte179f3572019-03-18 18:38:47 +010081 msg = talloc_named_const(ctx, sizeof(*msg) + size, name);
Harald Welteec8b4502010-02-20 20:34:29 +010082 if (!msg) {
Vadim Yanitskiyb4233052018-03-08 21:35:48 +070083 LOGP(DLGLOBAL, LOGL_FATAL, "Unable to allocate a msgb: "
84 "name='%s', size=%u\n", name, size);
Harald Welteec8b4502010-02-20 20:34:29 +010085 return NULL;
86 }
87
Vadim Yanitskiy10959cd2018-03-08 21:57:42 +070088 /* Manually zero-initialize allocated memory */
89 memset(msg, 0x00, sizeof(*msg) + size);
90
Harald Welteec8b4502010-02-20 20:34:29 +010091 msg->data_len = size;
92 msg->len = 0;
93 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +010094 msg->head = msg->_data;
95 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +010096
97 return msg;
98}
99
Harald Welte179f3572019-03-18 18:38:47 +0100100/* default msgb allocation context for msgb_alloc() */
101void *tall_msgb_ctx = NULL;
102
103/*! Allocate a new message buffer from tall_msgb_ctx
104 * \param[in] size Length in octets, including headroom
105 * \param[in] name Human-readable name to be associated with msgb
106 * \returns dynamically-allocated \ref msgb
107 *
108 * This function allocates a 'struct msgb' as well as the underlying
109 * memory buffer for the actual message data (size specified by \a size)
110 * using the talloc memory context previously set by \ref msgb_set_talloc_ctx
111 */
112struct msgb *msgb_alloc(uint16_t size, const char *name)
113{
114 return msgb_alloc_c(tall_msgb_ctx, size, name);
115}
116
117
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200118/*! Release given message buffer
Vadim Yanitskiy8c8e6d72018-03-08 21:10:23 +0700119 * \param[in] m Message buffer to be freed
Harald Welteba6988b2011-08-17 12:46:48 +0200120 */
Harald Welteec8b4502010-02-20 20:34:29 +0100121void msgb_free(struct msgb *m)
122{
123 talloc_free(m);
124}
125
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200126/*! Enqueue message buffer to tail of a queue
Harald Welteba6988b2011-08-17 12:46:48 +0200127 * \param[in] queue linked list header of queue
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100128 * \param[in] msg message buffer to be added to the queue
Harald Welteba6988b2011-08-17 12:46:48 +0200129 *
130 * The function will append the specified message buffer \a msg to the
131 * queue implemented by \ref llist_head \a queue
132 */
Harald Welteec8b4502010-02-20 20:34:29 +0100133void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
134{
135 llist_add_tail(&msg->list, queue);
136}
137
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200138/*! Dequeue message buffer from head of queue
Harald Welteba6988b2011-08-17 12:46:48 +0200139 * \param[in] queue linked list header of queue
140 * \returns message buffer (if any) or NULL if queue empty
141 *
142 * The function will remove the first message buffer from the queue
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100143 * implemented by \ref llist_head \a queue.
Harald Welteba6988b2011-08-17 12:46:48 +0200144 */
Harald Welteec8b4502010-02-20 20:34:29 +0100145struct msgb *msgb_dequeue(struct llist_head *queue)
146{
147 struct llist_head *lh;
148
149 if (llist_empty(queue))
150 return NULL;
151
152 lh = queue->next;
Maxd826f172016-06-23 13:14:02 +0200153
154 if (lh) {
155 llist_del(lh);
156 return llist_entry(lh, struct msgb, list);
157 } else
158 return NULL;
Harald Welteec8b4502010-02-20 20:34:29 +0100159}
160
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200161/*! Re-set all message buffer pointers
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100162 * \param[in] msg message buffer that is to be resetted
Harald Welteba6988b2011-08-17 12:46:48 +0200163 *
164 * This will re-set the various internal pointers into the underlying
Vadim Yanitskiy8c8e6d72018-03-08 21:10:23 +0700165 * message buffer, i.e. remove all headroom and treat the msgb as
Harald Welteba6988b2011-08-17 12:46:48 +0200166 * completely empty. It also initializes the control buffer to zero.
167 */
Harald Welteec8b4502010-02-20 20:34:29 +0100168void msgb_reset(struct msgb *msg)
169{
170 msg->len = 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100171 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +0100172 msg->head = msg->_data;
173 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +0100174
Harald Welteec8b4502010-02-20 20:34:29 +0100175 msg->trx = NULL;
176 msg->lchan = NULL;
177 msg->l2h = NULL;
178 msg->l3h = NULL;
Harald Weltebb77c9d2010-04-30 14:26:12 +0200179 msg->l4h = NULL;
Harald Welte95df5c02010-05-01 23:53:26 +0200180
181 memset(&msg->cb, 0, sizeof(msg->cb));
Harald Welteec8b4502010-02-20 20:34:29 +0100182}
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200183
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200184/*! get pointer to data section of message buffer
Harald Welteba6988b2011-08-17 12:46:48 +0200185 * \param[in] msg message buffer
186 * \returns pointer to data section of message buffer
187 */
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200188uint8_t *msgb_data(const struct msgb *msg)
189{
190 return msg->data;
191}
192
Maxdb038252018-12-03 14:14:44 +0100193/*! Compare and print: check data in msgb against given data and print errors if any
194 * \param[in] file text prefix, usually __FILE__, ignored if print == false
195 * \param[in] line numeric prefix, usually __LINE__, ignored if print == false
196 * \param[in] func text prefix, usually __func__, ignored if print == false
197 * \param[in] level while layer (L1, L2 etc) data should be compared against
198 * \param[in] msg message buffer
199 * \param[in] data expected data
200 * \param[in] len length of data
201 * \param[in] print boolean indicating whether we should print anything to stdout
202 * \returns boolean indicating whether msgb content is equal to a given data
203 *
204 * This function is not intended to be called directly but rather used through corresponding macro wrappers.
205 */
206bool _msgb_eq(const char *file, size_t line, const char *func, uint8_t level,
207 const struct msgb *msg, const uint8_t *data, size_t len, bool print)
208{
209 const char *m_dump;
210 unsigned int m_len, i;
211 uint8_t *m_data;
212
213 if (!msg) {
214 if (print)
215 LOGPSRC(DLGLOBAL, LOGL_FATAL, file, line, "%s() NULL msg comparison\n", func);
216 return false;
217 }
218
219 if (!data) {
220 if (print)
221 LOGPSRC(DLGLOBAL, LOGL_FATAL, file, line, "%s() NULL comparison data\n", func);
222 return false;
223 }
224
225 switch (level) {
226 case 0:
227 m_len = msgb_length(msg);
228 m_data = msgb_data(msg);
229 m_dump = print ? msgb_hexdump(msg) : NULL;
230 break;
231 case 1:
232 m_len = msgb_l1len(msg);
233 m_data = msgb_l1(msg);
234 m_dump = print ? msgb_hexdump_l1(msg) : NULL;
235 break;
236 case 2:
237 m_len = msgb_l2len(msg);
238 m_data = msgb_l2(msg);
239 m_dump = print ? msgb_hexdump_l2(msg) : NULL;
240 break;
241 case 3:
242 m_len = msgb_l3len(msg);
243 m_data = msgb_l3(msg);
244 m_dump = print ? msgb_hexdump_l3(msg) : NULL;
245 break;
246 case 4:
247 m_len = msgb_l4len(msg);
248 m_data = msgb_l4(msg);
249 m_dump = print ? msgb_hexdump_l4(msg) : NULL;
250 break;
251 default:
252 LOGPSRC(DLGLOBAL, LOGL_FATAL, file, line,
253 "%s() FIXME: unexpected comparison level %u\n", func, level);
254 return false;
255 }
256
257 if (m_len != len) {
258 if (print)
259 LOGPSRC(DLGLOBAL, LOGL_FATAL, file, line,
260 "%s() Length mismatch: %d != %zu, %s\n", func, m_len, len, m_dump);
261 return false;
262 }
263
264 if (memcmp(m_data, data, len) == 0)
265 return true;
266
267 if (!print)
268 return false;
269
270 LOGPSRC(DLGLOBAL, LOGL_FATAL, file, line,
271 "%s() L%u data mismatch:\nexpected %s\n ", func, level, osmo_hexdump(data, len));
272
273 for(i = 0; i < len; i++)
274 if (data[i] != m_data[i]) {
275 LOGPC(DLGLOBAL, LOGL_FATAL, "!!\n");
276 break;
277 } else
278 LOGPC(DLGLOBAL, LOGL_FATAL, ".. ");
279
Maxa6749ac2019-01-08 14:42:30 +0100280 LOGPC(DLGLOBAL, LOGL_FATAL, " msgb %s\n", osmo_hexdump(m_data, len));
Maxdb038252018-12-03 14:14:44 +0100281
282 return false;
283}
284
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200285/*! get length of message buffer
Harald Welteba6988b2011-08-17 12:46:48 +0200286 * \param[in] msg message buffer
287 * \returns length of data section in message buffer
288 */
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200289uint16_t msgb_length(const struct msgb *msg)
290{
291 return msg->len;
292}
Harald Welte9e1f0602011-06-29 18:46:10 +0200293
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200294/*! Set the talloc context for \ref msgb_alloc
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200295 * Deprecated, use msgb_talloc_ctx_init() instead.
Harald Welteba6988b2011-08-17 12:46:48 +0200296 * \param[in] ctx talloc context to be used as root for msgb allocations
297 */
Harald Welte9e1f0602011-06-29 18:46:10 +0200298void msgb_set_talloc_ctx(void *ctx)
299{
300 tall_msgb_ctx = ctx;
301}
Harald Welteba6988b2011-08-17 12:46:48 +0200302
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200303/*! Initialize a msgb talloc context for \ref msgb_alloc.
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200304 * Create a talloc context called "msgb". If \a pool_size is 0, create a named
305 * const as msgb talloc context. If \a pool_size is nonzero, create a talloc
306 * pool, possibly for faster msgb allocations (see talloc_pool()).
307 * \param[in] root_ctx talloc context used as parent for the new "msgb" ctx.
308 * \param[in] pool_size if nonzero, create a talloc pool of this size.
309 * \returns the new msgb talloc context, e.g. for reporting
310 */
311void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size)
312{
313 if (!pool_size)
314 tall_msgb_ctx = talloc_size(root_ctx, 0);
315 else
316 tall_msgb_ctx = talloc_pool(root_ctx, pool_size);
317 talloc_set_name_const(tall_msgb_ctx, "msgb");
318 return tall_msgb_ctx;
319}
320
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200321/*! Copy an msgb.
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100322 *
323 * This function allocates a new msgb, copies the data buffer of msg,
324 * and adjusts the pointers (incl l1h-l4h) accordingly. The cb part
325 * is not copied.
326 * \param[in] msg The old msgb object
327 * \param[in] name Human-readable name to be associated with msgb
328 */
Harald Welte179f3572019-03-18 18:38:47 +0100329struct msgb *msgb_copy_c(const void *ctx, const struct msgb *msg, const char *name)
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100330{
331 struct msgb *new_msg;
332
Harald Welte179f3572019-03-18 18:38:47 +0100333 new_msg = msgb_alloc_c(ctx, msg->data_len, name);
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100334 if (!new_msg)
335 return NULL;
336
337 /* copy data */
338 memcpy(new_msg->_data, msg->_data, new_msg->data_len);
339
340 /* copy header */
341 new_msg->len = msg->len;
342 new_msg->data += msg->data - msg->_data;
343 new_msg->head += msg->head - msg->_data;
344 new_msg->tail += msg->tail - msg->_data;
345
346 if (msg->l1h)
347 new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
348 if (msg->l2h)
349 new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
350 if (msg->l3h)
351 new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
352 if (msg->l4h)
353 new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
354
355 return new_msg;
356}
357
Harald Welte179f3572019-03-18 18:38:47 +0100358/*! Copy an msgb.
359 *
360 * This function allocates a new msgb, copies the data buffer of msg,
361 * and adjusts the pointers (incl l1h-l4h) accordingly. The cb part
362 * is not copied.
363 * \param[in] msg The old msgb object
364 * \param[in] name Human-readable name to be associated with msgb
365 */
366struct msgb *msgb_copy(const struct msgb *msg, const char *name)
367{
368 return msgb_copy_c(tall_msgb_ctx, msg, name);
369}
370
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200371/*! Resize an area within an msgb
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100372 *
373 * This resizes a sub area of the msgb data and adjusts the pointers (incl
374 * l1h-l4h) accordingly. The cb part is not updated. If the area is extended,
375 * the contents of the extension is undefined. The complete sub area must be a
376 * part of [data,tail].
377 *
378 * \param[inout] msg The msgb object
379 * \param[in] area A pointer to the sub-area
380 * \param[in] old_size The old size of the sub-area
381 * \param[in] new_size The new size of the sub-area
382 * \returns 0 on success, -1 if there is not enough space to extend the area
383 */
384int msgb_resize_area(struct msgb *msg, uint8_t *area,
385 int old_size, int new_size)
386{
387 int rc;
388 uint8_t *post_start = area + old_size;
389 int pre_len = area - msg->data;
390 int post_len = msg->len - old_size - pre_len;
391 int delta_size = new_size - old_size;
392
393 if (old_size < 0 || new_size < 0)
394 MSGB_ABORT(msg, "Negative sizes are not allowed\n");
395 if (area < msg->data || post_start > msg->tail)
396 MSGB_ABORT(msg, "Sub area is not fully contained in the msg data\n");
397
398 if (delta_size == 0)
399 return 0;
400
401 if (delta_size > 0) {
402 rc = msgb_trim(msg, msg->len + delta_size);
403 if (rc < 0)
404 return rc;
405 }
406
407 memmove(area + new_size, area + old_size, post_len);
408
409 if (msg->l1h >= post_start)
410 msg->l1h += delta_size;
411 if (msg->l2h >= post_start)
412 msg->l2h += delta_size;
413 if (msg->l3h >= post_start)
414 msg->l3h += delta_size;
415 if (msg->l4h >= post_start)
416 msg->l4h += delta_size;
417
418 if (delta_size < 0)
419 msgb_trim(msg, msg->len + delta_size);
420
421 return 0;
422}
423
424
Harald Welte4a62eda2019-03-18 18:27:00 +0100425/*! fill user-provided buffer with hexdump of the msg.
426 * \param[out] buf caller-allocated buffer for output string
427 * \param[in] buf_len length of buf
428 * \param[in] msg message buffer to be dumped
429 * \returns buf
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100430 */
Harald Welte4a62eda2019-03-18 18:27:00 +0100431char *msgb_hexdump_buf(char *buf, size_t buf_len, const struct msgb *msg)
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100432{
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100433 int buf_offs = 0;
434 int nchars;
435 const unsigned char *start = msg->data;
436 const unsigned char *lxhs[4];
437 int i;
438
439 lxhs[0] = msg->l1h;
440 lxhs[1] = msg->l2h;
441 lxhs[2] = msg->l3h;
442 lxhs[3] = msg->l4h;
443
444 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
445 if (!lxhs[i])
446 continue;
447
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100448 if (lxhs[i] < msg->head)
449 continue;
450 if (lxhs[i] > msg->head + msg->data_len)
451 continue;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100452 if (lxhs[i] > msg->tail)
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100453 continue;
454 if (lxhs[i] < msg->data || lxhs[i] > msg->tail) {
Harald Welte4a62eda2019-03-18 18:27:00 +0100455 nchars = snprintf(buf + buf_offs, buf_len - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100456 "(L%d=data%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100457 i+1, lxhs[i] - msg->data);
458 buf_offs += nchars;
459 continue;
460 }
461 if (lxhs[i] < start) {
Harald Welte4a62eda2019-03-18 18:27:00 +0100462 nchars = snprintf(buf + buf_offs, buf_len - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100463 "(L%d%+" PRIdPTR ") ", i+1,
464 start - lxhs[i]);
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100465 buf_offs += nchars;
466 continue;
467 }
Harald Welte4a62eda2019-03-18 18:27:00 +0100468 nchars = snprintf(buf + buf_offs, buf_len - buf_offs,
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100469 "%s[L%d]> ",
470 osmo_hexdump(start, lxhs[i] - start),
471 i+1);
Harald Welte4a62eda2019-03-18 18:27:00 +0100472 if (nchars < 0 || nchars + buf_offs >= buf_len)
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100473 return "ERROR";
474
475 buf_offs += nchars;
476 start = lxhs[i];
477 }
Harald Welte4a62eda2019-03-18 18:27:00 +0100478 nchars = snprintf(buf + buf_offs, buf_len - buf_offs,
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100479 "%s", osmo_hexdump(start, msg->tail - start));
Harald Welte4a62eda2019-03-18 18:27:00 +0100480 if (nchars < 0 || nchars + buf_offs >= buf_len)
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100481 return "ERROR";
482
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100483 buf_offs += nchars;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100484
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100485 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
486 if (!lxhs[i])
487 continue;
488
489 if (lxhs[i] < msg->head || lxhs[i] > msg->head + msg->data_len) {
Harald Welte4a62eda2019-03-18 18:27:00 +0100490 nchars = snprintf(buf + buf_offs, buf_len - buf_offs,
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100491 "(L%d out of range) ", i+1);
492 } else if (lxhs[i] <= msg->data + msg->data_len &&
493 lxhs[i] > msg->tail) {
Harald Welte4a62eda2019-03-18 18:27:00 +0100494 nchars = snprintf(buf + buf_offs, buf_len - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100495 "(L%d=tail%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100496 i+1, lxhs[i] - msg->tail);
497 } else
498 continue;
499
Harald Welte4a62eda2019-03-18 18:27:00 +0100500 if (nchars < 0 || nchars + buf_offs >= buf_len)
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100501 return "ERROR";
502 buf_offs += nchars;
503 }
504
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100505 return buf;
506}
507
Harald Welte4a62eda2019-03-18 18:27:00 +0100508/*! Return a (static) buffer containing a hexdump of the msg.
509 * \param[in] msg message buffer
510 * \returns a pointer to a static char array
511 */
512const char *msgb_hexdump(const struct msgb *msg)
513{
514 static char buf[4100];
515 return msgb_hexdump_buf(buf, sizeof(buf), msg);
516}
Philipp Maierc5b47cc2017-10-10 16:53:21 +0200517
Harald Welte179f3572019-03-18 18:38:47 +0100518/*! Return a dynamically allocated buffer containing a hexdump of the msg
519 * \param[in] ctx talloc context from where to allocate the output string
520 * \param[in] msg message buffer
521 * \returns a pointer to a static char array
522 */
523char *msgb_hexdump_c(const void *ctx, const struct msgb *msg)
524{
525 char *buf = talloc_size(ctx, msgb_length(msg)*3 + 100);
526 if (!buf)
527 return NULL;
528 return msgb_hexdump_buf(buf, sizeof(buf), msg);
529}
530
Philipp Maierc5b47cc2017-10-10 16:53:21 +0200531/*! Print a string to the end of message buffer.
Vadim Yanitskiyb8d06fb2019-03-25 23:59:11 +0700532 * \param[in] msgb message buffer.
533 * \param[in] format format string.
534 * \returns 0 on success, -EINVAL on error.
Philipp Maierc5b47cc2017-10-10 16:53:21 +0200535 *
536 * The resulting string is printed to the msgb without a trailing nul
537 * character. A nul following the data tail may be written as an implementation
538 * detail, but a trailing nul is never part of the msgb data in terms of
539 * msgb_length().
540 *
541 * Note: the tailroom must always be one byte longer than the string to be
542 * written. The msgb is filled only up to tailroom=1. This is an implementation
543 * detail that allows leaving a nul character behind the valid data.
544 *
545 * In case of error, the msgb remains unchanged, though data may have been
546 * written to the (unused) memory after the tail pointer.
547 */
548int msgb_printf(struct msgb *msgb, const char *format, ...)
549{
550 va_list args;
551 int str_len;
552 int rc = 0;
553
554 OSMO_ASSERT(msgb);
555 OSMO_ASSERT(format);
556
557 /* Regardless of what we plan to add to the buffer, we must at least
558 * be able to store a string terminator (nullstring) */
559 if (msgb_tailroom(msgb) < 1)
560 return -EINVAL;
561
562 va_start(args, format);
563
564 str_len =
565 vsnprintf((char *)msgb->tail, msgb_tailroom(msgb), format, args);
566
567 if (str_len >= msgb_tailroom(msgb) || str_len < 0) {
568 rc = -EINVAL;
569 } else
570 msgb_put(msgb, str_len);
571
572 va_end(args);
573 return rc;
574}
575
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200576/*! @} */