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 | * |
| 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 Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 21 | /*! \addtogroup msgb |
| 22 | * @{ |
| 23 | */ |
| 24 | |
| 25 | /*! \file msgb.c |
| 26 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 27 | |
| 28 | #include <unistd.h> |
| 29 | #include <string.h> |
| 30 | #include <stdlib.h> |
Neels Hofmeyr | 42fff58 | 2015-12-23 15:12:40 +0100 | [diff] [blame] | 31 | #include <inttypes.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 32 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 33 | #include <osmocom/core/msgb.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 34 | //#include <openbsc/gsm_data.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 35 | #include <osmocom/core/talloc.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 36 | //#include <openbsc/debug.h> |
| 37 | |
Neels Hofmeyr | f45334b | 2016-09-16 00:15:56 +0200 | [diff] [blame] | 38 | void *tall_msgb_ctx = NULL; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 39 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 40 | /*! \brief Allocate a new message buffer |
| 41 | * \param[in] size Length in octets, including headroom |
| 42 | * \param[in] name Human-readable name to be associated with msgb |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 43 | * \returns dynamically-allocated \ref msgb |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 44 | * |
| 45 | * This function allocates a 'struct msgb' as well as the underlying |
| 46 | * memory buffer for the actual message data (size specified by \a size) |
| 47 | * using the talloc memory context previously set by \ref msgb_set_talloc_ctx |
| 48 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 49 | struct msgb *msgb_alloc(uint16_t size, const char *name) |
| 50 | { |
| 51 | struct msgb *msg; |
| 52 | |
| 53 | msg = _talloc_zero(tall_msgb_ctx, sizeof(*msg) + size, name); |
| 54 | |
| 55 | if (!msg) { |
| 56 | //LOGP(DRSL, LOGL_FATAL, "unable to allocate msgb\n"); |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | msg->data_len = size; |
| 61 | msg->len = 0; |
| 62 | msg->data = msg->_data; |
Sylvain Munaut | 17a5a28 | 2010-02-24 22:57:46 +0100 | [diff] [blame] | 63 | msg->head = msg->_data; |
| 64 | msg->tail = msg->_data; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 65 | |
| 66 | return msg; |
| 67 | } |
| 68 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 69 | /*! \brief Release given message buffer |
| 70 | * \param[in] m Message buffer to be free'd |
| 71 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 72 | void msgb_free(struct msgb *m) |
| 73 | { |
| 74 | talloc_free(m); |
| 75 | } |
| 76 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 77 | /*! \brief Enqueue message buffer to tail of a queue |
| 78 | * \param[in] queue linked list header of queue |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 79 | * \param[in] msg message buffer to be added to the queue |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 80 | * |
| 81 | * The function will append the specified message buffer \a msg to the |
| 82 | * queue implemented by \ref llist_head \a queue |
| 83 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 84 | void msgb_enqueue(struct llist_head *queue, struct msgb *msg) |
| 85 | { |
| 86 | llist_add_tail(&msg->list, queue); |
| 87 | } |
| 88 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 89 | /*! \brief Dequeue message buffer from head of queue |
| 90 | * \param[in] queue linked list header of queue |
| 91 | * \returns message buffer (if any) or NULL if queue empty |
| 92 | * |
| 93 | * The function will remove the first message buffer from the queue |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 94 | * implemented by \ref llist_head \a queue. |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 95 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 96 | struct msgb *msgb_dequeue(struct llist_head *queue) |
| 97 | { |
| 98 | struct llist_head *lh; |
| 99 | |
| 100 | if (llist_empty(queue)) |
| 101 | return NULL; |
| 102 | |
| 103 | lh = queue->next; |
Max | d826f17 | 2016-06-23 13:14:02 +0200 | [diff] [blame] | 104 | |
| 105 | if (lh) { |
| 106 | llist_del(lh); |
| 107 | return llist_entry(lh, struct msgb, list); |
| 108 | } else |
| 109 | return NULL; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 112 | /*! \brief Re-set all message buffer pointers |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 113 | * \param[in] msg message buffer that is to be resetted |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 114 | * |
| 115 | * This will re-set the various internal pointers into the underlying |
| 116 | * message buffer, i.e. remvoe all headroom and treat the msgb as |
| 117 | * completely empty. It also initializes the control buffer to zero. |
| 118 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 119 | void msgb_reset(struct msgb *msg) |
| 120 | { |
| 121 | msg->len = 0; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 122 | msg->data = msg->_data; |
Sylvain Munaut | 17a5a28 | 2010-02-24 22:57:46 +0100 | [diff] [blame] | 123 | msg->head = msg->_data; |
| 124 | msg->tail = msg->_data; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 125 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 126 | msg->trx = NULL; |
| 127 | msg->lchan = NULL; |
| 128 | msg->l2h = NULL; |
| 129 | msg->l3h = NULL; |
Harald Welte | bb77c9d | 2010-04-30 14:26:12 +0200 | [diff] [blame] | 130 | msg->l4h = NULL; |
Harald Welte | 95df5c0 | 2010-05-01 23:53:26 +0200 | [diff] [blame] | 131 | |
| 132 | memset(&msg->cb, 0, sizeof(msg->cb)); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 133 | } |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 134 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 135 | /*! \brief get pointer to data section of message buffer |
| 136 | * \param[in] msg message buffer |
| 137 | * \returns pointer to data section of message buffer |
| 138 | */ |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 139 | uint8_t *msgb_data(const struct msgb *msg) |
| 140 | { |
| 141 | return msg->data; |
| 142 | } |
| 143 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 144 | /*! \brief get length of message buffer |
| 145 | * \param[in] msg message buffer |
| 146 | * \returns length of data section in message buffer |
| 147 | */ |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 148 | uint16_t msgb_length(const struct msgb *msg) |
| 149 | { |
| 150 | return msg->len; |
| 151 | } |
Harald Welte | 9e1f060 | 2011-06-29 18:46:10 +0200 | [diff] [blame] | 152 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 153 | /*! \brief Set the talloc context for \ref msgb_alloc |
Neels Hofmeyr | f45334b | 2016-09-16 00:15:56 +0200 | [diff] [blame] | 154 | * Deprecated, use msgb_talloc_ctx_init() instead. |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 155 | * \param[in] ctx talloc context to be used as root for msgb allocations |
| 156 | */ |
Harald Welte | 9e1f060 | 2011-06-29 18:46:10 +0200 | [diff] [blame] | 157 | void msgb_set_talloc_ctx(void *ctx) |
| 158 | { |
| 159 | tall_msgb_ctx = ctx; |
| 160 | } |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 161 | |
Neels Hofmeyr | f45334b | 2016-09-16 00:15:56 +0200 | [diff] [blame] | 162 | /*! \brief Initialize a msgb talloc context for \ref msgb_alloc. |
| 163 | * Create a talloc context called "msgb". If \a pool_size is 0, create a named |
| 164 | * const as msgb talloc context. If \a pool_size is nonzero, create a talloc |
| 165 | * pool, possibly for faster msgb allocations (see talloc_pool()). |
| 166 | * \param[in] root_ctx talloc context used as parent for the new "msgb" ctx. |
| 167 | * \param[in] pool_size if nonzero, create a talloc pool of this size. |
| 168 | * \returns the new msgb talloc context, e.g. for reporting |
| 169 | */ |
| 170 | void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size) |
| 171 | { |
| 172 | if (!pool_size) |
| 173 | tall_msgb_ctx = talloc_size(root_ctx, 0); |
| 174 | else |
| 175 | tall_msgb_ctx = talloc_pool(root_ctx, pool_size); |
| 176 | talloc_set_name_const(tall_msgb_ctx, "msgb"); |
| 177 | return tall_msgb_ctx; |
| 178 | } |
| 179 | |
Jacob Erlbeck | cdd05f0 | 2015-11-27 13:26:13 +0100 | [diff] [blame] | 180 | /*! \brief Copy an msgb. |
| 181 | * |
| 182 | * This function allocates a new msgb, copies the data buffer of msg, |
| 183 | * and adjusts the pointers (incl l1h-l4h) accordingly. The cb part |
| 184 | * is not copied. |
| 185 | * \param[in] msg The old msgb object |
| 186 | * \param[in] name Human-readable name to be associated with msgb |
| 187 | */ |
| 188 | struct msgb *msgb_copy(const struct msgb *msg, const char *name) |
| 189 | { |
| 190 | struct msgb *new_msg; |
| 191 | |
| 192 | new_msg = msgb_alloc(msg->data_len, name); |
| 193 | if (!new_msg) |
| 194 | return NULL; |
| 195 | |
| 196 | /* copy data */ |
| 197 | memcpy(new_msg->_data, msg->_data, new_msg->data_len); |
| 198 | |
| 199 | /* copy header */ |
| 200 | new_msg->len = msg->len; |
| 201 | new_msg->data += msg->data - msg->_data; |
| 202 | new_msg->head += msg->head - msg->_data; |
| 203 | new_msg->tail += msg->tail - msg->_data; |
| 204 | |
| 205 | if (msg->l1h) |
| 206 | new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data); |
| 207 | if (msg->l2h) |
| 208 | new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data); |
| 209 | if (msg->l3h) |
| 210 | new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data); |
| 211 | if (msg->l4h) |
| 212 | new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data); |
| 213 | |
| 214 | return new_msg; |
| 215 | } |
| 216 | |
| 217 | /*! \brief Resize an area within an msgb |
| 218 | * |
| 219 | * This resizes a sub area of the msgb data and adjusts the pointers (incl |
| 220 | * l1h-l4h) accordingly. The cb part is not updated. If the area is extended, |
| 221 | * the contents of the extension is undefined. The complete sub area must be a |
| 222 | * part of [data,tail]. |
| 223 | * |
| 224 | * \param[inout] msg The msgb object |
| 225 | * \param[in] area A pointer to the sub-area |
| 226 | * \param[in] old_size The old size of the sub-area |
| 227 | * \param[in] new_size The new size of the sub-area |
| 228 | * \returns 0 on success, -1 if there is not enough space to extend the area |
| 229 | */ |
| 230 | int msgb_resize_area(struct msgb *msg, uint8_t *area, |
| 231 | int old_size, int new_size) |
| 232 | { |
| 233 | int rc; |
| 234 | uint8_t *post_start = area + old_size; |
| 235 | int pre_len = area - msg->data; |
| 236 | int post_len = msg->len - old_size - pre_len; |
| 237 | int delta_size = new_size - old_size; |
| 238 | |
| 239 | if (old_size < 0 || new_size < 0) |
| 240 | MSGB_ABORT(msg, "Negative sizes are not allowed\n"); |
| 241 | if (area < msg->data || post_start > msg->tail) |
| 242 | MSGB_ABORT(msg, "Sub area is not fully contained in the msg data\n"); |
| 243 | |
| 244 | if (delta_size == 0) |
| 245 | return 0; |
| 246 | |
| 247 | if (delta_size > 0) { |
| 248 | rc = msgb_trim(msg, msg->len + delta_size); |
| 249 | if (rc < 0) |
| 250 | return rc; |
| 251 | } |
| 252 | |
| 253 | memmove(area + new_size, area + old_size, post_len); |
| 254 | |
| 255 | if (msg->l1h >= post_start) |
| 256 | msg->l1h += delta_size; |
| 257 | if (msg->l2h >= post_start) |
| 258 | msg->l2h += delta_size; |
| 259 | if (msg->l3h >= post_start) |
| 260 | msg->l3h += delta_size; |
| 261 | if (msg->l4h >= post_start) |
| 262 | msg->l4h += delta_size; |
| 263 | |
| 264 | if (delta_size < 0) |
| 265 | msgb_trim(msg, msg->len + delta_size); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 271 | /*! \brief Return a (static) buffer containing a hexdump of the msg |
| 272 | * \param[in] msg message buffer |
| 273 | * \returns a pointer to a static char array |
| 274 | */ |
| 275 | const char *msgb_hexdump(const struct msgb *msg) |
| 276 | { |
| 277 | static char buf[4100]; |
| 278 | int buf_offs = 0; |
| 279 | int nchars; |
| 280 | const unsigned char *start = msg->data; |
| 281 | const unsigned char *lxhs[4]; |
| 282 | int i; |
| 283 | |
| 284 | lxhs[0] = msg->l1h; |
| 285 | lxhs[1] = msg->l2h; |
| 286 | lxhs[2] = msg->l3h; |
| 287 | lxhs[3] = msg->l4h; |
| 288 | |
| 289 | for (i = 0; i < ARRAY_SIZE(lxhs); i++) { |
| 290 | if (!lxhs[i]) |
| 291 | continue; |
| 292 | |
Jacob Erlbeck | 86ec311 | 2015-11-27 13:26:14 +0100 | [diff] [blame] | 293 | if (lxhs[i] < msg->head) |
| 294 | continue; |
| 295 | if (lxhs[i] > msg->head + msg->data_len) |
| 296 | continue; |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 297 | if (lxhs[i] > msg->tail) |
Jacob Erlbeck | 86ec311 | 2015-11-27 13:26:14 +0100 | [diff] [blame] | 298 | continue; |
| 299 | if (lxhs[i] < msg->data || lxhs[i] > msg->tail) { |
| 300 | nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs, |
Neels Hofmeyr | 42fff58 | 2015-12-23 15:12:40 +0100 | [diff] [blame] | 301 | "(L%d=data%+" PRIdPTR ") ", |
Jacob Erlbeck | 86ec311 | 2015-11-27 13:26:14 +0100 | [diff] [blame] | 302 | i+1, lxhs[i] - msg->data); |
| 303 | buf_offs += nchars; |
| 304 | continue; |
| 305 | } |
| 306 | if (lxhs[i] < start) { |
| 307 | nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs, |
Neels Hofmeyr | 42fff58 | 2015-12-23 15:12:40 +0100 | [diff] [blame] | 308 | "(L%d%+" PRIdPTR ") ", i+1, |
| 309 | start - lxhs[i]); |
Jacob Erlbeck | 86ec311 | 2015-11-27 13:26:14 +0100 | [diff] [blame] | 310 | buf_offs += nchars; |
| 311 | continue; |
| 312 | } |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 313 | nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs, |
| 314 | "%s[L%d]> ", |
| 315 | osmo_hexdump(start, lxhs[i] - start), |
| 316 | i+1); |
| 317 | if (nchars < 0 || nchars + buf_offs >= sizeof(buf)) |
| 318 | return "ERROR"; |
| 319 | |
| 320 | buf_offs += nchars; |
| 321 | start = lxhs[i]; |
| 322 | } |
| 323 | nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs, |
| 324 | "%s", osmo_hexdump(start, msg->tail - start)); |
| 325 | if (nchars < 0 || nchars + buf_offs >= sizeof(buf)) |
| 326 | return "ERROR"; |
| 327 | |
Jacob Erlbeck | 86ec311 | 2015-11-27 13:26:14 +0100 | [diff] [blame] | 328 | buf_offs += nchars; |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 329 | |
Jacob Erlbeck | 86ec311 | 2015-11-27 13:26:14 +0100 | [diff] [blame] | 330 | for (i = 0; i < ARRAY_SIZE(lxhs); i++) { |
| 331 | if (!lxhs[i]) |
| 332 | continue; |
| 333 | |
| 334 | if (lxhs[i] < msg->head || lxhs[i] > msg->head + msg->data_len) { |
| 335 | nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs, |
| 336 | "(L%d out of range) ", i+1); |
| 337 | } else if (lxhs[i] <= msg->data + msg->data_len && |
| 338 | lxhs[i] > msg->tail) { |
| 339 | nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs, |
Neels Hofmeyr | 42fff58 | 2015-12-23 15:12:40 +0100 | [diff] [blame] | 340 | "(L%d=tail%+" PRIdPTR ") ", |
Jacob Erlbeck | 86ec311 | 2015-11-27 13:26:14 +0100 | [diff] [blame] | 341 | i+1, lxhs[i] - msg->tail); |
| 342 | } else |
| 343 | continue; |
| 344 | |
| 345 | if (nchars < 0 || nchars + buf_offs >= sizeof(buf)) |
| 346 | return "ERROR"; |
| 347 | buf_offs += nchars; |
| 348 | } |
| 349 | |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 350 | return buf; |
| 351 | } |
| 352 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 353 | /*! @} */ |