blob: ccb55aac3873ba8bae391f696c044918e7083080 [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 *
Harald Welteec8b4502010-02-20 20:34:29 +010017 */
18
Harald Welteba6988b2011-08-17 12:46:48 +020019/*! \addtogroup msgb
20 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020021 *
Neels Hofmeyr87e45502017-06-20 00:17:59 +020022 * libosmocore message buffers, inspired by Linux kernel skbuff
Harald Welte96e2a002017-06-12 21:44:18 +020023 *
24 * Inspired by the 'struct skbuff' of the Linux kernel, we implement a
25 * 'struct msgb' which we use for handling network
26 * packets aka messages aka PDUs.
27 *
28 * A msgb consists of
29 * * a header with some metadata, such as
30 * * a linked list header for message queues or the like
31 * * pointers to the headers of various protocol layers inside
32 * the packet
33 * * a data section consisting of
34 * * headroom, i.e. space in front of the message, to allow
35 * for additional headers being pushed in front of the current
36 * data
Vadim Yanitskiy8c8e6d72018-03-08 21:10:23 +070037 * * the currently occupied data for the message
Harald Welte96e2a002017-06-12 21:44:18 +020038 * * tailroom, i.e. space at the end of the message, to
39 * allow more data to be added after the end of the current
40 * data
41 *
42 * We have plenty of utility functions around the \ref msgb:
43 * * allocation / release
44 * * enqueue / dequeue from/to message queues
45 * * prepending (pushing) and appending (putting) data
46 * * copying / resizing
47 * * hex-dumping to a string for debug purposes
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020048 *
49 * \file msgb.c
Harald Welteba6988b2011-08-17 12:46:48 +020050 */
51
Harald Welteec8b4502010-02-20 20:34:29 +010052#include <unistd.h>
53#include <string.h>
54#include <stdlib.h>
Neels Hofmeyr42fff582015-12-23 15:12:40 +010055#include <inttypes.h>
Philipp Maierc5b47cc2017-10-10 16:53:21 +020056#include <stdarg.h>
57#include <errno.h>
58
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010059#include <osmocom/core/msgb.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010060#include <osmocom/core/talloc.h>
Vadim Yanitskiyb4233052018-03-08 21:35:48 +070061#include <osmocom/core/logging.h>
Harald Welteec8b4502010-02-20 20:34:29 +010062
Pau Espin Pedrol470cc122020-09-27 12:58:23 +020063/*! Allocate a new message buffer from given talloc context
Harald Welte179f3572019-03-18 18:38:47 +010064 * \param[in] ctx talloc context from which to allocate
Harald Welteba6988b2011-08-17 12:46:48 +020065 * \param[in] size Length in octets, including headroom
66 * \param[in] name Human-readable name to be associated with msgb
Harald Welte2d2e2cc2016-04-25 12:11:20 +020067 * \returns dynamically-allocated \ref msgb
Harald Welteba6988b2011-08-17 12:46:48 +020068 *
69 * This function allocates a 'struct msgb' as well as the underlying
70 * memory buffer for the actual message data (size specified by \a size)
71 * using the talloc memory context previously set by \ref msgb_set_talloc_ctx
72 */
Harald Welte179f3572019-03-18 18:38:47 +010073struct msgb *msgb_alloc_c(const void *ctx, uint16_t size, const char *name)
Harald Welteec8b4502010-02-20 20:34:29 +010074{
75 struct msgb *msg;
76
Harald Welte179f3572019-03-18 18:38:47 +010077 msg = talloc_named_const(ctx, sizeof(*msg) + size, name);
Harald Welteec8b4502010-02-20 20:34:29 +010078 if (!msg) {
Vadim Yanitskiyb4233052018-03-08 21:35:48 +070079 LOGP(DLGLOBAL, LOGL_FATAL, "Unable to allocate a msgb: "
80 "name='%s', size=%u\n", name, size);
Harald Welteec8b4502010-02-20 20:34:29 +010081 return NULL;
82 }
83
Vadim Yanitskiy10959cd2018-03-08 21:57:42 +070084 /* Manually zero-initialize allocated memory */
85 memset(msg, 0x00, sizeof(*msg) + size);
86
Harald Welteec8b4502010-02-20 20:34:29 +010087 msg->data_len = size;
88 msg->len = 0;
89 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +010090 msg->head = msg->_data;
91 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +010092
93 return msg;
94}
95
Harald Welte179f3572019-03-18 18:38:47 +010096/* default msgb allocation context for msgb_alloc() */
97void *tall_msgb_ctx = NULL;
98
99/*! Allocate a new message buffer from tall_msgb_ctx
100 * \param[in] size Length in octets, including headroom
101 * \param[in] name Human-readable name to be associated with msgb
102 * \returns dynamically-allocated \ref msgb
103 *
104 * This function allocates a 'struct msgb' as well as the underlying
105 * memory buffer for the actual message data (size specified by \a size)
106 * using the talloc memory context previously set by \ref msgb_set_talloc_ctx
107 */
108struct msgb *msgb_alloc(uint16_t size, const char *name)
109{
110 return msgb_alloc_c(tall_msgb_ctx, size, name);
111}
112
113
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200114/*! Release given message buffer
Vadim Yanitskiy8c8e6d72018-03-08 21:10:23 +0700115 * \param[in] m Message buffer to be freed
Harald Welteba6988b2011-08-17 12:46:48 +0200116 */
Harald Welteec8b4502010-02-20 20:34:29 +0100117void msgb_free(struct msgb *m)
118{
119 talloc_free(m);
120}
121
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200122/*! Enqueue message buffer to tail of a queue
Harald Welteba6988b2011-08-17 12:46:48 +0200123 * \param[in] queue linked list header of queue
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100124 * \param[in] msg message buffer to be added to the queue
Harald Welteba6988b2011-08-17 12:46:48 +0200125 *
126 * The function will append the specified message buffer \a msg to the
127 * queue implemented by \ref llist_head \a queue
128 */
Harald Welteec8b4502010-02-20 20:34:29 +0100129void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
130{
131 llist_add_tail(&msg->list, queue);
132}
133
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200134/*! Dequeue message buffer from head of queue
Harald Welteba6988b2011-08-17 12:46:48 +0200135 * \param[in] queue linked list header of queue
136 * \returns message buffer (if any) or NULL if queue empty
137 *
138 * The function will remove the first message buffer from the queue
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100139 * implemented by \ref llist_head \a queue.
Harald Welteba6988b2011-08-17 12:46:48 +0200140 */
Harald Welteec8b4502010-02-20 20:34:29 +0100141struct msgb *msgb_dequeue(struct llist_head *queue)
142{
143 struct llist_head *lh;
144
145 if (llist_empty(queue))
146 return NULL;
147
148 lh = queue->next;
Maxd826f172016-06-23 13:14:02 +0200149
150 if (lh) {
151 llist_del(lh);
152 return llist_entry(lh, struct msgb, list);
153 } else
154 return NULL;
Harald Welteec8b4502010-02-20 20:34:29 +0100155}
156
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200157/*! Re-set all message buffer pointers
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100158 * \param[in] msg message buffer that is to be resetted
Harald Welteba6988b2011-08-17 12:46:48 +0200159 *
160 * This will re-set the various internal pointers into the underlying
Vadim Yanitskiy8c8e6d72018-03-08 21:10:23 +0700161 * message buffer, i.e. remove all headroom and treat the msgb as
Harald Welteba6988b2011-08-17 12:46:48 +0200162 * completely empty. It also initializes the control buffer to zero.
163 */
Harald Welteec8b4502010-02-20 20:34:29 +0100164void msgb_reset(struct msgb *msg)
165{
166 msg->len = 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100167 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +0100168 msg->head = msg->_data;
169 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +0100170
Harald Welteec8b4502010-02-20 20:34:29 +0100171 msg->trx = NULL;
172 msg->lchan = NULL;
173 msg->l2h = NULL;
174 msg->l3h = NULL;
Harald Weltebb77c9d2010-04-30 14:26:12 +0200175 msg->l4h = NULL;
Harald Welte95df5c02010-05-01 23:53:26 +0200176
177 memset(&msg->cb, 0, sizeof(msg->cb));
Harald Welteec8b4502010-02-20 20:34:29 +0100178}
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200179
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200180/*! get pointer to data section of message buffer
Harald Welteba6988b2011-08-17 12:46:48 +0200181 * \param[in] msg message buffer
182 * \returns pointer to data section of message buffer
183 */
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200184uint8_t *msgb_data(const struct msgb *msg)
185{
186 return msg->data;
187}
188
Maxdb038252018-12-03 14:14:44 +0100189/*! Compare and print: check data in msgb against given data and print errors if any
190 * \param[in] file text prefix, usually __FILE__, ignored if print == false
191 * \param[in] line numeric prefix, usually __LINE__, ignored if print == false
192 * \param[in] func text prefix, usually __func__, ignored if print == false
193 * \param[in] level while layer (L1, L2 etc) data should be compared against
194 * \param[in] msg message buffer
195 * \param[in] data expected data
196 * \param[in] len length of data
197 * \param[in] print boolean indicating whether we should print anything to stdout
198 * \returns boolean indicating whether msgb content is equal to a given data
199 *
200 * This function is not intended to be called directly but rather used through corresponding macro wrappers.
201 */
202bool _msgb_eq(const char *file, size_t line, const char *func, uint8_t level,
203 const struct msgb *msg, const uint8_t *data, size_t len, bool print)
204{
205 const char *m_dump;
206 unsigned int m_len, i;
207 uint8_t *m_data;
208
209 if (!msg) {
210 if (print)
211 LOGPSRC(DLGLOBAL, LOGL_FATAL, file, line, "%s() NULL msg comparison\n", func);
212 return false;
213 }
214
215 if (!data) {
216 if (print)
217 LOGPSRC(DLGLOBAL, LOGL_FATAL, file, line, "%s() NULL comparison data\n", func);
218 return false;
219 }
220
221 switch (level) {
222 case 0:
223 m_len = msgb_length(msg);
224 m_data = msgb_data(msg);
225 m_dump = print ? msgb_hexdump(msg) : NULL;
226 break;
227 case 1:
228 m_len = msgb_l1len(msg);
229 m_data = msgb_l1(msg);
230 m_dump = print ? msgb_hexdump_l1(msg) : NULL;
231 break;
232 case 2:
233 m_len = msgb_l2len(msg);
234 m_data = msgb_l2(msg);
235 m_dump = print ? msgb_hexdump_l2(msg) : NULL;
236 break;
237 case 3:
238 m_len = msgb_l3len(msg);
239 m_data = msgb_l3(msg);
240 m_dump = print ? msgb_hexdump_l3(msg) : NULL;
241 break;
242 case 4:
243 m_len = msgb_l4len(msg);
244 m_data = msgb_l4(msg);
245 m_dump = print ? msgb_hexdump_l4(msg) : NULL;
246 break;
247 default:
248 LOGPSRC(DLGLOBAL, LOGL_FATAL, file, line,
249 "%s() FIXME: unexpected comparison level %u\n", func, level);
250 return false;
251 }
252
253 if (m_len != len) {
254 if (print)
255 LOGPSRC(DLGLOBAL, LOGL_FATAL, file, line,
256 "%s() Length mismatch: %d != %zu, %s\n", func, m_len, len, m_dump);
257 return false;
258 }
259
260 if (memcmp(m_data, data, len) == 0)
261 return true;
262
263 if (!print)
264 return false;
265
266 LOGPSRC(DLGLOBAL, LOGL_FATAL, file, line,
267 "%s() L%u data mismatch:\nexpected %s\n ", func, level, osmo_hexdump(data, len));
268
269 for(i = 0; i < len; i++)
270 if (data[i] != m_data[i]) {
271 LOGPC(DLGLOBAL, LOGL_FATAL, "!!\n");
272 break;
273 } else
274 LOGPC(DLGLOBAL, LOGL_FATAL, ".. ");
275
Maxa6749ac2019-01-08 14:42:30 +0100276 LOGPC(DLGLOBAL, LOGL_FATAL, " msgb %s\n", osmo_hexdump(m_data, len));
Maxdb038252018-12-03 14:14:44 +0100277
278 return false;
279}
280
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200281/*! get length of message buffer
Harald Welteba6988b2011-08-17 12:46:48 +0200282 * \param[in] msg message buffer
283 * \returns length of data section in message buffer
284 */
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200285uint16_t msgb_length(const struct msgb *msg)
286{
287 return msg->len;
288}
Harald Welte9e1f0602011-06-29 18:46:10 +0200289
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200290/*! Set the talloc context for \ref msgb_alloc
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200291 * Deprecated, use msgb_talloc_ctx_init() instead.
Harald Welteba6988b2011-08-17 12:46:48 +0200292 * \param[in] ctx talloc context to be used as root for msgb allocations
293 */
Harald Welte9e1f0602011-06-29 18:46:10 +0200294void msgb_set_talloc_ctx(void *ctx)
295{
296 tall_msgb_ctx = ctx;
297}
Harald Welteba6988b2011-08-17 12:46:48 +0200298
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200299/*! Initialize a msgb talloc context for \ref msgb_alloc.
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200300 * Create a talloc context called "msgb". If \a pool_size is 0, create a named
301 * const as msgb talloc context. If \a pool_size is nonzero, create a talloc
302 * pool, possibly for faster msgb allocations (see talloc_pool()).
303 * \param[in] root_ctx talloc context used as parent for the new "msgb" ctx.
304 * \param[in] pool_size if nonzero, create a talloc pool of this size.
305 * \returns the new msgb talloc context, e.g. for reporting
306 */
307void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size)
308{
309 if (!pool_size)
310 tall_msgb_ctx = talloc_size(root_ctx, 0);
311 else
312 tall_msgb_ctx = talloc_pool(root_ctx, pool_size);
313 talloc_set_name_const(tall_msgb_ctx, "msgb");
314 return tall_msgb_ctx;
315}
316
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200317/*! Copy an msgb.
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100318 *
319 * This function allocates a new msgb, copies the data buffer of msg,
320 * and adjusts the pointers (incl l1h-l4h) accordingly. The cb part
321 * is not copied.
322 * \param[in] msg The old msgb object
323 * \param[in] name Human-readable name to be associated with msgb
324 */
Harald Welte179f3572019-03-18 18:38:47 +0100325struct msgb *msgb_copy_c(const void *ctx, const struct msgb *msg, const char *name)
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100326{
327 struct msgb *new_msg;
328
Harald Welte179f3572019-03-18 18:38:47 +0100329 new_msg = msgb_alloc_c(ctx, msg->data_len, name);
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100330 if (!new_msg)
331 return NULL;
332
333 /* copy data */
334 memcpy(new_msg->_data, msg->_data, new_msg->data_len);
335
336 /* copy header */
337 new_msg->len = msg->len;
338 new_msg->data += msg->data - msg->_data;
339 new_msg->head += msg->head - msg->_data;
340 new_msg->tail += msg->tail - msg->_data;
341
342 if (msg->l1h)
343 new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
344 if (msg->l2h)
345 new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
346 if (msg->l3h)
347 new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
348 if (msg->l4h)
349 new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
350
351 return new_msg;
352}
353
Harald Welte179f3572019-03-18 18:38:47 +0100354/*! Copy an msgb.
355 *
356 * This function allocates a new msgb, copies the data buffer of msg,
357 * and adjusts the pointers (incl l1h-l4h) accordingly. The cb part
358 * is not copied.
359 * \param[in] msg The old msgb object
360 * \param[in] name Human-readable name to be associated with msgb
361 */
362struct msgb *msgb_copy(const struct msgb *msg, const char *name)
363{
364 return msgb_copy_c(tall_msgb_ctx, msg, name);
365}
366
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200367/*! Resize an area within an msgb
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +0100368 *
369 * This resizes a sub area of the msgb data and adjusts the pointers (incl
370 * l1h-l4h) accordingly. The cb part is not updated. If the area is extended,
371 * the contents of the extension is undefined. The complete sub area must be a
372 * part of [data,tail].
373 *
374 * \param[inout] msg The msgb object
375 * \param[in] area A pointer to the sub-area
376 * \param[in] old_size The old size of the sub-area
377 * \param[in] new_size The new size of the sub-area
378 * \returns 0 on success, -1 if there is not enough space to extend the area
379 */
380int msgb_resize_area(struct msgb *msg, uint8_t *area,
381 int old_size, int new_size)
382{
383 int rc;
384 uint8_t *post_start = area + old_size;
385 int pre_len = area - msg->data;
386 int post_len = msg->len - old_size - pre_len;
387 int delta_size = new_size - old_size;
388
389 if (old_size < 0 || new_size < 0)
390 MSGB_ABORT(msg, "Negative sizes are not allowed\n");
391 if (area < msg->data || post_start > msg->tail)
392 MSGB_ABORT(msg, "Sub area is not fully contained in the msg data\n");
393
394 if (delta_size == 0)
395 return 0;
396
397 if (delta_size > 0) {
398 rc = msgb_trim(msg, msg->len + delta_size);
399 if (rc < 0)
400 return rc;
401 }
402
403 memmove(area + new_size, area + old_size, post_len);
404
405 if (msg->l1h >= post_start)
406 msg->l1h += delta_size;
407 if (msg->l2h >= post_start)
408 msg->l2h += delta_size;
409 if (msg->l3h >= post_start)
410 msg->l3h += delta_size;
411 if (msg->l4h >= post_start)
412 msg->l4h += delta_size;
413
414 if (delta_size < 0)
415 msgb_trim(msg, msg->len + delta_size);
416
417 return 0;
418}
419
420
Harald Welte4a62eda2019-03-18 18:27:00 +0100421/*! fill user-provided buffer with hexdump of the msg.
422 * \param[out] buf caller-allocated buffer for output string
423 * \param[in] buf_len length of buf
424 * \param[in] msg message buffer to be dumped
425 * \returns buf
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100426 */
Harald Welte4a62eda2019-03-18 18:27:00 +0100427char *msgb_hexdump_buf(char *buf, size_t buf_len, const struct msgb *msg)
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100428{
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100429 int buf_offs = 0;
430 int nchars;
431 const unsigned char *start = msg->data;
432 const unsigned char *lxhs[4];
433 int i;
434
435 lxhs[0] = msg->l1h;
436 lxhs[1] = msg->l2h;
437 lxhs[2] = msg->l3h;
438 lxhs[3] = msg->l4h;
439
440 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
441 if (!lxhs[i])
442 continue;
443
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100444 if (lxhs[i] < msg->head)
445 continue;
446 if (lxhs[i] > msg->head + msg->data_len)
447 continue;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100448 if (lxhs[i] > msg->tail)
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100449 continue;
450 if (lxhs[i] < msg->data || lxhs[i] > msg->tail) {
Harald Welte4a62eda2019-03-18 18:27:00 +0100451 nchars = snprintf(buf + buf_offs, buf_len - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100452 "(L%d=data%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100453 i+1, lxhs[i] - msg->data);
454 buf_offs += nchars;
455 continue;
456 }
457 if (lxhs[i] < start) {
Harald Welte4a62eda2019-03-18 18:27:00 +0100458 nchars = snprintf(buf + buf_offs, buf_len - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100459 "(L%d%+" PRIdPTR ") ", i+1,
460 start - lxhs[i]);
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100461 buf_offs += nchars;
462 continue;
463 }
Harald Welte4a62eda2019-03-18 18:27:00 +0100464 nchars = snprintf(buf + buf_offs, buf_len - buf_offs,
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100465 "%s[L%d]> ",
466 osmo_hexdump(start, lxhs[i] - start),
467 i+1);
Harald Welte4a62eda2019-03-18 18:27:00 +0100468 if (nchars < 0 || nchars + buf_offs >= buf_len)
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100469 return "ERROR";
470
471 buf_offs += nchars;
472 start = lxhs[i];
473 }
Harald Welte4a62eda2019-03-18 18:27:00 +0100474 nchars = snprintf(buf + buf_offs, buf_len - buf_offs,
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100475 "%s", osmo_hexdump(start, msg->tail - start));
Harald Welte4a62eda2019-03-18 18:27:00 +0100476 if (nchars < 0 || nchars + buf_offs >= buf_len)
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100477 return "ERROR";
478
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100479 buf_offs += nchars;
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100480
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100481 for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
482 if (!lxhs[i])
483 continue;
484
485 if (lxhs[i] < msg->head || lxhs[i] > msg->head + msg->data_len) {
Harald Welte4a62eda2019-03-18 18:27:00 +0100486 nchars = snprintf(buf + buf_offs, buf_len - buf_offs,
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100487 "(L%d out of range) ", i+1);
488 } else if (lxhs[i] <= msg->data + msg->data_len &&
489 lxhs[i] > msg->tail) {
Harald Welte4a62eda2019-03-18 18:27:00 +0100490 nchars = snprintf(buf + buf_offs, buf_len - buf_offs,
Neels Hofmeyr42fff582015-12-23 15:12:40 +0100491 "(L%d=tail%+" PRIdPTR ") ",
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100492 i+1, lxhs[i] - msg->tail);
493 } else
494 continue;
495
Harald Welte4a62eda2019-03-18 18:27:00 +0100496 if (nchars < 0 || nchars + buf_offs >= buf_len)
Jacob Erlbeck86ec3112015-11-27 13:26:14 +0100497 return "ERROR";
498 buf_offs += nchars;
499 }
500
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +0100501 return buf;
502}
503
Harald Welte4a62eda2019-03-18 18:27:00 +0100504/*! Return a (static) buffer containing a hexdump of the msg.
505 * \param[in] msg message buffer
506 * \returns a pointer to a static char array
507 */
508const char *msgb_hexdump(const struct msgb *msg)
509{
Harald Welte171ef822019-03-28 10:49:05 +0100510 static __thread char buf[4100];
Harald Welte4a62eda2019-03-18 18:27:00 +0100511 return msgb_hexdump_buf(buf, sizeof(buf), msg);
512}
Philipp Maierc5b47cc2017-10-10 16:53:21 +0200513
Harald Welte179f3572019-03-18 18:38:47 +0100514/*! Return a dynamically allocated buffer containing a hexdump of the msg
515 * \param[in] ctx talloc context from where to allocate the output string
516 * \param[in] msg message buffer
517 * \returns a pointer to a static char array
518 */
519char *msgb_hexdump_c(const void *ctx, const struct msgb *msg)
520{
Vadim Yanitskiy4f619c22019-04-12 21:48:07 +0700521 size_t buf_len = msgb_length(msg) * 3 + 100;
522 char *buf = talloc_size(ctx, buf_len);
Harald Welte179f3572019-03-18 18:38:47 +0100523 if (!buf)
524 return NULL;
Vadim Yanitskiy4f619c22019-04-12 21:48:07 +0700525 return msgb_hexdump_buf(buf, buf_len, msg);
Harald Welte179f3572019-03-18 18:38:47 +0100526}
527
Philipp Maierc5b47cc2017-10-10 16:53:21 +0200528/*! Print a string to the end of message buffer.
Vadim Yanitskiyb8d06fb2019-03-25 23:59:11 +0700529 * \param[in] msgb message buffer.
530 * \param[in] format format string.
531 * \returns 0 on success, -EINVAL on error.
Philipp Maierc5b47cc2017-10-10 16:53:21 +0200532 *
533 * The resulting string is printed to the msgb without a trailing nul
534 * character. A nul following the data tail may be written as an implementation
535 * detail, but a trailing nul is never part of the msgb data in terms of
536 * msgb_length().
537 *
538 * Note: the tailroom must always be one byte longer than the string to be
539 * written. The msgb is filled only up to tailroom=1. This is an implementation
540 * detail that allows leaving a nul character behind the valid data.
541 *
542 * In case of error, the msgb remains unchanged, though data may have been
543 * written to the (unused) memory after the tail pointer.
544 */
545int msgb_printf(struct msgb *msgb, const char *format, ...)
546{
547 va_list args;
548 int str_len;
549 int rc = 0;
550
551 OSMO_ASSERT(msgb);
552 OSMO_ASSERT(format);
553
554 /* Regardless of what we plan to add to the buffer, we must at least
555 * be able to store a string terminator (nullstring) */
556 if (msgb_tailroom(msgb) < 1)
557 return -EINVAL;
558
559 va_start(args, format);
560
561 str_len =
562 vsnprintf((char *)msgb->tail, msgb_tailroom(msgb), format, args);
563
564 if (str_len >= msgb_tailroom(msgb) || str_len < 0) {
565 rc = -EINVAL;
566 } else
567 msgb_put(msgb, str_len);
568
569 va_end(args);
570 return rc;
571}
572
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200573/*! @} */