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