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