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> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 26 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 27 | /*! \defgroup msgb Message buffers |
| 28 | * @{ |
| 29 | */ |
| 30 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 31 | /*! \file msgb.h |
| 32 | * \brief Osmocom message buffers |
| 33 | * The Osmocom message buffers are modelled after the 'struct skb' |
| 34 | * inside the Linux kernel network stack. As they exist in userspace, |
| 35 | * they are much simplified. However, terminology such as headroom, |
| 36 | * tailroom, push/pull/put etc. remains the same. |
| 37 | */ |
| 38 | |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 39 | #define MSGB_DEBUG |
| 40 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 41 | /*! \brief Osmocom message buffer */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 42 | struct msgb { |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 43 | struct llist_head list; /*!< \brief linked list header */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 44 | |
Pablo Neira Ayuso | 29cbf61 | 2011-07-07 19:46:44 +0200 | [diff] [blame] | 45 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 46 | /* Part of which TRX logical channel we were received / transmitted */ |
Harald Welte | 074c9f9 | 2010-04-30 14:29:11 +0200 | [diff] [blame] | 47 | /* FIXME: move them into the control buffer */ |
Harald Welte | c5a0ded | 2011-07-18 16:59:27 +0200 | [diff] [blame] | 48 | union { |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 49 | void *dst; /*!< \brief reference of origin/destination */ |
Harald Welte | c5a0ded | 2011-07-18 16:59:27 +0200 | [diff] [blame] | 50 | struct gsm_bts_trx *trx; |
| 51 | }; |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 52 | struct gsm_lchan *lchan; /*!< \brief logical channel */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 53 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 54 | unsigned char *l1h; /*!< \brief pointer to Layer1 header (if any) */ |
| 55 | unsigned char *l2h; /*!< \brief pointer to A-bis layer 2 header: OML, RSL(RLL), NS */ |
| 56 | unsigned char *l3h; /*!< \brief pointer to Layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */ |
| 57 | unsigned char *l4h; /*!< \brief pointer to layer 4 header */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 58 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 59 | unsigned long cb[5]; /*!< \brief control buffer */ |
Harald Welte | 074c9f9 | 2010-04-30 14:29:11 +0200 | [diff] [blame] | 60 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 61 | uint16_t data_len; /*!< \brief length of underlying data array */ |
| 62 | uint16_t len; /*!< \brief length of bytes used in msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 63 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 64 | unsigned char *head; /*!< \brief start of underlying memory buffer */ |
| 65 | unsigned char *tail; /*!< \brief end of message in buffer */ |
| 66 | unsigned char *data; /*!< \brief start of message in buffer */ |
| 67 | unsigned char _data[0]; /*!< \brief optional immediate data array */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | extern struct msgb *msgb_alloc(uint16_t size, const char *name); |
| 71 | extern void msgb_free(struct msgb *m); |
| 72 | extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg); |
| 73 | extern struct msgb *msgb_dequeue(struct llist_head *queue); |
| 74 | extern void msgb_reset(struct msgb *m); |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 75 | uint16_t msgb_length(const struct msgb *msg); |
Jacob Erlbeck | baa225e | 2014-02-28 15:14:40 +0100 | [diff] [blame] | 76 | extern const char *msgb_hexdump(const struct msgb *msg); |
Jacob Erlbeck | cdd05f0 | 2015-11-27 13:26:13 +0100 | [diff] [blame] | 77 | extern int msgb_resize_area(struct msgb *msg, uint8_t *area, |
| 78 | int old_size, int new_size); |
| 79 | extern struct msgb *msgb_copy(const struct msgb *msg, const char *name); |
Jacob Erlbeck | 8db1134 | 2015-11-27 13:26:15 +0100 | [diff] [blame] | 80 | static int msgb_test_invariant(const struct msgb *msg) __attribute__((pure)); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 81 | |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 82 | #ifdef MSGB_DEBUG |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 83 | #include <osmocom/core/panic.h> |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 84 | #define MSGB_ABORT(msg, fmt, args ...) do { \ |
Harald Welte | dab0287 | 2010-11-09 13:42:26 +0100 | [diff] [blame] | 85 | osmo_panic("msgb(%p): " fmt, msg, ## args); \ |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 86 | } while(0) |
| 87 | #else |
| 88 | #define MSGB_ABORT(msg, fmt, args ...) |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 89 | #endif |
| 90 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 91 | /*! \brief obtain L1 header of msgb */ |
Harald Welte | fdd0a70 | 2010-03-01 22:30:51 +0100 | [diff] [blame] | 92 | #define msgb_l1(m) ((void *)(m->l1h)) |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 93 | /*! \brief obtain L2 header of msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 94 | #define msgb_l2(m) ((void *)(m->l2h)) |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 95 | /*! \brief obtain L3 header of msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 96 | #define msgb_l3(m) ((void *)(m->l3h)) |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 97 | /*! \brief obtain SMS header of msgb */ |
Harald Welte | bb77c9d | 2010-04-30 14:26:12 +0200 | [diff] [blame] | 98 | #define msgb_sms(m) ((void *)(m->l4h)) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 99 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 100 | /*! \brief determine length of L1 message |
| 101 | * \param[in] msgb message buffer |
| 102 | * \returns size of L1 message in bytes |
| 103 | * |
| 104 | * This function computes the number of bytes between the tail of the |
| 105 | * message and the layer 1 header. |
| 106 | */ |
Harald Welte | fdd0a70 | 2010-03-01 22:30:51 +0100 | [diff] [blame] | 107 | static inline unsigned int msgb_l1len(const struct msgb *msgb) |
| 108 | { |
| 109 | return msgb->tail - (uint8_t *)msgb_l1(msgb); |
| 110 | } |
| 111 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 112 | /*! \brief determine length of L2 message |
| 113 | * \param[in] msgb message buffer |
| 114 | * \returns size of L2 message in bytes |
| 115 | * |
| 116 | * This function computes the number of bytes between the tail of the |
| 117 | * message and the layer 2 header. |
| 118 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 119 | static inline unsigned int msgb_l2len(const struct msgb *msgb) |
| 120 | { |
| 121 | return msgb->tail - (uint8_t *)msgb_l2(msgb); |
| 122 | } |
| 123 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 124 | /*! \brief determine length of L3 message |
| 125 | * \param[in] msgb message buffer |
| 126 | * \returns size of L3 message in bytes |
| 127 | * |
| 128 | * This function computes the number of bytes between the tail of the |
| 129 | * message and the layer 3 header. |
| 130 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 131 | static inline unsigned int msgb_l3len(const struct msgb *msgb) |
| 132 | { |
| 133 | return msgb->tail - (uint8_t *)msgb_l3(msgb); |
| 134 | } |
| 135 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 136 | /*! \brief determine the length of the header |
| 137 | * \param[in] msgb message buffer |
| 138 | * \returns number of bytes between start of buffer and start of msg |
| 139 | * |
| 140 | * This function computes the length difference between the underlying |
| 141 | * data buffer and the used section of the \a msgb. |
| 142 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 143 | static inline unsigned int msgb_headlen(const struct msgb *msgb) |
| 144 | { |
| 145 | return msgb->len - msgb->data_len; |
| 146 | } |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 147 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 148 | /*! \brief determine how much tail room is left in msgb |
| 149 | * \param[in] msgb message buffer |
| 150 | * \returns number of bytes remaining at end of msgb |
| 151 | * |
| 152 | * This function computes the amount of octets left in the underlying |
| 153 | * data buffer after the end of the message. |
| 154 | */ |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 155 | static inline int msgb_tailroom(const struct msgb *msgb) |
| 156 | { |
| 157 | return (msgb->head + msgb->data_len) - msgb->tail; |
| 158 | } |
| 159 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 160 | /*! \brief determine the amount of headroom in msgb |
| 161 | * \param[in] msgb message buffer |
| 162 | * \returns number of bytes left ahead of message start in msgb |
| 163 | * |
| 164 | * This function computes the amount of bytes left in the underlying |
| 165 | * data buffer before the start of the actual message. |
| 166 | */ |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 167 | static inline int msgb_headroom(const struct msgb *msgb) |
| 168 | { |
| 169 | return (msgb->data - msgb->head); |
| 170 | } |
| 171 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 172 | /*! \brief append data to end of message buffer |
| 173 | * \param[in] msgb message buffer |
| 174 | * \param[in] len number of bytes to append to message |
| 175 | * \returns pointer to start of newly-appended data |
| 176 | * |
| 177 | * This function will move the \a tail pointer of the message buffer \a |
| 178 | * len bytes further, thus enlarging the message by \a len bytes. |
| 179 | * |
| 180 | * The return value is a pointer to start of the newly added section at |
| 181 | * the end of the message and can be used for actually filling/copying |
| 182 | * data into it. |
| 183 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 184 | static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len) |
| 185 | { |
| 186 | unsigned char *tmp = msgb->tail; |
Harald Welte | 46a45a6 | 2010-11-09 13:41:48 +0100 | [diff] [blame] | 187 | if (msgb_tailroom(msgb) < (int) len) |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 188 | MSGB_ABORT(msgb, "Not enough tailroom msgb_push (%u < %u)\n", |
| 189 | msgb_tailroom(msgb), len); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 190 | msgb->tail += len; |
| 191 | msgb->len += len; |
| 192 | return tmp; |
| 193 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 194 | |
| 195 | /*! \brief append a uint8 value to the end of the message |
| 196 | * \param[in] msgb message buffer |
| 197 | * \param[in] word unsigned 8bit byte to be appended |
| 198 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 199 | static inline void msgb_put_u8(struct msgb *msgb, uint8_t word) |
| 200 | { |
| 201 | uint8_t *space = msgb_put(msgb, 1); |
| 202 | space[0] = word & 0xFF; |
| 203 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 204 | |
| 205 | /*! \brief append a uint16 value to the end of the message |
| 206 | * \param[in] msgb message buffer |
| 207 | * \param[in] word unsigned 16bit byte to be appended |
| 208 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 209 | static inline void msgb_put_u16(struct msgb *msgb, uint16_t word) |
| 210 | { |
| 211 | uint8_t *space = msgb_put(msgb, 2); |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 212 | osmo_store16be(word, space); |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 213 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 214 | |
| 215 | /*! \brief append a uint32 value to the end of the message |
| 216 | * \param[in] msgb message buffer |
| 217 | * \param[in] word unsigned 32bit byte to be appended |
| 218 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 219 | static inline void msgb_put_u32(struct msgb *msgb, uint32_t word) |
| 220 | { |
| 221 | uint8_t *space = msgb_put(msgb, 4); |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 222 | osmo_store32be(word, space); |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 223 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 224 | |
| 225 | /*! \brief remove data from end of message |
| 226 | * \param[in] msgb message buffer |
| 227 | * \param[in] len number of bytes to remove from end |
| 228 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 229 | static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len) |
| 230 | { |
Harald Welte | cac3cd6 | 2012-09-10 20:58:20 +0200 | [diff] [blame] | 231 | unsigned char *tmp = msgb->tail - len; |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 232 | if (msgb_length(msgb) < len) |
| 233 | MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n", |
| 234 | len, msgb_length(msgb)); |
| 235 | msgb->tail -= len; |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 236 | msgb->len -= len; |
| 237 | return tmp; |
| 238 | } |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 239 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 240 | /*! \brief remove uint8 from end of message |
| 241 | * \param[in] msgb message buffer |
| 242 | * \returns 8bit value taken from end of msgb |
| 243 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 244 | static inline uint8_t msgb_get_u8(struct msgb *msgb) |
| 245 | { |
| 246 | uint8_t *space = msgb_get(msgb, 1); |
| 247 | return space[0]; |
| 248 | } |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 249 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 250 | /*! \brief remove uint16 from end of message |
| 251 | * \param[in] msgb message buffer |
| 252 | * \returns 16bit value taken from end of msgb |
| 253 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 254 | static inline uint16_t msgb_get_u16(struct msgb *msgb) |
| 255 | { |
| 256 | uint8_t *space = msgb_get(msgb, 2); |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 257 | return osmo_load16be(space); |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 258 | } |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 259 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 260 | /*! \brief remove uint32 from end of message |
| 261 | * \param[in] msgb message buffer |
| 262 | * \returns 32bit value taken from end of msgb |
| 263 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 264 | static inline uint32_t msgb_get_u32(struct msgb *msgb) |
| 265 | { |
| 266 | uint8_t *space = msgb_get(msgb, 4); |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 267 | return osmo_load32be(space); |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 268 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 269 | |
| 270 | /*! \brief prepend (push) some data to start of message |
| 271 | * \param[in] msgb message buffer |
| 272 | * \param[in] len number of bytes to pre-pend |
| 273 | * \returns pointer to newly added portion at start of \a msgb |
| 274 | * |
| 275 | * This function moves the \a data pointer of the \ref msgb further |
| 276 | * to the front (by \a len bytes), thereby enlarging the message by \a |
| 277 | * len bytes. |
| 278 | * |
| 279 | * The return value is a pointer to the newly added section in the |
| 280 | * beginning of the message. It can be used to fill/copy data into it. |
| 281 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 282 | static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len) |
| 283 | { |
Harald Welte | 46a45a6 | 2010-11-09 13:41:48 +0100 | [diff] [blame] | 284 | if (msgb_headroom(msgb) < (int) len) |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 285 | MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n", |
| 286 | msgb_headroom(msgb), len); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 287 | msgb->data -= len; |
| 288 | msgb->len += len; |
| 289 | return msgb->data; |
| 290 | } |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 291 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 292 | /*! \brief remove (pull) a header from the front of the message buffer |
| 293 | * \param[in] msgb message buffer |
| 294 | * \param[in] len number of octets to be pulled |
| 295 | * \returns pointer to new start of msgb |
| 296 | * |
| 297 | * This function moves the \a data pointer of the \ref msgb further back |
| 298 | * in the message, thereby shrinking the size of the message by \a len |
| 299 | * bytes. |
| 300 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 301 | static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len) |
| 302 | { |
| 303 | msgb->len -= len; |
| 304 | return msgb->data += len; |
| 305 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 306 | |
Jacob Erlbeck | 8dac415 | 2014-01-28 11:03:11 +0100 | [diff] [blame] | 307 | /*! \brief remove (pull) all headers in front of l3h from the message buffer. |
| 308 | * \param[in] msgb message buffer with a valid l3h |
| 309 | * \returns pointer to new start of msgb (l3h) |
| 310 | * |
| 311 | * This function moves the \a data pointer of the \ref msgb further back |
| 312 | * in the message, thereby shrinking the size of the message. |
| 313 | * l1h and l2h will be cleared. |
| 314 | */ |
| 315 | static inline unsigned char *msgb_pull_to_l3(struct msgb *msg) |
| 316 | { |
| 317 | unsigned char *ret = msgb_pull(msg, msg->l3h - msg->data); |
| 318 | msg->l1h = msg->l2h = NULL; |
| 319 | return ret; |
| 320 | } |
| 321 | |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 322 | /*! \brief remove uint8 from front of message |
| 323 | * \param[in] msgb message buffer |
| 324 | * \returns 8bit value taken from end of msgb |
| 325 | */ |
| 326 | static inline uint8_t msgb_pull_u8(struct msgb *msgb) |
| 327 | { |
Steve Markgraf | 5905d5b | 2012-11-14 19:36:00 +0100 | [diff] [blame] | 328 | uint8_t *space = msgb_pull(msgb, 1) - 1; |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 329 | return space[0]; |
| 330 | } |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 331 | |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 332 | /*! \brief remove uint16 from front of message |
| 333 | * \param[in] msgb message buffer |
| 334 | * \returns 16bit value taken from end of msgb |
| 335 | */ |
| 336 | static inline uint16_t msgb_pull_u16(struct msgb *msgb) |
| 337 | { |
Steve Markgraf | 5905d5b | 2012-11-14 19:36:00 +0100 | [diff] [blame] | 338 | uint8_t *space = msgb_pull(msgb, 2) - 2; |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 339 | return osmo_load16be(space); |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 340 | } |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 341 | |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 342 | /*! \brief remove uint32 from front of message |
| 343 | * \param[in] msgb message buffer |
| 344 | * \returns 32bit value taken from end of msgb |
| 345 | */ |
| 346 | static inline uint32_t msgb_pull_u32(struct msgb *msgb) |
| 347 | { |
Steve Markgraf | 5905d5b | 2012-11-14 19:36:00 +0100 | [diff] [blame] | 348 | uint8_t *space = msgb_pull(msgb, 4) - 4; |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 349 | return osmo_load32be(space); |
Harald Welte | 972b502 | 2012-09-08 19:58:59 +0200 | [diff] [blame] | 350 | } |
| 351 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 352 | /*! \brief Increase headroom of empty msgb, reducing the tailroom |
| 353 | * \param[in] msg message buffer |
| 354 | * \param[in] len amount of extra octets to be reserved as headroom |
| 355 | * |
| 356 | * This function reserves some memory at the beginning of the underlying |
| 357 | * data buffer. The idea is to reserve space in case further headers |
| 358 | * have to be pushed to the \ref msgb during further processing. |
| 359 | * |
| 360 | * Calling this function leads to undefined reusults if it is called on |
| 361 | * a non-empty \ref msgb. |
| 362 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 363 | static inline void msgb_reserve(struct msgb *msg, int len) |
| 364 | { |
| 365 | msg->data += len; |
| 366 | msg->tail += len; |
| 367 | } |
| 368 | |
Harald Welte | b0f9151 | 2012-01-14 21:53:41 +0100 | [diff] [blame] | 369 | /*! \brief Trim the msgb to a given absolute length |
| 370 | * \param[in] msg message buffer |
| 371 | * \param[in] len new total length of buffer |
| 372 | * \returns 0 in case of success, negative in case of error |
| 373 | */ |
| 374 | static inline int msgb_trim(struct msgb *msg, int len) |
| 375 | { |
Jacob Erlbeck | ff42b26 | 2015-11-27 13:26:19 +0100 | [diff] [blame] | 376 | if (len < 0) |
| 377 | MSGB_ABORT(msg, "Negative length is not allowed\n"); |
Harald Welte | 3068d57 | 2012-01-14 22:07:59 +0100 | [diff] [blame] | 378 | if (len > msg->data_len) |
Harald Welte | b0f9151 | 2012-01-14 21:53:41 +0100 | [diff] [blame] | 379 | return -1; |
| 380 | |
Harald Welte | 3068d57 | 2012-01-14 22:07:59 +0100 | [diff] [blame] | 381 | msg->len = len; |
| 382 | msg->tail = msg->data + len; |
Harald Welte | b0f9151 | 2012-01-14 21:53:41 +0100 | [diff] [blame] | 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | /*! \brief Trim the msgb to a given layer3 length |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 388 | * \param[in] msg message buffer |
Harald Welte | b0f9151 | 2012-01-14 21:53:41 +0100 | [diff] [blame] | 389 | * \param[in] l3len new layer3 length |
| 390 | * \returns 0 in case of success, negative in case of error |
| 391 | */ |
| 392 | static inline int msgb_l3trim(struct msgb *msg, int l3len) |
| 393 | { |
| 394 | return msgb_trim(msg, (msg->l3h - msg->data) + l3len); |
| 395 | } |
| 396 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 397 | /*! \brief Allocate message buffer with specified headroom |
| 398 | * \param[in] size size in bytes, including headroom |
| 399 | * \param[in] headroom headroom in bytes |
| 400 | * \param[in] name human-readable name |
| 401 | * \returns allocated message buffer with specified headroom |
| 402 | * |
| 403 | * This function is a convenience wrapper around \ref msgb_alloc |
| 404 | * followed by \ref msgb_reserve in order to create a new \ref msgb with |
| 405 | * user-specified amount of headroom. |
| 406 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 407 | static inline struct msgb *msgb_alloc_headroom(int size, int headroom, |
| 408 | const char *name) |
| 409 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 410 | osmo_static_assert(size > headroom, headroom_bigger); |
Holger Hans Peter Freyther | 5853f08 | 2011-01-16 17:38:22 +0100 | [diff] [blame] | 411 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 412 | struct msgb *msg = msgb_alloc(size, name); |
| 413 | if (msg) |
| 414 | msgb_reserve(msg, headroom); |
| 415 | return msg; |
| 416 | } |
| 417 | |
Jacob Erlbeck | 8db1134 | 2015-11-27 13:26:15 +0100 | [diff] [blame] | 418 | /*! \brief Check a message buffer for consistency |
| 419 | * \param[in] msg message buffer |
| 420 | * \returns 0 (false) if inconsistent, != 0 (true) otherwise |
| 421 | */ |
| 422 | static inline int msgb_test_invariant(const struct msgb *msg) |
| 423 | { |
| 424 | const unsigned char *lbound; |
| 425 | if (!msg || !msg->data || !msg->tail || |
| 426 | (msg->data + msg->len != msg->tail) || |
| 427 | (msg->data < msg->head) || |
| 428 | (msg->tail > msg->head + msg->data_len)) |
| 429 | return 0; |
| 430 | |
| 431 | lbound = msg->head; |
| 432 | |
| 433 | if (msg->l1h) { |
| 434 | if (msg->l1h < lbound) |
| 435 | return 0; |
| 436 | lbound = msg->l1h; |
| 437 | } |
| 438 | if (msg->l2h) { |
| 439 | if (msg->l2h < lbound) |
| 440 | return 0; |
| 441 | lbound = msg->l2h; |
| 442 | } |
| 443 | if (msg->l3h) { |
| 444 | if (msg->l3h < lbound) |
| 445 | return 0; |
| 446 | lbound = msg->l3h; |
| 447 | } |
| 448 | if (msg->l4h) { |
| 449 | if (msg->l4h < lbound) |
| 450 | return 0; |
| 451 | lbound = msg->l4h; |
| 452 | } |
| 453 | |
| 454 | return lbound <= msg->head + msg->data_len; |
| 455 | } |
| 456 | |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 457 | /* non inline functions to ease binding */ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 458 | |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 459 | uint8_t *msgb_data(const struct msgb *msg); |
Harald Welte | 9e1f060 | 2011-06-29 18:46:10 +0200 | [diff] [blame] | 460 | void msgb_set_talloc_ctx(void *ctx); |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 461 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 462 | /*! @} */ |