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