Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 1 | #pragma once |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 2 | |
| 3 | /* (C) 2008 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <stdint.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 23 | #include <osmocom/core/linuxlist.h> |
| 24 | #include <osmocom/core/utils.h> |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 25 | #include <osmocom/core/bits.h> |
Neels Hofmeyr | f45334b | 2016-09-16 00:15:56 +0200 | [diff] [blame] | 26 | #include <osmocom/core/defs.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 27 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 28 | /*! \defgroup msgb Message buffers |
| 29 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 30 | * \file msgb.h */ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 31 | |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 32 | #define MSGB_DEBUG |
| 33 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 34 | /*! Osmocom message buffer */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 35 | struct msgb { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 36 | struct llist_head list; /*!< linked list header */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 37 | |
Pablo Neira Ayuso | 29cbf61 | 2011-07-07 19:46:44 +0200 | [diff] [blame] | 38 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 39 | /* Part of which TRX logical channel we were received / transmitted */ |
Harald Welte | 074c9f9 | 2010-04-30 14:29:11 +0200 | [diff] [blame] | 40 | /* FIXME: move them into the control buffer */ |
Harald Welte | c5a0ded | 2011-07-18 16:59:27 +0200 | [diff] [blame] | 41 | union { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 42 | void *dst; /*!< reference of origin/destination */ |
Harald Welte | c5a0ded | 2011-07-18 16:59:27 +0200 | [diff] [blame] | 43 | struct gsm_bts_trx *trx; |
| 44 | }; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 45 | struct gsm_lchan *lchan; /*!< logical channel */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 46 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 47 | unsigned char *l1h; /*!< pointer to Layer1 header (if any) */ |
| 48 | unsigned char *l2h; /*!< pointer to A-bis layer 2 header: OML, RSL(RLL), NS */ |
| 49 | unsigned char *l3h; /*!< pointer to Layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */ |
| 50 | unsigned char *l4h; /*!< pointer to layer 4 header */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 51 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 52 | unsigned long cb[5]; /*!< control buffer */ |
Harald Welte | 074c9f9 | 2010-04-30 14:29:11 +0200 | [diff] [blame] | 53 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 54 | uint16_t data_len; /*!< length of underlying data array */ |
| 55 | uint16_t len; /*!< length of bytes used in msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 56 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 57 | unsigned char *head; /*!< start of underlying memory buffer */ |
| 58 | unsigned char *tail; /*!< end of message in buffer */ |
| 59 | unsigned char *data; /*!< start of message in buffer */ |
| 60 | unsigned char _data[0]; /*!< optional immediate data array */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 61 | }; |
| 62 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 63 | extern 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] | 64 | extern struct msgb *msgb_alloc(uint16_t size, const char *name); |
| 65 | extern void msgb_free(struct msgb *m); |
| 66 | extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg); |
| 67 | extern struct msgb *msgb_dequeue(struct llist_head *queue); |
| 68 | extern void msgb_reset(struct msgb *m); |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 69 | uint16_t msgb_length(const struct msgb *msg); |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 70 | extern const char *msgb_hexdump(const struct msgb *msg); |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 71 | char *msgb_hexdump_buf(char *buf, size_t buf_len, const struct msgb *msg); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 72 | char *msgb_hexdump_c(const void *ctx, const struct msgb *msg); |
Jacob Erlbeck | cdd05f0 | 2015-11-27 13:26:13 +0100 | [diff] [blame] | 73 | extern int msgb_resize_area(struct msgb *msg, uint8_t *area, |
| 74 | int old_size, int new_size); |
| 75 | extern struct msgb *msgb_copy(const struct msgb *msg, const char *name); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 76 | extern struct msgb *msgb_copy_c(const void *ctx, const struct msgb *msg, const char *name); |
Jacob Erlbeck | 8db1134 | 2015-11-27 13:26:15 +0100 | [diff] [blame] | 77 | static int msgb_test_invariant(const struct msgb *msg) __attribute__((pure)); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 78 | |
Neels Hofmeyr | 3fad5d7 | 2017-11-20 14:15:03 +0100 | [diff] [blame] | 79 | /*! Free all msgbs from a queue built with msgb_enqueue(). |
| 80 | * \param[in] queue list head of a msgb queue. |
| 81 | */ |
| 82 | static inline void msgb_queue_free(struct llist_head *queue) |
| 83 | { |
| 84 | struct msgb *msg; |
| 85 | while ((msg = msgb_dequeue(queue))) msgb_free(msg); |
| 86 | } |
| 87 | |
Pau Espin Pedrol | 8ce6f48 | 2018-08-17 10:33:57 +0200 | [diff] [blame] | 88 | /*! Enqueue message buffer to tail of a queue and increment queue size counter |
| 89 | * \param[in] queue linked list header of queue |
| 90 | * \param[in] msg message buffer to be added to the queue |
| 91 | * \param[in] count pointer to variable holding size of the queue |
| 92 | * |
| 93 | * The function will append the specified message buffer \a msg to the queue |
| 94 | * implemented by \ref llist_head \a queue using function \ref msgb_enqueue_count, |
| 95 | * then increment \a count |
| 96 | */ |
| 97 | static inline void msgb_enqueue_count(struct llist_head *queue, struct msgb *msg, |
| 98 | unsigned int *count) |
| 99 | { |
| 100 | msgb_enqueue(queue, msg); |
| 101 | (*count)++; |
| 102 | } |
| 103 | |
| 104 | /*! Dequeue message buffer from head of queue and decrement queue size counter |
| 105 | * \param[in] queue linked list header of queue |
| 106 | * \param[in] count pointer to variable holding size of the queue |
| 107 | * \returns message buffer (if any) or NULL if queue empty |
| 108 | * |
| 109 | * The function will remove the first message buffer from the queue |
| 110 | * implemented by \ref llist_head \a queue using function \ref msgb_enqueue_count, |
| 111 | * and decrement \a count, all if queue is not empty. |
| 112 | */ |
| 113 | static inline struct msgb *msgb_dequeue_count(struct llist_head *queue, |
| 114 | unsigned int *count) |
| 115 | { |
| 116 | struct msgb *msg = msgb_dequeue(queue); |
| 117 | if (msg) |
| 118 | (*count)--; |
| 119 | return msg; |
| 120 | } |
| 121 | |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 122 | #ifdef MSGB_DEBUG |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 123 | #include <osmocom/core/panic.h> |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 124 | #define MSGB_ABORT(msg, fmt, args ...) do { \ |
Harald Welte | dab0287 | 2010-11-09 13:42:26 +0100 | [diff] [blame] | 125 | osmo_panic("msgb(%p): " fmt, msg, ## args); \ |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 126 | } while(0) |
| 127 | #else |
| 128 | #define MSGB_ABORT(msg, fmt, args ...) |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 129 | #endif |
| 130 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 131 | /*! obtain L1 header of msgb */ |
Harald Welte | fdd0a70 | 2010-03-01 22:30:51 +0100 | [diff] [blame] | 132 | #define msgb_l1(m) ((void *)(m->l1h)) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 133 | /*! obtain L2 header of msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 134 | #define msgb_l2(m) ((void *)(m->l2h)) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 135 | /*! obtain L3 header of msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 136 | #define msgb_l3(m) ((void *)(m->l3h)) |
Max | cb02a39 | 2018-12-03 14:00:49 +0100 | [diff] [blame] | 137 | /*! obtain L4 header of msgb */ |
| 138 | #define msgb_l4(m) ((void *)(m->l4h)) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 139 | /*! obtain SMS header of msgb */ |
Max | cb02a39 | 2018-12-03 14:00:49 +0100 | [diff] [blame] | 140 | #define msgb_sms(m) msgb_l4(m) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 141 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 142 | /*! determine length of L1 message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 143 | * \param[in] msgb message buffer |
| 144 | * \returns size of L1 message in bytes |
| 145 | * |
| 146 | * This function computes the number of bytes between the tail of the |
| 147 | * message and the layer 1 header. |
| 148 | */ |
Harald Welte | fdd0a70 | 2010-03-01 22:30:51 +0100 | [diff] [blame] | 149 | static inline unsigned int msgb_l1len(const struct msgb *msgb) |
| 150 | { |
| 151 | return msgb->tail - (uint8_t *)msgb_l1(msgb); |
| 152 | } |
| 153 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 154 | /*! determine length of L2 message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 155 | * \param[in] msgb message buffer |
| 156 | * \returns size of L2 message in bytes |
| 157 | * |
| 158 | * This function computes the number of bytes between the tail of the |
| 159 | * message and the layer 2 header. |
| 160 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 161 | static inline unsigned int msgb_l2len(const struct msgb *msgb) |
| 162 | { |
| 163 | return msgb->tail - (uint8_t *)msgb_l2(msgb); |
| 164 | } |
| 165 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 166 | /*! determine length of L3 message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 167 | * \param[in] msgb message buffer |
| 168 | * \returns size of L3 message in bytes |
| 169 | * |
| 170 | * This function computes the number of bytes between the tail of the |
| 171 | * message and the layer 3 header. |
| 172 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 173 | static inline unsigned int msgb_l3len(const struct msgb *msgb) |
| 174 | { |
| 175 | return msgb->tail - (uint8_t *)msgb_l3(msgb); |
| 176 | } |
| 177 | |
Vadim Yanitskiy | e521ede | 2018-11-07 03:53:03 +0700 | [diff] [blame] | 178 | /*! determine length of L4 message |
| 179 | * \param[in] msgb message buffer |
| 180 | * \returns size of L4 message in bytes |
| 181 | * |
| 182 | * This function computes the number of bytes between the tail of the |
| 183 | * message and the layer 4 header. |
| 184 | */ |
| 185 | static inline unsigned int msgb_l4len(const struct msgb *msgb) |
| 186 | { |
| 187 | return msgb->tail - (uint8_t *)msgb_sms(msgb); |
| 188 | } |
| 189 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 190 | /*! determine the length of the header |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 191 | * \param[in] msgb message buffer |
| 192 | * \returns number of bytes between start of buffer and start of msg |
| 193 | * |
| 194 | * This function computes the length difference between the underlying |
| 195 | * data buffer and the used section of the \a msgb. |
| 196 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 197 | static inline unsigned int msgb_headlen(const struct msgb *msgb) |
| 198 | { |
| 199 | return msgb->len - msgb->data_len; |
| 200 | } |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 201 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 202 | /*! determine how much tail room is left in msgb |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 203 | * \param[in] msgb message buffer |
| 204 | * \returns number of bytes remaining at end of msgb |
| 205 | * |
| 206 | * This function computes the amount of octets left in the underlying |
| 207 | * data buffer after the end of the message. |
| 208 | */ |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 209 | static inline int msgb_tailroom(const struct msgb *msgb) |
| 210 | { |
| 211 | return (msgb->head + msgb->data_len) - msgb->tail; |
| 212 | } |
| 213 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 214 | /*! determine the amount of headroom in msgb |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 215 | * \param[in] msgb message buffer |
| 216 | * \returns number of bytes left ahead of message start in msgb |
| 217 | * |
| 218 | * This function computes the amount of bytes left in the underlying |
| 219 | * data buffer before the start of the actual message. |
| 220 | */ |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 221 | static inline int msgb_headroom(const struct msgb *msgb) |
| 222 | { |
| 223 | return (msgb->data - msgb->head); |
| 224 | } |
| 225 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 226 | /*! append data to end of message buffer |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 227 | * \param[in] msgb message buffer |
| 228 | * \param[in] len number of bytes to append to message |
| 229 | * \returns pointer to start of newly-appended data |
| 230 | * |
| 231 | * This function will move the \a tail pointer of the message buffer \a |
| 232 | * len bytes further, thus enlarging the message by \a len bytes. |
| 233 | * |
| 234 | * The return value is a pointer to start of the newly added section at |
| 235 | * the end of the message and can be used for actually filling/copying |
| 236 | * data into it. |
| 237 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 238 | static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len) |
| 239 | { |
| 240 | unsigned char *tmp = msgb->tail; |
Harald Welte | 46a45a6 | 2010-11-09 13:41:48 +0100 | [diff] [blame] | 241 | if (msgb_tailroom(msgb) < (int) len) |
Neels Hofmeyr | 418ca58 | 2016-09-09 02:03:43 +0200 | [diff] [blame] | 242 | MSGB_ABORT(msgb, "Not enough tailroom msgb_put (%u < %u)\n", |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 243 | msgb_tailroom(msgb), len); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 244 | msgb->tail += len; |
| 245 | msgb->len += len; |
| 246 | return tmp; |
| 247 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 248 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 249 | /*! append a uint8 value to the end of the message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 250 | * \param[in] msgb message buffer |
| 251 | * \param[in] word unsigned 8bit byte to be appended |
| 252 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 253 | static inline void msgb_put_u8(struct msgb *msgb, uint8_t word) |
| 254 | { |
| 255 | uint8_t *space = msgb_put(msgb, 1); |
| 256 | space[0] = word & 0xFF; |
| 257 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 258 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 259 | /*! append a uint16 value to the end of the message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 260 | * \param[in] msgb message buffer |
| 261 | * \param[in] word unsigned 16bit byte to be appended |
| 262 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 263 | static inline void msgb_put_u16(struct msgb *msgb, uint16_t word) |
| 264 | { |
| 265 | uint8_t *space = msgb_put(msgb, 2); |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 266 | osmo_store16be(word, space); |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 267 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 268 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 269 | /*! append a uint32 value to the end of the message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 270 | * \param[in] msgb message buffer |
| 271 | * \param[in] word unsigned 32bit byte to be appended |
| 272 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 273 | static inline void msgb_put_u32(struct msgb *msgb, uint32_t word) |
| 274 | { |
| 275 | uint8_t *space = msgb_put(msgb, 4); |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 276 | osmo_store32be(word, space); |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 277 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 278 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 279 | /*! remove data from end of message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 280 | * \param[in] msgb message buffer |
| 281 | * \param[in] len number of bytes to remove from end |
| 282 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 283 | static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len) |
| 284 | { |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 285 | if (msgb_length(msgb) < len) |
| 286 | MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n", |
| 287 | len, msgb_length(msgb)); |
| 288 | msgb->tail -= len; |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 289 | msgb->len -= len; |
Pau Espin Pedrol | 9fa0912 | 2018-04-11 20:09:13 +0200 | [diff] [blame] | 290 | return msgb->tail; |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 291 | } |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 292 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 293 | /*! remove uint8 from end of message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 294 | * \param[in] msgb message buffer |
| 295 | * \returns 8bit value taken from end of msgb |
| 296 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 297 | static inline uint8_t msgb_get_u8(struct msgb *msgb) |
| 298 | { |
| 299 | uint8_t *space = msgb_get(msgb, 1); |
| 300 | return space[0]; |
| 301 | } |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 302 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 303 | /*! remove uint16 from end of message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 304 | * \param[in] msgb message buffer |
| 305 | * \returns 16bit value taken from end of msgb |
| 306 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 307 | static inline uint16_t msgb_get_u16(struct msgb *msgb) |
| 308 | { |
| 309 | uint8_t *space = msgb_get(msgb, 2); |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 310 | return osmo_load16be(space); |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 311 | } |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 312 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 313 | /*! remove uint32 from end of message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 314 | * \param[in] msgb message buffer |
| 315 | * \returns 32bit value taken from end of msgb |
| 316 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 317 | static inline uint32_t msgb_get_u32(struct msgb *msgb) |
| 318 | { |
| 319 | uint8_t *space = msgb_get(msgb, 4); |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 320 | return osmo_load32be(space); |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 321 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 322 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 323 | /*! prepend (push) some data to start of message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 324 | * \param[in] msgb message buffer |
| 325 | * \param[in] len number of bytes to pre-pend |
| 326 | * \returns pointer to newly added portion at start of \a msgb |
| 327 | * |
| 328 | * This function moves the \a data pointer of the \ref msgb further |
| 329 | * to the front (by \a len bytes), thereby enlarging the message by \a |
| 330 | * len bytes. |
| 331 | * |
| 332 | * The return value is a pointer to the newly added section in the |
| 333 | * beginning of the message. It can be used to fill/copy data into it. |
| 334 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 335 | static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len) |
| 336 | { |
Harald Welte | 46a45a6 | 2010-11-09 13:41:48 +0100 | [diff] [blame] | 337 | if (msgb_headroom(msgb) < (int) len) |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 338 | MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n", |
| 339 | msgb_headroom(msgb), len); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 340 | msgb->data -= len; |
| 341 | msgb->len += len; |
| 342 | return msgb->data; |
| 343 | } |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 344 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 345 | /*! prepend a uint8 value to the head of the message |
Vadim Yanitskiy | 914a8ec | 2019-03-26 00:20:35 +0700 | [diff] [blame] | 346 | * \param[in] msg message buffer |
Harald Welte | 8593f1f | 2016-11-11 19:26:51 +0100 | [diff] [blame] | 347 | * \param[in] word unsigned 8bit byte to be prepended |
| 348 | */ |
| 349 | static inline void msgb_push_u8(struct msgb *msg, uint8_t word) |
| 350 | { |
| 351 | uint8_t *space = msgb_push(msg, 1); |
| 352 | space[0] = word; |
| 353 | } |
| 354 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 355 | /*! prepend a uint16 value to the head of the message |
Vadim Yanitskiy | 914a8ec | 2019-03-26 00:20:35 +0700 | [diff] [blame] | 356 | * \param[in] msg message buffer |
Harald Welte | 8593f1f | 2016-11-11 19:26:51 +0100 | [diff] [blame] | 357 | * \param[in] word unsigned 16bit byte to be prepended |
| 358 | */ |
| 359 | static inline void msgb_push_u16(struct msgb *msg, uint16_t word) |
| 360 | { |
| 361 | uint16_t *space = (uint16_t *) msgb_push(msg, 2); |
| 362 | osmo_store16be(word, space); |
| 363 | } |
| 364 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 365 | /*! prepend a uint32 value to the head of the message |
Vadim Yanitskiy | 914a8ec | 2019-03-26 00:20:35 +0700 | [diff] [blame] | 366 | * \param[in] msg message buffer |
Harald Welte | 8593f1f | 2016-11-11 19:26:51 +0100 | [diff] [blame] | 367 | * \param[in] word unsigned 32bit byte to be prepended |
| 368 | */ |
| 369 | static inline void msgb_push_u32(struct msgb *msg, uint32_t word) |
| 370 | { |
| 371 | uint32_t *space = (uint32_t *) msgb_push(msg, 4); |
| 372 | osmo_store32be(word, space); |
| 373 | } |
| 374 | |
Max | b27e6fe | 2019-01-21 14:19:59 +0100 | [diff] [blame] | 375 | static inline unsigned char *msgb_push_tl(struct msgb *msgb, uint8_t tag) |
Max | 84fb5bb | 2018-11-08 13:23:59 +0100 | [diff] [blame] | 376 | { |
| 377 | uint8_t *data = msgb_push(msgb, 2); |
| 378 | |
| 379 | data[0] = tag; |
| 380 | data[1] = msgb->len - 2; |
| 381 | return data; |
| 382 | } |
| 383 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 384 | /*! remove (pull) a header from the front of the message buffer |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 385 | * \param[in] msgb message buffer |
| 386 | * \param[in] len number of octets to be pulled |
| 387 | * \returns pointer to new start of msgb |
| 388 | * |
| 389 | * This function moves the \a data pointer of the \ref msgb further back |
| 390 | * in the message, thereby shrinking the size of the message by \a len |
| 391 | * bytes. |
| 392 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 393 | static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len) |
| 394 | { |
Pau Espin Pedrol | c62fc2d | 2018-04-11 20:14:08 +0200 | [diff] [blame] | 395 | if (msgb_length(msgb) < len) |
| 396 | MSGB_ABORT(msgb, "msgb too small to pull %u (len %u)\n", |
| 397 | len, msgb_length(msgb)); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 398 | msgb->len -= len; |
| 399 | return msgb->data += len; |
| 400 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 401 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 402 | /*! remove (pull) all headers in front of l3h from the message buffer. |
Vadim Yanitskiy | 914a8ec | 2019-03-26 00:20:35 +0700 | [diff] [blame] | 403 | * \param[in] msg message buffer with a valid l3h |
Jacob Erlbeck | 8dac415 | 2014-01-28 11:03:11 +0100 | [diff] [blame] | 404 | * \returns pointer to new start of msgb (l3h) |
| 405 | * |
| 406 | * This function moves the \a data pointer of the \ref msgb further back |
| 407 | * in the message, thereby shrinking the size of the message. |
| 408 | * l1h and l2h will be cleared. |
| 409 | */ |
| 410 | static inline unsigned char *msgb_pull_to_l3(struct msgb *msg) |
| 411 | { |
| 412 | unsigned char *ret = msgb_pull(msg, msg->l3h - msg->data); |
| 413 | msg->l1h = msg->l2h = NULL; |
| 414 | return ret; |
| 415 | } |
| 416 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 417 | /*! remove (pull) all headers in front of l2h from the message buffer. |
Vadim Yanitskiy | 914a8ec | 2019-03-26 00:20:35 +0700 | [diff] [blame] | 418 | * \param[in] msg message buffer with a valid l2h |
Harald Welte | 59a9ebf | 2017-04-15 19:04:34 +0200 | [diff] [blame] | 419 | * \returns pointer to new start of msgb (l2h) |
| 420 | * |
| 421 | * This function moves the \a data pointer of the \ref msgb further back |
| 422 | * in the message, thereby shrinking the size of the message. |
| 423 | * l1h will be cleared. |
| 424 | */ |
| 425 | static inline unsigned char *msgb_pull_to_l2(struct msgb *msg) |
| 426 | { |
| 427 | unsigned char *ret = msgb_pull(msg, msg->l2h - msg->data); |
| 428 | msg->l1h = NULL; |
| 429 | return ret; |
| 430 | } |
| 431 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 432 | /*! remove uint8 from front of message |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 433 | * \param[in] msgb message buffer |
| 434 | * \returns 8bit value taken from end of msgb |
| 435 | */ |
| 436 | static inline uint8_t msgb_pull_u8(struct msgb *msgb) |
| 437 | { |
Steve Markgraf | 5905d5b | 2012-11-14 19:36:00 +0100 | [diff] [blame] | 438 | uint8_t *space = msgb_pull(msgb, 1) - 1; |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 439 | return space[0]; |
| 440 | } |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 441 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 442 | /*! remove uint16 from front of message |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 443 | * \param[in] msgb message buffer |
| 444 | * \returns 16bit value taken from end of msgb |
| 445 | */ |
| 446 | static inline uint16_t msgb_pull_u16(struct msgb *msgb) |
| 447 | { |
Steve Markgraf | 5905d5b | 2012-11-14 19:36:00 +0100 | [diff] [blame] | 448 | uint8_t *space = msgb_pull(msgb, 2) - 2; |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 449 | return osmo_load16be(space); |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 450 | } |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 451 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 452 | /*! remove uint32 from front of message |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 453 | * \param[in] msgb message buffer |
| 454 | * \returns 32bit value taken from end of msgb |
| 455 | */ |
| 456 | static inline uint32_t msgb_pull_u32(struct msgb *msgb) |
| 457 | { |
Steve Markgraf | 5905d5b | 2012-11-14 19:36:00 +0100 | [diff] [blame] | 458 | uint8_t *space = msgb_pull(msgb, 4) - 4; |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 459 | return osmo_load32be(space); |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 460 | } |
| 461 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 462 | /*! Increase headroom of empty msgb, reducing the tailroom |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 463 | * \param[in] msg message buffer |
| 464 | * \param[in] len amount of extra octets to be reserved as headroom |
| 465 | * |
| 466 | * This function reserves some memory at the beginning of the underlying |
| 467 | * data buffer. The idea is to reserve space in case further headers |
| 468 | * have to be pushed to the \ref msgb during further processing. |
| 469 | * |
| 470 | * Calling this function leads to undefined reusults if it is called on |
| 471 | * a non-empty \ref msgb. |
| 472 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 473 | static inline void msgb_reserve(struct msgb *msg, int len) |
| 474 | { |
| 475 | msg->data += len; |
| 476 | msg->tail += len; |
| 477 | } |
| 478 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 479 | /*! Trim the msgb to a given absolute length |
Harald Welte | b0f9151 | 2012-01-14 21:53:41 +0100 | [diff] [blame] | 480 | * \param[in] msg message buffer |
| 481 | * \param[in] len new total length of buffer |
| 482 | * \returns 0 in case of success, negative in case of error |
| 483 | */ |
| 484 | static inline int msgb_trim(struct msgb *msg, int len) |
| 485 | { |
Jacob Erlbeck | ff42b26 | 2015-11-27 13:26:19 +0100 | [diff] [blame] | 486 | if (len < 0) |
| 487 | MSGB_ABORT(msg, "Negative length is not allowed\n"); |
Harald Welte | 3068d57 | 2012-01-14 22:07:59 +0100 | [diff] [blame] | 488 | if (len > msg->data_len) |
Harald Welte | b0f9151 | 2012-01-14 21:53:41 +0100 | [diff] [blame] | 489 | return -1; |
| 490 | |
Harald Welte | 3068d57 | 2012-01-14 22:07:59 +0100 | [diff] [blame] | 491 | msg->len = len; |
| 492 | msg->tail = msg->data + len; |
Harald Welte | b0f9151 | 2012-01-14 21:53:41 +0100 | [diff] [blame] | 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 497 | /*! Trim the msgb to a given layer3 length |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 498 | * \param[in] msg message buffer |
Harald Welte | b0f9151 | 2012-01-14 21:53:41 +0100 | [diff] [blame] | 499 | * \param[in] l3len new layer3 length |
| 500 | * \returns 0 in case of success, negative in case of error |
| 501 | */ |
| 502 | static inline int msgb_l3trim(struct msgb *msg, int l3len) |
| 503 | { |
| 504 | return msgb_trim(msg, (msg->l3h - msg->data) + l3len); |
| 505 | } |
| 506 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 507 | /*! Allocate message buffer with specified headroom from specified talloc context. |
| 508 | * \param[in] ctx talloc context from which to allocate |
| 509 | * \param[in] size size in bytes, including headroom |
| 510 | * \param[in] headroom headroom in bytes |
| 511 | * \param[in] name human-readable name |
| 512 | * \returns allocated message buffer with specified headroom |
| 513 | * |
| 514 | * This function is a convenience wrapper around \ref msgb_alloc |
| 515 | * followed by \ref msgb_reserve in order to create a new \ref msgb with |
| 516 | * user-specified amount of headroom. |
| 517 | */ |
| 518 | static inline struct msgb *msgb_alloc_headroom_c(const void *ctx, int size, int headroom, |
| 519 | const char *name) |
| 520 | { |
| 521 | osmo_static_assert(size > headroom, headroom_bigger); |
| 522 | |
| 523 | struct msgb *msg = msgb_alloc_c(ctx, size, name); |
| 524 | if (msg) |
| 525 | msgb_reserve(msg, headroom); |
| 526 | return msg; |
| 527 | } |
| 528 | |
| 529 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 530 | /*! Allocate message buffer with specified headroom |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 531 | * \param[in] size size in bytes, including headroom |
| 532 | * \param[in] headroom headroom in bytes |
| 533 | * \param[in] name human-readable name |
| 534 | * \returns allocated message buffer with specified headroom |
| 535 | * |
| 536 | * This function is a convenience wrapper around \ref msgb_alloc |
| 537 | * followed by \ref msgb_reserve in order to create a new \ref msgb with |
| 538 | * user-specified amount of headroom. |
| 539 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 540 | static inline struct msgb *msgb_alloc_headroom(int size, int headroom, |
| 541 | const char *name) |
| 542 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 543 | osmo_static_assert(size > headroom, headroom_bigger); |
Holger Hans Peter Freyther | 5853f08 | 2011-01-16 17:38:22 +0100 | [diff] [blame] | 544 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 545 | struct msgb *msg = msgb_alloc(size, name); |
| 546 | if (msg) |
| 547 | msgb_reserve(msg, headroom); |
| 548 | return msg; |
| 549 | } |
| 550 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 551 | /*! Check a message buffer for consistency |
Jacob Erlbeck | 8db1134 | 2015-11-27 13:26:15 +0100 | [diff] [blame] | 552 | * \param[in] msg message buffer |
| 553 | * \returns 0 (false) if inconsistent, != 0 (true) otherwise |
| 554 | */ |
| 555 | static inline int msgb_test_invariant(const struct msgb *msg) |
| 556 | { |
| 557 | const unsigned char *lbound; |
| 558 | if (!msg || !msg->data || !msg->tail || |
| 559 | (msg->data + msg->len != msg->tail) || |
| 560 | (msg->data < msg->head) || |
| 561 | (msg->tail > msg->head + msg->data_len)) |
| 562 | return 0; |
| 563 | |
| 564 | lbound = msg->head; |
| 565 | |
| 566 | if (msg->l1h) { |
| 567 | if (msg->l1h < lbound) |
| 568 | return 0; |
| 569 | lbound = msg->l1h; |
| 570 | } |
| 571 | if (msg->l2h) { |
| 572 | if (msg->l2h < lbound) |
| 573 | return 0; |
| 574 | lbound = msg->l2h; |
| 575 | } |
| 576 | if (msg->l3h) { |
| 577 | if (msg->l3h < lbound) |
| 578 | return 0; |
| 579 | lbound = msg->l3h; |
| 580 | } |
| 581 | if (msg->l4h) { |
| 582 | if (msg->l4h < lbound) |
| 583 | return 0; |
| 584 | lbound = msg->l4h; |
| 585 | } |
| 586 | |
| 587 | return lbound <= msg->head + msg->data_len; |
| 588 | } |
| 589 | |
Max | db03825 | 2018-12-03 14:14:44 +0100 | [diff] [blame] | 590 | |
| 591 | /* msgb data comparison helpers */ |
| 592 | |
| 593 | /*! Compare: check data in msgb against given data |
| 594 | * \param[in] msg message buffer |
| 595 | * \param[in] data expected data |
| 596 | * \param[in] len length of data |
| 597 | * \returns boolean indicating whether msgb content is equal to the given data |
| 598 | */ |
| 599 | #define msgb_eq_data(msg, data, len) \ |
| 600 | _msgb_eq(__FILE__, __LINE__, __func__, 0, msg, data, len, false) |
| 601 | |
| 602 | /*! Compare: check L1 data in msgb against given data |
| 603 | * \param[in] msg message buffer |
| 604 | * \param[in] data expected L1 data |
| 605 | * \param[in] len length of data |
| 606 | * \returns boolean indicating whether msgb L1 content is equal to the given data |
| 607 | */ |
| 608 | #define msgb_eq_l1_data(msg, data, len) \ |
| 609 | _msgb_eq(__FILE__, __LINE__, __func__, 1, msg, data, len, false) |
| 610 | |
| 611 | /*! Compare: check L2 data in msgb against given data |
| 612 | * \param[in] msg message buffer |
| 613 | * \param[in] data expected L2 data |
| 614 | * \param[in] len length of data |
| 615 | * \returns boolean indicating whether msgb L2 content is equal to the given data |
| 616 | */ |
| 617 | #define msgb_eq_l2_data(msg, data, len) \ |
| 618 | _msgb_eq(__FILE__, __LINE__, __func__, 2, msg, data, len, false) |
| 619 | |
| 620 | /*! Compare: check L3 data in msgb against given data |
| 621 | * \param[in] msg message buffer |
| 622 | * \param[in] data expected L3 data |
| 623 | * \param[in] len length of data |
| 624 | * \returns boolean indicating whether msgb L3 content is equal to the given data |
| 625 | */ |
| 626 | #define msgb_eq_l3_data(msg, data, len) \ |
| 627 | _msgb_eq(__FILE__, __LINE__, __func__, 3, msg, data, len, false) |
| 628 | |
| 629 | /*! Compare: check L4 data in msgb against given data |
| 630 | * \param[in] msg message buffer |
| 631 | * \param[in] data expected L4 data |
| 632 | * \param[in] len length of data |
| 633 | * \returns boolean indicating whether msgb L4 content is equal to the given data |
| 634 | */ |
| 635 | #define msgb_eq_l4_data(msg, data, len) \ |
| 636 | _msgb_eq(__FILE__, __LINE__, __func__, 4, msg, data, len, false) |
| 637 | |
| 638 | |
| 639 | /* msgb test/debug helpers */ |
| 640 | |
| 641 | /*! Compare and print: check data in msgb against given data and print errors if any |
| 642 | * \param[in] msg message buffer |
| 643 | * \param[in] data expected data |
| 644 | * \param[in] len length of data |
| 645 | * \returns boolean indicating whether msgb content is equal to the given data |
| 646 | */ |
| 647 | #define msgb_eq_data_print(msg, data, len) \ |
| 648 | _msgb_eq(__FILE__, __LINE__, __func__, 0, msg, data, len, true) |
| 649 | |
| 650 | /*! Compare and print: check L1 data in msgb against given data and print errors if any |
| 651 | * \param[in] msg message buffer |
| 652 | * \param[in] data expected L1 data |
| 653 | * \param[in] len length of data |
| 654 | * \returns boolean indicating whether msgb L1 content is equal to the given data |
| 655 | */ |
| 656 | #define msgb_eq_l1_data_print(msg, data, len) \ |
| 657 | _msgb_eq(__FILE__, __LINE__, __func__, 1, msg, data, len, true) |
| 658 | |
| 659 | /*! Compare and print: check L2 data in msgb against given data and print errors if any |
| 660 | * \param[in] msg message buffer |
| 661 | * \param[in] data expected L2 data |
| 662 | * \param[in] len length of data |
| 663 | * \returns boolean indicating whether msgb L2 content is equal to the given data |
| 664 | */ |
| 665 | #define msgb_eq_l2_data_print(msg, data, len) \ |
| 666 | _msgb_eq(__FILE__, __LINE__, __func__, 2, msg, data, len, true) |
| 667 | |
| 668 | /*! Compare and print: check L3 data in msgb against given data and print errors if any |
| 669 | * \param[in] msg message buffer |
| 670 | * \param[in] data expected L3 data |
| 671 | * \param[in] len length of data |
| 672 | * \returns boolean indicating whether msgb L3 content is equal to the given data |
| 673 | */ |
| 674 | #define msgb_eq_l3_data_print(msg, data, len) \ |
| 675 | _msgb_eq(__FILE__, __LINE__, __func__, 3, msg, data, len, true) |
| 676 | |
| 677 | |
| 678 | /*! Compare and print: check L4 data in msgb against given data and print errors if any |
| 679 | * \param[in] msg message buffer |
| 680 | * \param[in] data expected L4 data |
| 681 | * \param[in] len length of data |
| 682 | * \returns boolean indicating whether msgb L4 content is equal to the given data |
| 683 | */ |
| 684 | #define msgb_eq_l4_data_print(msg, data, len) \ |
| 685 | _msgb_eq(__FILE__, __LINE__, __func__, 4, msg, data, len, true) |
| 686 | |
| 687 | bool _msgb_eq(const char *file, size_t line, const char *func, uint8_t level, |
| 688 | const struct msgb *msg, const uint8_t *data, size_t len, bool print); |
| 689 | |
| 690 | |
| 691 | /* msgb data comparison */ |
| 692 | |
| 693 | /*! Compare msgbs |
| 694 | * \param[in] msg1 message buffer |
| 695 | * \param[in] msg2 reference message buffer |
| 696 | * \returns boolean indicating whether msgb content is equal |
| 697 | */ |
Vadim Yanitskiy | ba426e3 | 2019-03-25 23:41:07 +0700 | [diff] [blame] | 698 | #define msgb_eq(msg1, msg2) msgb_eq_data(msg1, msgb_data(msg2), msgb_length(msg2)) |
Max | db03825 | 2018-12-03 14:14:44 +0100 | [diff] [blame] | 699 | |
| 700 | /*! Compare msgbs L1 content |
| 701 | * \param[in] msg1 message buffer |
| 702 | * \param[in] msg2 reference message buffer |
| 703 | * \returns boolean indicating whether msgb L1 content is equal |
| 704 | */ |
Vadim Yanitskiy | ba426e3 | 2019-03-25 23:41:07 +0700 | [diff] [blame] | 705 | #define msgb_eq_l1(msg1, msg2) msgb_eq_l1_data(msg1, msgb_l1(msg2), msgb_l1len(msg2)) |
Max | db03825 | 2018-12-03 14:14:44 +0100 | [diff] [blame] | 706 | |
| 707 | /*! Compare msgbs L2 content |
| 708 | * \param[in] msg1 message buffer |
| 709 | * \param[in] msg2 reference message buffer |
| 710 | * \returns boolean indicating whether msgb L2 content is equal |
| 711 | */ |
Vadim Yanitskiy | ba426e3 | 2019-03-25 23:41:07 +0700 | [diff] [blame] | 712 | #define msgb_eq_l2(msg1, msg2) msgb_eq_l2_data(msg1, msgb_l2(msg2), msgb_l2len(msg2)) |
Max | db03825 | 2018-12-03 14:14:44 +0100 | [diff] [blame] | 713 | |
| 714 | /*! Compare msgbs L3 content |
| 715 | * \param[in] msg1 message buffer |
| 716 | * \param[in] msg2 reference message buffer |
| 717 | * \returns boolean indicating whether msgb L3 content is equal |
| 718 | */ |
Vadim Yanitskiy | ba426e3 | 2019-03-25 23:41:07 +0700 | [diff] [blame] | 719 | #define msgb_eq_l3(msg1, msg2) msgb_eq_l3_data(msg1, msgb_l3(msg2), msgb_l3len(msg2)) |
Max | db03825 | 2018-12-03 14:14:44 +0100 | [diff] [blame] | 720 | |
| 721 | /*! Compare msgbs L4 content |
| 722 | * \param[in] msg1 message buffer |
| 723 | * \param[in] msg2 reference message buffer |
| 724 | * \returns boolean indicating whether msgb L4 content is equal |
| 725 | */ |
Vadim Yanitskiy | ba426e3 | 2019-03-25 23:41:07 +0700 | [diff] [blame] | 726 | #define msgb_eq_l4(msg1, msg2) msgb_eq_l4_data(msg1, msgb_l4(msg2), msgb_l4len(msg2)) |
Max | db03825 | 2018-12-03 14:14:44 +0100 | [diff] [blame] | 727 | |
| 728 | |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 729 | /* non inline functions to ease binding */ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 730 | |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 731 | uint8_t *msgb_data(const struct msgb *msg); |
Neels Hofmeyr | f45334b | 2016-09-16 00:15:56 +0200 | [diff] [blame] | 732 | |
| 733 | void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size); |
| 734 | void msgb_set_talloc_ctx(void *ctx) OSMO_DEPRECATED("Use msgb_talloc_ctx_init() instead"); |
Philipp Maier | c5b47cc | 2017-10-10 16:53:21 +0200 | [diff] [blame] | 735 | int msgb_printf(struct msgb *msgb, const char *format, ...); |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 736 | |
Max | cb02a39 | 2018-12-03 14:00:49 +0100 | [diff] [blame] | 737 | static inline const char *msgb_hexdump_l1(const struct msgb *msg) |
| 738 | { |
| 739 | if (!msgb_l1(msg) || !(msgb_l1len(msg))) |
| 740 | return "[]"; |
| 741 | return osmo_hexdump((const unsigned char *) msgb_l1(msg), msgb_l1len(msg)); |
| 742 | } |
| 743 | |
Harald Welte | 5721770 | 2018-02-09 02:01:48 +0100 | [diff] [blame] | 744 | static inline const char *msgb_hexdump_l2(const struct msgb *msg) |
| 745 | { |
| 746 | if (!msgb_l2(msg) || !(msgb_l2len(msg))) |
| 747 | return "[]"; |
Harald Welte | ebd2b0f | 2018-02-09 10:32:03 +0100 | [diff] [blame] | 748 | return osmo_hexdump((const unsigned char *) msgb_l2(msg), msgb_l2len(msg)); |
Harald Welte | 5721770 | 2018-02-09 02:01:48 +0100 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | static inline const char *msgb_hexdump_l3(const struct msgb *msg) |
| 752 | { |
| 753 | if (!msgb_l3(msg) || !(msgb_l3len(msg))) |
| 754 | return "[]"; |
Harald Welte | ebd2b0f | 2018-02-09 10:32:03 +0100 | [diff] [blame] | 755 | return osmo_hexdump((const unsigned char*) msgb_l3(msg), msgb_l3len(msg)); |
Harald Welte | 5721770 | 2018-02-09 02:01:48 +0100 | [diff] [blame] | 756 | } |
| 757 | |
Max | cb02a39 | 2018-12-03 14:00:49 +0100 | [diff] [blame] | 758 | static inline const char *msgb_hexdump_l4(const struct msgb *msg) |
| 759 | { |
| 760 | if (!msgb_l4(msg) || !(msgb_l4len(msg))) |
| 761 | return "[]"; |
| 762 | return osmo_hexdump((const unsigned char*) msgb_l4(msg), msgb_l4len(msg)); |
| 763 | } |
| 764 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 765 | /*! @} */ |