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 | |
| 63 | extern struct msgb *msgb_alloc(uint16_t size, const char *name); |
| 64 | extern void msgb_free(struct msgb *m); |
| 65 | extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg); |
| 66 | extern struct msgb *msgb_dequeue(struct llist_head *queue); |
| 67 | extern void msgb_reset(struct msgb *m); |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 68 | uint16_t msgb_length(const struct msgb *msg); |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 69 | extern const char *msgb_hexdump(const struct msgb *msg); |
Jacob Erlbeck | cdd05f0 | 2015-11-27 13:26:13 +0100 | [diff] [blame] | 70 | extern int msgb_resize_area(struct msgb *msg, uint8_t *area, |
| 71 | int old_size, int new_size); |
| 72 | extern struct msgb *msgb_copy(const struct msgb *msg, const char *name); |
Jacob Erlbeck | 8db1134 | 2015-11-27 13:26:15 +0100 | [diff] [blame] | 73 | static int msgb_test_invariant(const struct msgb *msg) __attribute__((pure)); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 74 | |
Neels Hofmeyr | 3fad5d7 | 2017-11-20 14:15:03 +0100 | [diff] [blame] | 75 | /*! Free all msgbs from a queue built with msgb_enqueue(). |
| 76 | * \param[in] queue list head of a msgb queue. |
| 77 | */ |
| 78 | static inline void msgb_queue_free(struct llist_head *queue) |
| 79 | { |
| 80 | struct msgb *msg; |
| 81 | while ((msg = msgb_dequeue(queue))) msgb_free(msg); |
| 82 | } |
| 83 | |
Pau Espin Pedrol | 8ce6f48 | 2018-08-17 10:33:57 +0200 | [diff] [blame] | 84 | /*! Enqueue message buffer to tail of a queue and increment queue size counter |
| 85 | * \param[in] queue linked list header of queue |
| 86 | * \param[in] msg message buffer to be added to the queue |
| 87 | * \param[in] count pointer to variable holding size of the queue |
| 88 | * |
| 89 | * The function will append the specified message buffer \a msg to the queue |
| 90 | * implemented by \ref llist_head \a queue using function \ref msgb_enqueue_count, |
| 91 | * then increment \a count |
| 92 | */ |
| 93 | static inline void msgb_enqueue_count(struct llist_head *queue, struct msgb *msg, |
| 94 | unsigned int *count) |
| 95 | { |
| 96 | msgb_enqueue(queue, msg); |
| 97 | (*count)++; |
| 98 | } |
| 99 | |
| 100 | /*! Dequeue message buffer from head of queue and decrement queue size counter |
| 101 | * \param[in] queue linked list header of queue |
| 102 | * \param[in] count pointer to variable holding size of the queue |
| 103 | * \returns message buffer (if any) or NULL if queue empty |
| 104 | * |
| 105 | * The function will remove the first message buffer from the queue |
| 106 | * implemented by \ref llist_head \a queue using function \ref msgb_enqueue_count, |
| 107 | * and decrement \a count, all if queue is not empty. |
| 108 | */ |
| 109 | static inline struct msgb *msgb_dequeue_count(struct llist_head *queue, |
| 110 | unsigned int *count) |
| 111 | { |
| 112 | struct msgb *msg = msgb_dequeue(queue); |
| 113 | if (msg) |
| 114 | (*count)--; |
| 115 | return msg; |
| 116 | } |
| 117 | |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 118 | #ifdef MSGB_DEBUG |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 119 | #include <osmocom/core/panic.h> |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 120 | #define MSGB_ABORT(msg, fmt, args ...) do { \ |
Harald Welte | dab0287 | 2010-11-09 13:42:26 +0100 | [diff] [blame] | 121 | osmo_panic("msgb(%p): " fmt, msg, ## args); \ |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 122 | } while(0) |
| 123 | #else |
| 124 | #define MSGB_ABORT(msg, fmt, args ...) |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 125 | #endif |
| 126 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 127 | /*! obtain L1 header of msgb */ |
Harald Welte | fdd0a70 | 2010-03-01 22:30:51 +0100 | [diff] [blame] | 128 | #define msgb_l1(m) ((void *)(m->l1h)) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 129 | /*! obtain L2 header of msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 130 | #define msgb_l2(m) ((void *)(m->l2h)) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 131 | /*! obtain L3 header of msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 132 | #define msgb_l3(m) ((void *)(m->l3h)) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 133 | /*! obtain SMS header of msgb */ |
Harald Welte | bb77c9d | 2010-04-30 14:26:12 +0200 | [diff] [blame] | 134 | #define msgb_sms(m) ((void *)(m->l4h)) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 135 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 136 | /*! determine length of L1 message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 137 | * \param[in] msgb message buffer |
| 138 | * \returns size of L1 message in bytes |
| 139 | * |
| 140 | * This function computes the number of bytes between the tail of the |
| 141 | * message and the layer 1 header. |
| 142 | */ |
Harald Welte | fdd0a70 | 2010-03-01 22:30:51 +0100 | [diff] [blame] | 143 | static inline unsigned int msgb_l1len(const struct msgb *msgb) |
| 144 | { |
| 145 | return msgb->tail - (uint8_t *)msgb_l1(msgb); |
| 146 | } |
| 147 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 148 | /*! determine length of L2 message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 149 | * \param[in] msgb message buffer |
| 150 | * \returns size of L2 message in bytes |
| 151 | * |
| 152 | * This function computes the number of bytes between the tail of the |
| 153 | * message and the layer 2 header. |
| 154 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 155 | static inline unsigned int msgb_l2len(const struct msgb *msgb) |
| 156 | { |
| 157 | return msgb->tail - (uint8_t *)msgb_l2(msgb); |
| 158 | } |
| 159 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 160 | /*! determine length of L3 message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 161 | * \param[in] msgb message buffer |
| 162 | * \returns size of L3 message in bytes |
| 163 | * |
| 164 | * This function computes the number of bytes between the tail of the |
| 165 | * message and the layer 3 header. |
| 166 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 167 | static inline unsigned int msgb_l3len(const struct msgb *msgb) |
| 168 | { |
| 169 | return msgb->tail - (uint8_t *)msgb_l3(msgb); |
| 170 | } |
| 171 | |
Vadim Yanitskiy | e521ede | 2018-11-07 03:53:03 +0700 | [diff] [blame] | 172 | /*! determine length of L4 message |
| 173 | * \param[in] msgb message buffer |
| 174 | * \returns size of L4 message in bytes |
| 175 | * |
| 176 | * This function computes the number of bytes between the tail of the |
| 177 | * message and the layer 4 header. |
| 178 | */ |
| 179 | static inline unsigned int msgb_l4len(const struct msgb *msgb) |
| 180 | { |
| 181 | return msgb->tail - (uint8_t *)msgb_sms(msgb); |
| 182 | } |
| 183 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 184 | /*! determine the length of the header |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 185 | * \param[in] msgb message buffer |
| 186 | * \returns number of bytes between start of buffer and start of msg |
| 187 | * |
| 188 | * This function computes the length difference between the underlying |
| 189 | * data buffer and the used section of the \a msgb. |
| 190 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 191 | static inline unsigned int msgb_headlen(const struct msgb *msgb) |
| 192 | { |
| 193 | return msgb->len - msgb->data_len; |
| 194 | } |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 195 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 196 | /*! determine how much tail room is left in msgb |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 197 | * \param[in] msgb message buffer |
| 198 | * \returns number of bytes remaining at end of msgb |
| 199 | * |
| 200 | * This function computes the amount of octets left in the underlying |
| 201 | * data buffer after the end of the message. |
| 202 | */ |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 203 | static inline int msgb_tailroom(const struct msgb *msgb) |
| 204 | { |
| 205 | return (msgb->head + msgb->data_len) - msgb->tail; |
| 206 | } |
| 207 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 208 | /*! determine the amount of headroom in msgb |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 209 | * \param[in] msgb message buffer |
| 210 | * \returns number of bytes left ahead of message start in msgb |
| 211 | * |
| 212 | * This function computes the amount of bytes left in the underlying |
| 213 | * data buffer before the start of the actual message. |
| 214 | */ |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 215 | static inline int msgb_headroom(const struct msgb *msgb) |
| 216 | { |
| 217 | return (msgb->data - msgb->head); |
| 218 | } |
| 219 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 220 | /*! append data to end of message buffer |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 221 | * \param[in] msgb message buffer |
| 222 | * \param[in] len number of bytes to append to message |
| 223 | * \returns pointer to start of newly-appended data |
| 224 | * |
| 225 | * This function will move the \a tail pointer of the message buffer \a |
| 226 | * len bytes further, thus enlarging the message by \a len bytes. |
| 227 | * |
| 228 | * The return value is a pointer to start of the newly added section at |
| 229 | * the end of the message and can be used for actually filling/copying |
| 230 | * data into it. |
| 231 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 232 | static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len) |
| 233 | { |
| 234 | unsigned char *tmp = msgb->tail; |
Harald Welte | 46a45a6 | 2010-11-09 13:41:48 +0100 | [diff] [blame] | 235 | if (msgb_tailroom(msgb) < (int) len) |
Neels Hofmeyr | 418ca58 | 2016-09-09 02:03:43 +0200 | [diff] [blame] | 236 | MSGB_ABORT(msgb, "Not enough tailroom msgb_put (%u < %u)\n", |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 237 | msgb_tailroom(msgb), len); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 238 | msgb->tail += len; |
| 239 | msgb->len += len; |
| 240 | return tmp; |
| 241 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 242 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 243 | /*! append a uint8 value to the end of the message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 244 | * \param[in] msgb message buffer |
| 245 | * \param[in] word unsigned 8bit byte to be appended |
| 246 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 247 | static inline void msgb_put_u8(struct msgb *msgb, uint8_t word) |
| 248 | { |
| 249 | uint8_t *space = msgb_put(msgb, 1); |
| 250 | space[0] = word & 0xFF; |
| 251 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 252 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 253 | /*! append a uint16 value to the end of the message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 254 | * \param[in] msgb message buffer |
| 255 | * \param[in] word unsigned 16bit byte to be appended |
| 256 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 257 | static inline void msgb_put_u16(struct msgb *msgb, uint16_t word) |
| 258 | { |
| 259 | uint8_t *space = msgb_put(msgb, 2); |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 260 | osmo_store16be(word, space); |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 261 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 262 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 263 | /*! append a uint32 value to the end of the message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 264 | * \param[in] msgb message buffer |
| 265 | * \param[in] word unsigned 32bit byte to be appended |
| 266 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 267 | static inline void msgb_put_u32(struct msgb *msgb, uint32_t word) |
| 268 | { |
| 269 | uint8_t *space = msgb_put(msgb, 4); |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 270 | osmo_store32be(word, space); |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 271 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 272 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 273 | /*! remove data from end of message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 274 | * \param[in] msgb message buffer |
| 275 | * \param[in] len number of bytes to remove from end |
| 276 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 277 | static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len) |
| 278 | { |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 279 | if (msgb_length(msgb) < len) |
| 280 | MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n", |
| 281 | len, msgb_length(msgb)); |
| 282 | msgb->tail -= len; |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 283 | msgb->len -= len; |
Pau Espin Pedrol | 9fa0912 | 2018-04-11 20:09:13 +0200 | [diff] [blame] | 284 | return msgb->tail; |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 285 | } |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 286 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 287 | /*! remove uint8 from end of message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 288 | * \param[in] msgb message buffer |
| 289 | * \returns 8bit value taken from end of msgb |
| 290 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 291 | static inline uint8_t msgb_get_u8(struct msgb *msgb) |
| 292 | { |
| 293 | uint8_t *space = msgb_get(msgb, 1); |
| 294 | return space[0]; |
| 295 | } |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 296 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 297 | /*! remove uint16 from end of message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 298 | * \param[in] msgb message buffer |
| 299 | * \returns 16bit value taken from end of msgb |
| 300 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 301 | static inline uint16_t msgb_get_u16(struct msgb *msgb) |
| 302 | { |
| 303 | uint8_t *space = msgb_get(msgb, 2); |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 304 | return osmo_load16be(space); |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 305 | } |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 306 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 307 | /*! remove uint32 from end of message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 308 | * \param[in] msgb message buffer |
| 309 | * \returns 32bit value taken from end of msgb |
| 310 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 311 | static inline uint32_t msgb_get_u32(struct msgb *msgb) |
| 312 | { |
| 313 | uint8_t *space = msgb_get(msgb, 4); |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 314 | return osmo_load32be(space); |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 315 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 316 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 317 | /*! prepend (push) some data to start of message |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 318 | * \param[in] msgb message buffer |
| 319 | * \param[in] len number of bytes to pre-pend |
| 320 | * \returns pointer to newly added portion at start of \a msgb |
| 321 | * |
| 322 | * This function moves the \a data pointer of the \ref msgb further |
| 323 | * to the front (by \a len bytes), thereby enlarging the message by \a |
| 324 | * len bytes. |
| 325 | * |
| 326 | * The return value is a pointer to the newly added section in the |
| 327 | * beginning of the message. It can be used to fill/copy data into it. |
| 328 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 329 | static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len) |
| 330 | { |
Harald Welte | 46a45a6 | 2010-11-09 13:41:48 +0100 | [diff] [blame] | 331 | if (msgb_headroom(msgb) < (int) len) |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 332 | MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n", |
| 333 | msgb_headroom(msgb), len); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 334 | msgb->data -= len; |
| 335 | msgb->len += len; |
| 336 | return msgb->data; |
| 337 | } |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 338 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 339 | /*! prepend a uint8 value to the head of the message |
Harald Welte | 8593f1f | 2016-11-11 19:26:51 +0100 | [diff] [blame] | 340 | * \param[in] msgb message buffer |
| 341 | * \param[in] word unsigned 8bit byte to be prepended |
| 342 | */ |
| 343 | static inline void msgb_push_u8(struct msgb *msg, uint8_t word) |
| 344 | { |
| 345 | uint8_t *space = msgb_push(msg, 1); |
| 346 | space[0] = word; |
| 347 | } |
| 348 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 349 | /*! prepend a uint16 value to the head of the message |
Harald Welte | 8593f1f | 2016-11-11 19:26:51 +0100 | [diff] [blame] | 350 | * \param[in] msgb message buffer |
| 351 | * \param[in] word unsigned 16bit byte to be prepended |
| 352 | */ |
| 353 | static inline void msgb_push_u16(struct msgb *msg, uint16_t word) |
| 354 | { |
| 355 | uint16_t *space = (uint16_t *) msgb_push(msg, 2); |
| 356 | osmo_store16be(word, space); |
| 357 | } |
| 358 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 359 | /*! prepend a uint32 value to the head of the message |
Harald Welte | 8593f1f | 2016-11-11 19:26:51 +0100 | [diff] [blame] | 360 | * \param[in] msgb message buffer |
| 361 | * \param[in] word unsigned 32bit byte to be prepended |
| 362 | */ |
| 363 | static inline void msgb_push_u32(struct msgb *msg, uint32_t word) |
| 364 | { |
| 365 | uint32_t *space = (uint32_t *) msgb_push(msg, 4); |
| 366 | osmo_store32be(word, space); |
| 367 | } |
| 368 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 369 | /*! remove (pull) a header from the front of the message buffer |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 370 | * \param[in] msgb message buffer |
| 371 | * \param[in] len number of octets to be pulled |
| 372 | * \returns pointer to new start of msgb |
| 373 | * |
| 374 | * This function moves the \a data pointer of the \ref msgb further back |
| 375 | * in the message, thereby shrinking the size of the message by \a len |
| 376 | * bytes. |
| 377 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 378 | static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len) |
| 379 | { |
Pau Espin Pedrol | c62fc2d | 2018-04-11 20:14:08 +0200 | [diff] [blame] | 380 | if (msgb_length(msgb) < len) |
| 381 | MSGB_ABORT(msgb, "msgb too small to pull %u (len %u)\n", |
| 382 | len, msgb_length(msgb)); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 383 | msgb->len -= len; |
| 384 | return msgb->data += len; |
| 385 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 386 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 387 | /*! remove (pull) all headers in front of l3h from the message buffer. |
Jacob Erlbeck | 8dac415 | 2014-01-28 11:03:11 +0100 | [diff] [blame] | 388 | * \param[in] msgb message buffer with a valid l3h |
| 389 | * \returns pointer to new start of msgb (l3h) |
| 390 | * |
| 391 | * This function moves the \a data pointer of the \ref msgb further back |
| 392 | * in the message, thereby shrinking the size of the message. |
| 393 | * l1h and l2h will be cleared. |
| 394 | */ |
| 395 | static inline unsigned char *msgb_pull_to_l3(struct msgb *msg) |
| 396 | { |
| 397 | unsigned char *ret = msgb_pull(msg, msg->l3h - msg->data); |
| 398 | msg->l1h = msg->l2h = NULL; |
| 399 | return ret; |
| 400 | } |
| 401 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 402 | /*! remove (pull) all headers in front of l2h from the message buffer. |
Harald Welte | 59a9ebf | 2017-04-15 19:04:34 +0200 | [diff] [blame] | 403 | * \param[in] msgb message buffer with a valid l2h |
| 404 | * \returns pointer to new start of msgb (l2h) |
| 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 will be cleared. |
| 409 | */ |
| 410 | static inline unsigned char *msgb_pull_to_l2(struct msgb *msg) |
| 411 | { |
| 412 | unsigned char *ret = msgb_pull(msg, msg->l2h - msg->data); |
| 413 | msg->l1h = NULL; |
| 414 | return ret; |
| 415 | } |
| 416 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 417 | /*! remove uint8 from front of message |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 418 | * \param[in] msgb message buffer |
| 419 | * \returns 8bit value taken from end of msgb |
| 420 | */ |
| 421 | static inline uint8_t msgb_pull_u8(struct msgb *msgb) |
| 422 | { |
Steve Markgraf | 5905d5b | 2012-11-14 19:36:00 +0100 | [diff] [blame] | 423 | uint8_t *space = msgb_pull(msgb, 1) - 1; |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 424 | return space[0]; |
| 425 | } |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 426 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 427 | /*! remove uint16 from front of message |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 428 | * \param[in] msgb message buffer |
| 429 | * \returns 16bit value taken from end of msgb |
| 430 | */ |
| 431 | static inline uint16_t msgb_pull_u16(struct msgb *msgb) |
| 432 | { |
Steve Markgraf | 5905d5b | 2012-11-14 19:36:00 +0100 | [diff] [blame] | 433 | uint8_t *space = msgb_pull(msgb, 2) - 2; |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 434 | return osmo_load16be(space); |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 435 | } |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 436 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 437 | /*! remove uint32 from front of message |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 438 | * \param[in] msgb message buffer |
| 439 | * \returns 32bit value taken from end of msgb |
| 440 | */ |
| 441 | static inline uint32_t msgb_pull_u32(struct msgb *msgb) |
| 442 | { |
Steve Markgraf | 5905d5b | 2012-11-14 19:36:00 +0100 | [diff] [blame] | 443 | uint8_t *space = msgb_pull(msgb, 4) - 4; |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 444 | return osmo_load32be(space); |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 445 | } |
| 446 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 447 | /*! Increase headroom of empty msgb, reducing the tailroom |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 448 | * \param[in] msg message buffer |
| 449 | * \param[in] len amount of extra octets to be reserved as headroom |
| 450 | * |
| 451 | * This function reserves some memory at the beginning of the underlying |
| 452 | * data buffer. The idea is to reserve space in case further headers |
| 453 | * have to be pushed to the \ref msgb during further processing. |
| 454 | * |
| 455 | * Calling this function leads to undefined reusults if it is called on |
| 456 | * a non-empty \ref msgb. |
| 457 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 458 | static inline void msgb_reserve(struct msgb *msg, int len) |
| 459 | { |
| 460 | msg->data += len; |
| 461 | msg->tail += len; |
| 462 | } |
| 463 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 464 | /*! Trim the msgb to a given absolute length |
Harald Welte | b0f9151 | 2012-01-14 21:53:41 +0100 | [diff] [blame] | 465 | * \param[in] msg message buffer |
| 466 | * \param[in] len new total length of buffer |
| 467 | * \returns 0 in case of success, negative in case of error |
| 468 | */ |
| 469 | static inline int msgb_trim(struct msgb *msg, int len) |
| 470 | { |
Jacob Erlbeck | ff42b26 | 2015-11-27 13:26:19 +0100 | [diff] [blame] | 471 | if (len < 0) |
| 472 | MSGB_ABORT(msg, "Negative length is not allowed\n"); |
Harald Welte | 3068d57 | 2012-01-14 22:07:59 +0100 | [diff] [blame] | 473 | if (len > msg->data_len) |
Harald Welte | b0f9151 | 2012-01-14 21:53:41 +0100 | [diff] [blame] | 474 | return -1; |
| 475 | |
Harald Welte | 3068d57 | 2012-01-14 22:07:59 +0100 | [diff] [blame] | 476 | msg->len = len; |
| 477 | msg->tail = msg->data + len; |
Harald Welte | b0f9151 | 2012-01-14 21:53:41 +0100 | [diff] [blame] | 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 482 | /*! Trim the msgb to a given layer3 length |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 483 | * \param[in] msg message buffer |
Harald Welte | b0f9151 | 2012-01-14 21:53:41 +0100 | [diff] [blame] | 484 | * \param[in] l3len new layer3 length |
| 485 | * \returns 0 in case of success, negative in case of error |
| 486 | */ |
| 487 | static inline int msgb_l3trim(struct msgb *msg, int l3len) |
| 488 | { |
| 489 | return msgb_trim(msg, (msg->l3h - msg->data) + l3len); |
| 490 | } |
| 491 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 492 | /*! Allocate message buffer with specified headroom |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 493 | * \param[in] size size in bytes, including headroom |
| 494 | * \param[in] headroom headroom in bytes |
| 495 | * \param[in] name human-readable name |
| 496 | * \returns allocated message buffer with specified headroom |
| 497 | * |
| 498 | * This function is a convenience wrapper around \ref msgb_alloc |
| 499 | * followed by \ref msgb_reserve in order to create a new \ref msgb with |
| 500 | * user-specified amount of headroom. |
| 501 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 502 | static inline struct msgb *msgb_alloc_headroom(int size, int headroom, |
| 503 | const char *name) |
| 504 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 505 | osmo_static_assert(size > headroom, headroom_bigger); |
Holger Hans Peter Freyther | 5853f08 | 2011-01-16 17:38:22 +0100 | [diff] [blame] | 506 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 507 | struct msgb *msg = msgb_alloc(size, name); |
| 508 | if (msg) |
| 509 | msgb_reserve(msg, headroom); |
| 510 | return msg; |
| 511 | } |
| 512 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 513 | /*! Check a message buffer for consistency |
Jacob Erlbeck | 8db1134 | 2015-11-27 13:26:15 +0100 | [diff] [blame] | 514 | * \param[in] msg message buffer |
| 515 | * \returns 0 (false) if inconsistent, != 0 (true) otherwise |
| 516 | */ |
| 517 | static inline int msgb_test_invariant(const struct msgb *msg) |
| 518 | { |
| 519 | const unsigned char *lbound; |
| 520 | if (!msg || !msg->data || !msg->tail || |
| 521 | (msg->data + msg->len != msg->tail) || |
| 522 | (msg->data < msg->head) || |
| 523 | (msg->tail > msg->head + msg->data_len)) |
| 524 | return 0; |
| 525 | |
| 526 | lbound = msg->head; |
| 527 | |
| 528 | if (msg->l1h) { |
| 529 | if (msg->l1h < lbound) |
| 530 | return 0; |
| 531 | lbound = msg->l1h; |
| 532 | } |
| 533 | if (msg->l2h) { |
| 534 | if (msg->l2h < lbound) |
| 535 | return 0; |
| 536 | lbound = msg->l2h; |
| 537 | } |
| 538 | if (msg->l3h) { |
| 539 | if (msg->l3h < lbound) |
| 540 | return 0; |
| 541 | lbound = msg->l3h; |
| 542 | } |
| 543 | if (msg->l4h) { |
| 544 | if (msg->l4h < lbound) |
| 545 | return 0; |
| 546 | lbound = msg->l4h; |
| 547 | } |
| 548 | |
| 549 | return lbound <= msg->head + msg->data_len; |
| 550 | } |
| 551 | |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 552 | /* non inline functions to ease binding */ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 553 | |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 554 | uint8_t *msgb_data(const struct msgb *msg); |
Neels Hofmeyr | f45334b | 2016-09-16 00:15:56 +0200 | [diff] [blame] | 555 | |
| 556 | void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size); |
| 557 | 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] | 558 | int msgb_printf(struct msgb *msgb, const char *format, ...); |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 559 | |
Harald Welte | 5721770 | 2018-02-09 02:01:48 +0100 | [diff] [blame] | 560 | static inline const char *msgb_hexdump_l2(const struct msgb *msg) |
| 561 | { |
| 562 | if (!msgb_l2(msg) || !(msgb_l2len(msg))) |
| 563 | return "[]"; |
Harald Welte | ebd2b0f | 2018-02-09 10:32:03 +0100 | [diff] [blame] | 564 | return osmo_hexdump((const unsigned char *) msgb_l2(msg), msgb_l2len(msg)); |
Harald Welte | 5721770 | 2018-02-09 02:01:48 +0100 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | static inline const char *msgb_hexdump_l3(const struct msgb *msg) |
| 568 | { |
| 569 | if (!msgb_l3(msg) || !(msgb_l3len(msg))) |
| 570 | return "[]"; |
Harald Welte | ebd2b0f | 2018-02-09 10:32:03 +0100 | [diff] [blame] | 571 | return osmo_hexdump((const unsigned char*) msgb_l3(msg), msgb_l3len(msg)); |
Harald Welte | 5721770 | 2018-02-09 02:01:48 +0100 | [diff] [blame] | 572 | } |
| 573 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 574 | /*! @} */ |