Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 1 | /* (C) 2008 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 2 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 3 | * All Rights Reserved |
| 4 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | * |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 7 | * 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 Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 17 | */ |
| 18 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 19 | /*! \addtogroup msgb |
| 20 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 21 | * |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 22 | * libosmocore message buffers, inspired by Linux kernel skbuff |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 23 | * |
| 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 Yanitskiy | 8c8e6d7 | 2018-03-08 21:10:23 +0700 | [diff] [blame] | 37 | * * the currently occupied data for the message |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 38 | * * 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 Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 48 | * |
| 49 | * \file msgb.c |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 50 | */ |
| 51 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 52 | #include <unistd.h> |
| 53 | #include <string.h> |
| 54 | #include <stdlib.h> |
Neels Hofmeyr | 42fff58 | 2015-12-23 15:12:40 +0100 | [diff] [blame] | 55 | #include <inttypes.h> |
Philipp Maier | c5b47cc | 2017-10-10 16:53:21 +0200 | [diff] [blame] | 56 | #include <stdarg.h> |
| 57 | #include <errno.h> |
| 58 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 59 | #include <osmocom/core/msgb.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 60 | #include <osmocom/core/talloc.h> |
Vadim Yanitskiy | b423305 | 2018-03-08 21:35:48 +0700 | [diff] [blame] | 61 | #include <osmocom/core/logging.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 62 | |
Pau Espin Pedrol | 470cc12 | 2020-09-27 12:58:23 +0200 | [diff] [blame] | 63 | /*! Allocate a new message buffer from given talloc context |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 64 | * \param[in] ctx talloc context from which to allocate |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 65 | * \param[in] size Length in octets, including headroom |
| 66 | * \param[in] name Human-readable name to be associated with msgb |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 67 | * \returns dynamically-allocated \ref msgb |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 68 | * |
| 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 Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 73 | struct msgb *msgb_alloc_c(const void *ctx, uint16_t size, const char *name) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 74 | { |
| 75 | struct msgb *msg; |
| 76 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 77 | msg = talloc_named_const(ctx, sizeof(*msg) + size, name); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 78 | if (!msg) { |
Vadim Yanitskiy | b423305 | 2018-03-08 21:35:48 +0700 | [diff] [blame] | 79 | LOGP(DLGLOBAL, LOGL_FATAL, "Unable to allocate a msgb: " |
| 80 | "name='%s', size=%u\n", name, size); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 81 | return NULL; |
| 82 | } |
| 83 | |
Vadim Yanitskiy | 10959cd | 2018-03-08 21:57:42 +0700 | [diff] [blame] | 84 | /* Manually zero-initialize allocated memory */ |
| 85 | memset(msg, 0x00, sizeof(*msg) + size); |
| 86 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 87 | msg->data_len = size; |
| 88 | msg->len = 0; |
| 89 | msg->data = msg->_data; |
Sylvain Munaut | 17a5a28 | 2010-02-24 22:57:46 +0100 | [diff] [blame] | 90 | msg->head = msg->_data; |
| 91 | msg->tail = msg->_data; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 92 | |
| 93 | return msg; |
| 94 | } |
| 95 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 96 | /* default msgb allocation context for msgb_alloc() */ |
| 97 | void *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 | */ |
| 108 | struct msgb *msgb_alloc(uint16_t size, const char *name) |
| 109 | { |
| 110 | return msgb_alloc_c(tall_msgb_ctx, size, name); |
| 111 | } |
| 112 | |
| 113 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 114 | /*! Release given message buffer |
Vadim Yanitskiy | 8c8e6d7 | 2018-03-08 21:10:23 +0700 | [diff] [blame] | 115 | * \param[in] m Message buffer to be freed |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 116 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 117 | void msgb_free(struct msgb *m) |
| 118 | { |
| 119 | talloc_free(m); |
| 120 | } |
| 121 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 122 | /*! Enqueue message buffer to tail of a queue |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 123 | * \param[in] queue linked list header of queue |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 124 | * \param[in] msg message buffer to be added to the queue |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 125 | * |
| 126 | * The function will append the specified message buffer \a msg to the |
| 127 | * queue implemented by \ref llist_head \a queue |
| 128 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 129 | void msgb_enqueue(struct llist_head *queue, struct msgb *msg) |
| 130 | { |
| 131 | llist_add_tail(&msg->list, queue); |
| 132 | } |
| 133 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 134 | /*! Dequeue message buffer from head of queue |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 135 | * \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-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 139 | * implemented by \ref llist_head \a queue. |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 140 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 141 | struct 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; |
Max | d826f17 | 2016-06-23 13:14:02 +0200 | [diff] [blame] | 149 | |
| 150 | if (lh) { |
| 151 | llist_del(lh); |
| 152 | return llist_entry(lh, struct msgb, list); |
| 153 | } else |
| 154 | return NULL; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 157 | /*! Re-set all message buffer pointers |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 158 | * \param[in] msg message buffer that is to be resetted |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 159 | * |
| 160 | * This will re-set the various internal pointers into the underlying |
Vadim Yanitskiy | 8c8e6d7 | 2018-03-08 21:10:23 +0700 | [diff] [blame] | 161 | * message buffer, i.e. remove all headroom and treat the msgb as |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 162 | * completely empty. It also initializes the control buffer to zero. |
| 163 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 164 | void msgb_reset(struct msgb *msg) |
| 165 | { |
| 166 | msg->len = 0; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 167 | msg->data = msg->_data; |
Sylvain Munaut | 17a5a28 | 2010-02-24 22:57:46 +0100 | [diff] [blame] | 168 | msg->head = msg->_data; |
| 169 | msg->tail = msg->_data; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 170 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 171 | msg->trx = NULL; |
| 172 | msg->lchan = NULL; |
| 173 | msg->l2h = NULL; |
| 174 | msg->l3h = NULL; |
Harald Welte | bb77c9d | 2010-04-30 14:26:12 +0200 | [diff] [blame] | 175 | msg->l4h = NULL; |
Harald Welte | 95df5c0 | 2010-05-01 23:53:26 +0200 | [diff] [blame] | 176 | |
| 177 | memset(&msg->cb, 0, sizeof(msg->cb)); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 178 | } |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 179 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 180 | /*! get pointer to data section of message buffer |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 181 | * \param[in] msg message buffer |
| 182 | * \returns pointer to data section of message buffer |
| 183 | */ |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 184 | uint8_t *msgb_data(const struct msgb *msg) |
| 185 | { |
| 186 | return msg->data; |
| 187 | } |
| 188 | |
Max | db03825 | 2018-12-03 14:14:44 +0100 | [diff] [blame] | 189 | /*! 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 | */ |
| 202 | bool _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 | |
Max | a6749ac | 2019-01-08 14:42:30 +0100 | [diff] [blame] | 276 | LOGPC(DLGLOBAL, LOGL_FATAL, " msgb %s\n", osmo_hexdump(m_data, len)); |
Max | db03825 | 2018-12-03 14:14:44 +0100 | [diff] [blame] | 277 | |
| 278 | return false; |
| 279 | } |
| 280 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 281 | /*! get length of message buffer |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 282 | * \param[in] msg message buffer |
| 283 | * \returns length of data section in message buffer |
| 284 | */ |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 285 | uint16_t msgb_length(const struct msgb *msg) |
| 286 | { |
| 287 | return msg->len; |
| 288 | } |
Harald Welte | 9e1f060 | 2011-06-29 18:46:10 +0200 | [diff] [blame] | 289 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 290 | /*! Set the talloc context for \ref msgb_alloc |
Neels Hofmeyr | f45334b | 2016-09-16 00:15:56 +0200 | [diff] [blame] | 291 | * Deprecated, use msgb_talloc_ctx_init() instead. |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 292 | * \param[in] ctx talloc context to be used as root for msgb allocations |
| 293 | */ |
Harald Welte | 9e1f060 | 2011-06-29 18:46:10 +0200 | [diff] [blame] | 294 | void msgb_set_talloc_ctx(void *ctx) |
| 295 | { |
| 296 | tall_msgb_ctx = ctx; |
| 297 | } |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 298 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 299 | /*! Initialize a msgb talloc context for \ref msgb_alloc. |
Neels Hofmeyr | f45334b | 2016-09-16 00:15:56 +0200 | [diff] [blame] | 300 | * 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 | */ |
| 307 | void *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 | |
Max | df5b1e1 | 2022-10-14 20:39:40 +0300 | [diff] [blame] | 317 | /*! Copy an msgb with memory reallocation. |
Jacob Erlbeck | cdd05f0 | 2015-11-27 13:26:13 +0100 | [diff] [blame] | 318 | * |
Max | df5b1e1 | 2022-10-14 20:39:40 +0300 | [diff] [blame] | 319 | * This function allocates a new msgb with new_len size, copies the data buffer of msg, |
| 320 | * and adjusts the pointers (incl l1h-l4h) accordingly. The cb part is not copied. |
| 321 | * \param[in] ctx talloc context on which allocation happens |
Jacob Erlbeck | cdd05f0 | 2015-11-27 13:26:13 +0100 | [diff] [blame] | 322 | * \param[in] msg The old msgb object |
Max | df5b1e1 | 2022-10-14 20:39:40 +0300 | [diff] [blame] | 323 | * \param[in] new_len The length of new msgb object |
| 324 | * \param[in] name Human-readable name to be associated with new msgb |
Jacob Erlbeck | cdd05f0 | 2015-11-27 13:26:13 +0100 | [diff] [blame] | 325 | */ |
Max | df5b1e1 | 2022-10-14 20:39:40 +0300 | [diff] [blame] | 326 | struct msgb *msgb_copy_resize_c(const void *ctx, const struct msgb *msg, uint16_t new_len, const char *name) |
Jacob Erlbeck | cdd05f0 | 2015-11-27 13:26:13 +0100 | [diff] [blame] | 327 | { |
| 328 | struct msgb *new_msg; |
| 329 | |
Max | df5b1e1 | 2022-10-14 20:39:40 +0300 | [diff] [blame] | 330 | if (new_len < msgb_length(msg)) { |
| 331 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 332 | "Data from old msgb (%u bytes) won't fit into new msgb (%u bytes) after reallocation\n", |
| 333 | msgb_length(msg), new_len); |
| 334 | return NULL; |
| 335 | } |
| 336 | |
| 337 | new_msg = msgb_alloc_c(ctx, new_len, name); |
Jacob Erlbeck | cdd05f0 | 2015-11-27 13:26:13 +0100 | [diff] [blame] | 338 | if (!new_msg) |
| 339 | return NULL; |
| 340 | |
Jacob Erlbeck | cdd05f0 | 2015-11-27 13:26:13 +0100 | [diff] [blame] | 341 | /* copy header */ |
| 342 | new_msg->len = msg->len; |
| 343 | new_msg->data += msg->data - msg->_data; |
| 344 | new_msg->head += msg->head - msg->_data; |
| 345 | new_msg->tail += msg->tail - msg->_data; |
| 346 | |
Max | df5b1e1 | 2022-10-14 20:39:40 +0300 | [diff] [blame] | 347 | /* copy data */ |
| 348 | memcpy(new_msg->data, msg->data, msgb_length(msg)); |
| 349 | |
Jacob Erlbeck | cdd05f0 | 2015-11-27 13:26:13 +0100 | [diff] [blame] | 350 | if (msg->l1h) |
| 351 | new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data); |
| 352 | if (msg->l2h) |
| 353 | new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data); |
| 354 | if (msg->l3h) |
| 355 | new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data); |
| 356 | if (msg->l4h) |
| 357 | new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data); |
| 358 | |
| 359 | return new_msg; |
| 360 | } |
| 361 | |
Max | df5b1e1 | 2022-10-14 20:39:40 +0300 | [diff] [blame] | 362 | /*! Copy an msgb with memory reallocation. |
| 363 | * |
| 364 | * This function allocates a new msgb with new_len size, copies the data buffer of msg, |
| 365 | * and adjusts the pointers (incl l1h-l4h) accordingly. The cb part is not copied. |
| 366 | * \param[in] msg The old msgb object |
| 367 | * \param[in] name Human-readable name to be associated with new msgb |
| 368 | */ |
| 369 | struct msgb *msgb_copy_resize(const struct msgb *msg, uint16_t new_len, const char *name) |
| 370 | { |
| 371 | return msgb_copy_resize_c(tall_msgb_ctx, msg, new_len, name); |
| 372 | } |
| 373 | |
| 374 | /*! Copy an msgb. |
| 375 | * |
| 376 | * This function allocates a new msgb, copies the data buffer of msg, |
| 377 | * and adjusts the pointers (incl l1h-l4h) accordingly. The cb part |
| 378 | * is not copied. |
| 379 | * \param[in] ctx talloc context on which allocation happens |
| 380 | * \param[in] msg The old msgb object |
| 381 | * \param[in] name Human-readable name to be associated with msgb |
| 382 | */ |
| 383 | struct msgb *msgb_copy_c(const void *ctx, const struct msgb *msg, const char *name) |
| 384 | { |
| 385 | return msgb_copy_resize_c(ctx, msg, msg->data_len, name); |
| 386 | } |
| 387 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 388 | /*! Copy an msgb. |
| 389 | * |
| 390 | * This function allocates a new msgb, copies the data buffer of msg, |
| 391 | * and adjusts the pointers (incl l1h-l4h) accordingly. The cb part |
| 392 | * is not copied. |
| 393 | * \param[in] msg The old msgb object |
| 394 | * \param[in] name Human-readable name to be associated with msgb |
| 395 | */ |
| 396 | struct msgb *msgb_copy(const struct msgb *msg, const char *name) |
| 397 | { |
| 398 | return msgb_copy_c(tall_msgb_ctx, msg, name); |
| 399 | } |
| 400 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 401 | /*! Resize an area within an msgb |
Jacob Erlbeck | cdd05f0 | 2015-11-27 13:26:13 +0100 | [diff] [blame] | 402 | * |
| 403 | * This resizes a sub area of the msgb data and adjusts the pointers (incl |
| 404 | * l1h-l4h) accordingly. The cb part is not updated. If the area is extended, |
| 405 | * the contents of the extension is undefined. The complete sub area must be a |
| 406 | * part of [data,tail]. |
| 407 | * |
| 408 | * \param[inout] msg The msgb object |
| 409 | * \param[in] area A pointer to the sub-area |
| 410 | * \param[in] old_size The old size of the sub-area |
| 411 | * \param[in] new_size The new size of the sub-area |
| 412 | * \returns 0 on success, -1 if there is not enough space to extend the area |
| 413 | */ |
| 414 | int msgb_resize_area(struct msgb *msg, uint8_t *area, |
| 415 | int old_size, int new_size) |
| 416 | { |
| 417 | int rc; |
| 418 | uint8_t *post_start = area + old_size; |
| 419 | int pre_len = area - msg->data; |
| 420 | int post_len = msg->len - old_size - pre_len; |
| 421 | int delta_size = new_size - old_size; |
| 422 | |
| 423 | if (old_size < 0 || new_size < 0) |
| 424 | MSGB_ABORT(msg, "Negative sizes are not allowed\n"); |
| 425 | if (area < msg->data || post_start > msg->tail) |
| 426 | MSGB_ABORT(msg, "Sub area is not fully contained in the msg data\n"); |
| 427 | |
| 428 | if (delta_size == 0) |
| 429 | return 0; |
| 430 | |
| 431 | if (delta_size > 0) { |
| 432 | rc = msgb_trim(msg, msg->len + delta_size); |
| 433 | if (rc < 0) |
| 434 | return rc; |
| 435 | } |
| 436 | |
| 437 | memmove(area + new_size, area + old_size, post_len); |
| 438 | |
| 439 | if (msg->l1h >= post_start) |
| 440 | msg->l1h += delta_size; |
| 441 | if (msg->l2h >= post_start) |
| 442 | msg->l2h += delta_size; |
| 443 | if (msg->l3h >= post_start) |
| 444 | msg->l3h += delta_size; |
| 445 | if (msg->l4h >= post_start) |
| 446 | msg->l4h += delta_size; |
| 447 | |
| 448 | if (delta_size < 0) |
| 449 | msgb_trim(msg, msg->len + delta_size); |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 455 | /*! fill user-provided buffer with hexdump of the msg. |
| 456 | * \param[out] buf caller-allocated buffer for output string |
| 457 | * \param[in] buf_len length of buf |
| 458 | * \param[in] msg message buffer to be dumped |
| 459 | * \returns buf |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 460 | */ |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 461 | char *msgb_hexdump_buf(char *buf, size_t buf_len, const struct msgb *msg) |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 462 | { |
Harald Welte | 494282f | 2022-01-09 12:03:36 +0100 | [diff] [blame] | 463 | unsigned int buf_offs = 0; |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 464 | int nchars; |
| 465 | const unsigned char *start = msg->data; |
| 466 | const unsigned char *lxhs[4]; |
Harald Welte | 494282f | 2022-01-09 12:03:36 +0100 | [diff] [blame] | 467 | unsigned int i; |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 468 | |
| 469 | lxhs[0] = msg->l1h; |
| 470 | lxhs[1] = msg->l2h; |
| 471 | lxhs[2] = msg->l3h; |
| 472 | lxhs[3] = msg->l4h; |
| 473 | |
| 474 | for (i = 0; i < ARRAY_SIZE(lxhs); i++) { |
| 475 | if (!lxhs[i]) |
| 476 | continue; |
| 477 | |
Jacob Erlbeck | 86ec311 | 2015-11-27 13:26:14 +0100 | [diff] [blame] | 478 | if (lxhs[i] < msg->head) |
| 479 | continue; |
| 480 | if (lxhs[i] > msg->head + msg->data_len) |
| 481 | continue; |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 482 | if (lxhs[i] > msg->tail) |
Jacob Erlbeck | 86ec311 | 2015-11-27 13:26:14 +0100 | [diff] [blame] | 483 | continue; |
| 484 | if (lxhs[i] < msg->data || lxhs[i] > msg->tail) { |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 485 | nchars = snprintf(buf + buf_offs, buf_len - buf_offs, |
Neels Hofmeyr | 42fff58 | 2015-12-23 15:12:40 +0100 | [diff] [blame] | 486 | "(L%d=data%+" PRIdPTR ") ", |
Jacob Erlbeck | 86ec311 | 2015-11-27 13:26:14 +0100 | [diff] [blame] | 487 | i+1, lxhs[i] - msg->data); |
| 488 | buf_offs += nchars; |
| 489 | continue; |
| 490 | } |
| 491 | if (lxhs[i] < start) { |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 492 | nchars = snprintf(buf + buf_offs, buf_len - buf_offs, |
Neels Hofmeyr | 42fff58 | 2015-12-23 15:12:40 +0100 | [diff] [blame] | 493 | "(L%d%+" PRIdPTR ") ", i+1, |
| 494 | start - lxhs[i]); |
Jacob Erlbeck | 86ec311 | 2015-11-27 13:26:14 +0100 | [diff] [blame] | 495 | buf_offs += nchars; |
| 496 | continue; |
| 497 | } |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 498 | nchars = snprintf(buf + buf_offs, buf_len - buf_offs, |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 499 | "%s[L%d]> ", |
| 500 | osmo_hexdump(start, lxhs[i] - start), |
| 501 | i+1); |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 502 | if (nchars < 0 || nchars + buf_offs >= buf_len) |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 503 | return "ERROR"; |
| 504 | |
| 505 | buf_offs += nchars; |
| 506 | start = lxhs[i]; |
| 507 | } |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 508 | nchars = snprintf(buf + buf_offs, buf_len - buf_offs, |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 509 | "%s", osmo_hexdump(start, msg->tail - start)); |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 510 | if (nchars < 0 || nchars + buf_offs >= buf_len) |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 511 | return "ERROR"; |
| 512 | |
Jacob Erlbeck | 86ec311 | 2015-11-27 13:26:14 +0100 | [diff] [blame] | 513 | buf_offs += nchars; |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 514 | |
Jacob Erlbeck | 86ec311 | 2015-11-27 13:26:14 +0100 | [diff] [blame] | 515 | for (i = 0; i < ARRAY_SIZE(lxhs); i++) { |
| 516 | if (!lxhs[i]) |
| 517 | continue; |
| 518 | |
| 519 | if (lxhs[i] < msg->head || lxhs[i] > msg->head + msg->data_len) { |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 520 | nchars = snprintf(buf + buf_offs, buf_len - buf_offs, |
Jacob Erlbeck | 86ec311 | 2015-11-27 13:26:14 +0100 | [diff] [blame] | 521 | "(L%d out of range) ", i+1); |
| 522 | } else if (lxhs[i] <= msg->data + msg->data_len && |
| 523 | lxhs[i] > msg->tail) { |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 524 | nchars = snprintf(buf + buf_offs, buf_len - buf_offs, |
Neels Hofmeyr | 42fff58 | 2015-12-23 15:12:40 +0100 | [diff] [blame] | 525 | "(L%d=tail%+" PRIdPTR ") ", |
Jacob Erlbeck | 86ec311 | 2015-11-27 13:26:14 +0100 | [diff] [blame] | 526 | i+1, lxhs[i] - msg->tail); |
| 527 | } else |
| 528 | continue; |
| 529 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 530 | if (nchars < 0 || nchars + buf_offs >= buf_len) |
Jacob Erlbeck | 86ec311 | 2015-11-27 13:26:14 +0100 | [diff] [blame] | 531 | return "ERROR"; |
| 532 | buf_offs += nchars; |
| 533 | } |
| 534 | |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 535 | return buf; |
| 536 | } |
| 537 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 538 | /*! Return a (static) buffer containing a hexdump of the msg. |
| 539 | * \param[in] msg message buffer |
| 540 | * \returns a pointer to a static char array |
| 541 | */ |
| 542 | const char *msgb_hexdump(const struct msgb *msg) |
| 543 | { |
Harald Welte | 171ef82 | 2019-03-28 10:49:05 +0100 | [diff] [blame] | 544 | static __thread char buf[4100]; |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 545 | return msgb_hexdump_buf(buf, sizeof(buf), msg); |
| 546 | } |
Philipp Maier | c5b47cc | 2017-10-10 16:53:21 +0200 | [diff] [blame] | 547 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 548 | /*! Return a dynamically allocated buffer containing a hexdump of the msg |
| 549 | * \param[in] ctx talloc context from where to allocate the output string |
| 550 | * \param[in] msg message buffer |
| 551 | * \returns a pointer to a static char array |
| 552 | */ |
| 553 | char *msgb_hexdump_c(const void *ctx, const struct msgb *msg) |
| 554 | { |
Vadim Yanitskiy | 4f619c2 | 2019-04-12 21:48:07 +0700 | [diff] [blame] | 555 | size_t buf_len = msgb_length(msg) * 3 + 100; |
| 556 | char *buf = talloc_size(ctx, buf_len); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 557 | if (!buf) |
| 558 | return NULL; |
Vadim Yanitskiy | 4f619c2 | 2019-04-12 21:48:07 +0700 | [diff] [blame] | 559 | return msgb_hexdump_buf(buf, buf_len, msg); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 560 | } |
| 561 | |
Philipp Maier | c5b47cc | 2017-10-10 16:53:21 +0200 | [diff] [blame] | 562 | /*! Print a string to the end of message buffer. |
Vadim Yanitskiy | b8d06fb | 2019-03-25 23:59:11 +0700 | [diff] [blame] | 563 | * \param[in] msgb message buffer. |
| 564 | * \param[in] format format string. |
| 565 | * \returns 0 on success, -EINVAL on error. |
Philipp Maier | c5b47cc | 2017-10-10 16:53:21 +0200 | [diff] [blame] | 566 | * |
| 567 | * The resulting string is printed to the msgb without a trailing nul |
| 568 | * character. A nul following the data tail may be written as an implementation |
| 569 | * detail, but a trailing nul is never part of the msgb data in terms of |
| 570 | * msgb_length(). |
| 571 | * |
| 572 | * Note: the tailroom must always be one byte longer than the string to be |
| 573 | * written. The msgb is filled only up to tailroom=1. This is an implementation |
| 574 | * detail that allows leaving a nul character behind the valid data. |
| 575 | * |
| 576 | * In case of error, the msgb remains unchanged, though data may have been |
| 577 | * written to the (unused) memory after the tail pointer. |
| 578 | */ |
| 579 | int msgb_printf(struct msgb *msgb, const char *format, ...) |
| 580 | { |
| 581 | va_list args; |
| 582 | int str_len; |
| 583 | int rc = 0; |
| 584 | |
| 585 | OSMO_ASSERT(msgb); |
| 586 | OSMO_ASSERT(format); |
| 587 | |
| 588 | /* Regardless of what we plan to add to the buffer, we must at least |
| 589 | * be able to store a string terminator (nullstring) */ |
| 590 | if (msgb_tailroom(msgb) < 1) |
| 591 | return -EINVAL; |
| 592 | |
| 593 | va_start(args, format); |
| 594 | |
| 595 | str_len = |
| 596 | vsnprintf((char *)msgb->tail, msgb_tailroom(msgb), format, args); |
| 597 | |
| 598 | if (str_len >= msgb_tailroom(msgb) || str_len < 0) { |
| 599 | rc = -EINVAL; |
| 600 | } else |
| 601 | msgb_put(msgb, str_len); |
| 602 | |
| 603 | va_end(args); |
| 604 | return rc; |
| 605 | } |
| 606 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 607 | /*! @} */ |