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