Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 1 | #ifndef _MSGB_H |
| 2 | #define _MSGB_H |
| 3 | |
| 4 | /* (C) 2008 by Harald Welte <laforge@gnumonks.org> |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <stdint.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 24 | #include <osmocom/core/linuxlist.h> |
| 25 | #include <osmocom/core/utils.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 26 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 27 | /*! \file msgb.h |
| 28 | * \brief Osmocom message buffers |
| 29 | * The Osmocom message buffers are modelled after the 'struct skb' |
| 30 | * inside the Linux kernel network stack. As they exist in userspace, |
| 31 | * they are much simplified. However, terminology such as headroom, |
| 32 | * tailroom, push/pull/put etc. remains the same. |
| 33 | */ |
| 34 | |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 35 | #define MSGB_DEBUG |
| 36 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 37 | /*! \brief Osmocom message buffer */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 38 | struct msgb { |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 39 | struct llist_head list; /*!< \brief linked list header */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 40 | |
Pablo Neira Ayuso | 29cbf61 | 2011-07-07 19:46:44 +0200 | [diff] [blame] | 41 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 42 | /* Part of which TRX logical channel we were received / transmitted */ |
Harald Welte | 074c9f9 | 2010-04-30 14:29:11 +0200 | [diff] [blame] | 43 | /* FIXME: move them into the control buffer */ |
Harald Welte | c5a0ded | 2011-07-18 16:59:27 +0200 | [diff] [blame] | 44 | union { |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 45 | void *dst; /*!< \brief reference of origin/destination */ |
Harald Welte | c5a0ded | 2011-07-18 16:59:27 +0200 | [diff] [blame] | 46 | struct gsm_bts_trx *trx; |
| 47 | }; |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 48 | struct gsm_lchan *lchan; /*!< \brief logical channel */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 49 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 50 | unsigned char *l1h; /*!< \brief pointer to Layer1 header (if any) */ |
| 51 | unsigned char *l2h; /*!< \brief pointer to A-bis layer 2 header: OML, RSL(RLL), NS */ |
| 52 | unsigned char *l3h; /*!< \brief pointer to Layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */ |
| 53 | unsigned char *l4h; /*!< \brief pointer to layer 4 header */ |
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 long cb[5]; /*!< \brief control buffer */ |
Harald Welte | 074c9f9 | 2010-04-30 14:29:11 +0200 | [diff] [blame] | 56 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 57 | uint16_t data_len; /*!< \brief length of underlying data array */ |
| 58 | uint16_t len; /*!< \brief length of bytes used in msgb */ |
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 char *head; /*!< \brief start of underlying memory buffer */ |
| 61 | unsigned char *tail; /*!< \brief end of message in buffer */ |
| 62 | unsigned char *data; /*!< \brief start of message in buffer */ |
| 63 | unsigned char _data[0]; /*!< \brief optional immediate data array */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 64 | }; |
| 65 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 66 | /*! \brief Allocate a new message buffer |
| 67 | * \param[in] size Length in octets, including headroom |
| 68 | * \param[in] name Human-readable name to be associated with msgb |
| 69 | * |
| 70 | * This function allocates a 'struct msgb' as well as the underlying |
| 71 | * memory buffer for the actual message data (size specified by \a size) |
| 72 | * using the talloc memory context previously set by \ref msgb_set_talloc_ctx |
| 73 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 74 | extern struct msgb *msgb_alloc(uint16_t size, const char *name); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 75 | |
| 76 | /*! \brief Release given message buffer |
| 77 | * \param[in] m Message buffer to be free'd |
| 78 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 79 | extern void msgb_free(struct msgb *m); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 80 | |
| 81 | /*! \brief Enqueue message buffer to tail of a queue |
| 82 | * \param[in] queue linked list header of queue |
| 83 | * \param[in] msgb message buffer to be added to the queue |
| 84 | * |
| 85 | * The function will append the specified message buffer \a msg to the |
| 86 | * queue implemented by \ref llist_head \a queue |
| 87 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 88 | extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 89 | |
| 90 | /*! \brief Dequeue message buffer from head of queue |
| 91 | * \param[in] queue linked list header of queue |
| 92 | * \returns message buffer (if any) or NULL if queue empty |
| 93 | * |
| 94 | * The function will remove the first message buffer from the queue |
| 95 | * implemented by 'ref llist_head \a queue. |
| 96 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 97 | extern struct msgb *msgb_dequeue(struct llist_head *queue); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 98 | |
| 99 | /*! \brief Re-set all message buffer pointers |
| 100 | * \param[in] m message buffer that is to be resetted |
| 101 | * |
| 102 | * This will re-set the various internal pointers into the underlying |
| 103 | * message buffer, i.e. remvoe all headroom and treat the msgb as |
| 104 | * completely empty. It also initializes the control buffer to zero. |
| 105 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 106 | extern void msgb_reset(struct msgb *m); |
| 107 | |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 108 | #ifdef MSGB_DEBUG |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 109 | #include <osmocom/core/panic.h> |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 110 | #define MSGB_ABORT(msg, fmt, args ...) do { \ |
Harald Welte | dab0287 | 2010-11-09 13:42:26 +0100 | [diff] [blame] | 111 | osmo_panic("msgb(%p): " fmt, msg, ## args); \ |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 112 | } while(0) |
| 113 | #else |
| 114 | #define MSGB_ABORT(msg, fmt, args ...) |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 115 | #endif |
| 116 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 117 | /*! \brief obtain L1 header of msgb */ |
Harald Welte | fdd0a70 | 2010-03-01 22:30:51 +0100 | [diff] [blame] | 118 | #define msgb_l1(m) ((void *)(m->l1h)) |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 119 | /*! \brief obtain L2 header of msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 120 | #define msgb_l2(m) ((void *)(m->l2h)) |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 121 | /*! \brief obtain L3 header of msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 122 | #define msgb_l3(m) ((void *)(m->l3h)) |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 123 | /*! \brief obtain SMS header of msgb */ |
Harald Welte | bb77c9d | 2010-04-30 14:26:12 +0200 | [diff] [blame] | 124 | #define msgb_sms(m) ((void *)(m->l4h)) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 125 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 126 | /*! \brief determine length of L1 message |
| 127 | * \param[in] msgb message buffer |
| 128 | * \returns size of L1 message in bytes |
| 129 | * |
| 130 | * This function computes the number of bytes between the tail of the |
| 131 | * message and the layer 1 header. |
| 132 | */ |
Harald Welte | fdd0a70 | 2010-03-01 22:30:51 +0100 | [diff] [blame] | 133 | static inline unsigned int msgb_l1len(const struct msgb *msgb) |
| 134 | { |
| 135 | return msgb->tail - (uint8_t *)msgb_l1(msgb); |
| 136 | } |
| 137 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 138 | /*! \brief determine length of L2 message |
| 139 | * \param[in] msgb message buffer |
| 140 | * \returns size of L2 message in bytes |
| 141 | * |
| 142 | * This function computes the number of bytes between the tail of the |
| 143 | * message and the layer 2 header. |
| 144 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 145 | static inline unsigned int msgb_l2len(const struct msgb *msgb) |
| 146 | { |
| 147 | return msgb->tail - (uint8_t *)msgb_l2(msgb); |
| 148 | } |
| 149 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 150 | /*! \brief determine length of L3 message |
| 151 | * \param[in] msgb message buffer |
| 152 | * \returns size of L3 message in bytes |
| 153 | * |
| 154 | * This function computes the number of bytes between the tail of the |
| 155 | * message and the layer 3 header. |
| 156 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 157 | static inline unsigned int msgb_l3len(const struct msgb *msgb) |
| 158 | { |
| 159 | return msgb->tail - (uint8_t *)msgb_l3(msgb); |
| 160 | } |
| 161 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 162 | /*! \brief determine the length of the header |
| 163 | * \param[in] msgb message buffer |
| 164 | * \returns number of bytes between start of buffer and start of msg |
| 165 | * |
| 166 | * This function computes the length difference between the underlying |
| 167 | * data buffer and the used section of the \a msgb. |
| 168 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 169 | static inline unsigned int msgb_headlen(const struct msgb *msgb) |
| 170 | { |
| 171 | return msgb->len - msgb->data_len; |
| 172 | } |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 173 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 174 | /*! \brief determine how much tail room is left in msgb |
| 175 | * \param[in] msgb message buffer |
| 176 | * \returns number of bytes remaining at end of msgb |
| 177 | * |
| 178 | * This function computes the amount of octets left in the underlying |
| 179 | * data buffer after the end of the message. |
| 180 | */ |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 181 | static inline int msgb_tailroom(const struct msgb *msgb) |
| 182 | { |
| 183 | return (msgb->head + msgb->data_len) - msgb->tail; |
| 184 | } |
| 185 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 186 | /*! \brief determine the amount of headroom in msgb |
| 187 | * \param[in] msgb message buffer |
| 188 | * \returns number of bytes left ahead of message start in msgb |
| 189 | * |
| 190 | * This function computes the amount of bytes left in the underlying |
| 191 | * data buffer before the start of the actual message. |
| 192 | */ |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 193 | static inline int msgb_headroom(const struct msgb *msgb) |
| 194 | { |
| 195 | return (msgb->data - msgb->head); |
| 196 | } |
| 197 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 198 | /*! \brief append data to end of message buffer |
| 199 | * \param[in] msgb message buffer |
| 200 | * \param[in] len number of bytes to append to message |
| 201 | * \returns pointer to start of newly-appended data |
| 202 | * |
| 203 | * This function will move the \a tail pointer of the message buffer \a |
| 204 | * len bytes further, thus enlarging the message by \a len bytes. |
| 205 | * |
| 206 | * The return value is a pointer to start of the newly added section at |
| 207 | * the end of the message and can be used for actually filling/copying |
| 208 | * data into it. |
| 209 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 210 | static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len) |
| 211 | { |
| 212 | unsigned char *tmp = msgb->tail; |
Harald Welte | 46a45a6 | 2010-11-09 13:41:48 +0100 | [diff] [blame] | 213 | if (msgb_tailroom(msgb) < (int) len) |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 214 | MSGB_ABORT(msgb, "Not enough tailroom msgb_push (%u < %u)\n", |
| 215 | msgb_tailroom(msgb), len); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 216 | msgb->tail += len; |
| 217 | msgb->len += len; |
| 218 | return tmp; |
| 219 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 220 | |
| 221 | /*! \brief append a uint8 value to the end of the message |
| 222 | * \param[in] msgb message buffer |
| 223 | * \param[in] word unsigned 8bit byte to be appended |
| 224 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 225 | static inline void msgb_put_u8(struct msgb *msgb, uint8_t word) |
| 226 | { |
| 227 | uint8_t *space = msgb_put(msgb, 1); |
| 228 | space[0] = word & 0xFF; |
| 229 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 230 | |
| 231 | /*! \brief append a uint16 value to the end of the message |
| 232 | * \param[in] msgb message buffer |
| 233 | * \param[in] word unsigned 16bit byte to be appended |
| 234 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 235 | static inline void msgb_put_u16(struct msgb *msgb, uint16_t word) |
| 236 | { |
| 237 | uint8_t *space = msgb_put(msgb, 2); |
| 238 | space[0] = word >> 8 & 0xFF; |
| 239 | space[1] = word & 0xFF; |
| 240 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 241 | |
| 242 | /*! \brief append a uint32 value to the end of the message |
| 243 | * \param[in] msgb message buffer |
| 244 | * \param[in] word unsigned 32bit byte to be appended |
| 245 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 246 | static inline void msgb_put_u32(struct msgb *msgb, uint32_t word) |
| 247 | { |
| 248 | uint8_t *space = msgb_put(msgb, 4); |
| 249 | space[0] = word >> 24 & 0xFF; |
| 250 | space[1] = word >> 16 & 0xFF; |
| 251 | space[2] = word >> 8 & 0xFF; |
| 252 | space[3] = word & 0xFF; |
| 253 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 254 | |
| 255 | /*! \brief remove data from end of message |
| 256 | * \param[in] msgb message buffer |
| 257 | * \param[in] len number of bytes to remove from end |
| 258 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 259 | static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len) |
| 260 | { |
| 261 | unsigned char *tmp = msgb->data; |
| 262 | msgb->data += len; |
| 263 | msgb->len -= len; |
| 264 | return tmp; |
| 265 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 266 | /*! \brief remove uint8 from end of message |
| 267 | * \param[in] msgb message buffer |
| 268 | * \returns 8bit value taken from end of msgb |
| 269 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 270 | static inline uint8_t msgb_get_u8(struct msgb *msgb) |
| 271 | { |
| 272 | uint8_t *space = msgb_get(msgb, 1); |
| 273 | return space[0]; |
| 274 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 275 | /*! \brief remove uint16 from end of message |
| 276 | * \param[in] msgb message buffer |
| 277 | * \returns 16bit value taken from end of msgb |
| 278 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 279 | static inline uint16_t msgb_get_u16(struct msgb *msgb) |
| 280 | { |
| 281 | uint8_t *space = msgb_get(msgb, 2); |
| 282 | return space[0] << 8 | space[1]; |
| 283 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 284 | /*! \brief remove uint32 from end of message |
| 285 | * \param[in] msgb message buffer |
| 286 | * \returns 32bit value taken from end of msgb |
| 287 | */ |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 288 | static inline uint32_t msgb_get_u32(struct msgb *msgb) |
| 289 | { |
| 290 | uint8_t *space = msgb_get(msgb, 4); |
| 291 | return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3]; |
| 292 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 293 | |
| 294 | /*! \brief prepend (push) some data to start of message |
| 295 | * \param[in] msgb message buffer |
| 296 | * \param[in] len number of bytes to pre-pend |
| 297 | * \returns pointer to newly added portion at start of \a msgb |
| 298 | * |
| 299 | * This function moves the \a data pointer of the \ref msgb further |
| 300 | * to the front (by \a len bytes), thereby enlarging the message by \a |
| 301 | * len bytes. |
| 302 | * |
| 303 | * The return value is a pointer to the newly added section in the |
| 304 | * beginning of the message. It can be used to fill/copy data into it. |
| 305 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 306 | static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len) |
| 307 | { |
Harald Welte | 46a45a6 | 2010-11-09 13:41:48 +0100 | [diff] [blame] | 308 | if (msgb_headroom(msgb) < (int) len) |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 309 | MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n", |
| 310 | msgb_headroom(msgb), len); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 311 | msgb->data -= len; |
| 312 | msgb->len += len; |
| 313 | return msgb->data; |
| 314 | } |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 315 | /*! \brief remove (pull) a header from the front of the message buffer |
| 316 | * \param[in] msgb message buffer |
| 317 | * \param[in] len number of octets to be pulled |
| 318 | * \returns pointer to new start of msgb |
| 319 | * |
| 320 | * This function moves the \a data pointer of the \ref msgb further back |
| 321 | * in the message, thereby shrinking the size of the message by \a len |
| 322 | * bytes. |
| 323 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 324 | static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len) |
| 325 | { |
| 326 | msgb->len -= len; |
| 327 | return msgb->data += len; |
| 328 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 329 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 330 | /*! \brief Increase headroom of empty msgb, reducing the tailroom |
| 331 | * \param[in] msg message buffer |
| 332 | * \param[in] len amount of extra octets to be reserved as headroom |
| 333 | * |
| 334 | * This function reserves some memory at the beginning of the underlying |
| 335 | * data buffer. The idea is to reserve space in case further headers |
| 336 | * have to be pushed to the \ref msgb during further processing. |
| 337 | * |
| 338 | * Calling this function leads to undefined reusults if it is called on |
| 339 | * a non-empty \ref msgb. |
| 340 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 341 | static inline void msgb_reserve(struct msgb *msg, int len) |
| 342 | { |
| 343 | msg->data += len; |
| 344 | msg->tail += len; |
| 345 | } |
| 346 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 347 | /*! \brief Allocate message buffer with specified headroom |
| 348 | * \param[in] size size in bytes, including headroom |
| 349 | * \param[in] headroom headroom in bytes |
| 350 | * \param[in] name human-readable name |
| 351 | * \returns allocated message buffer with specified headroom |
| 352 | * |
| 353 | * This function is a convenience wrapper around \ref msgb_alloc |
| 354 | * followed by \ref msgb_reserve in order to create a new \ref msgb with |
| 355 | * user-specified amount of headroom. |
| 356 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 357 | static inline struct msgb *msgb_alloc_headroom(int size, int headroom, |
| 358 | const char *name) |
| 359 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 360 | osmo_static_assert(size > headroom, headroom_bigger); |
Holger Hans Peter Freyther | 5853f08 | 2011-01-16 17:38:22 +0100 | [diff] [blame] | 361 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 362 | struct msgb *msg = msgb_alloc(size, name); |
| 363 | if (msg) |
| 364 | msgb_reserve(msg, headroom); |
| 365 | return msg; |
| 366 | } |
| 367 | |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 368 | /* non inline functions to ease binding */ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 369 | |
| 370 | /*! \brief get pointer to data section of message buffer |
| 371 | * \param[in] msg message buffer |
| 372 | * \returns pointer to data section of message buffer |
| 373 | */ |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 374 | uint8_t *msgb_data(const struct msgb *msg); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 375 | |
| 376 | /*! \brief get length of message buffer |
| 377 | * \param[in] msg message buffer |
| 378 | * \returns length of data section in message buffer |
| 379 | */ |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 380 | uint16_t msgb_length(const struct msgb *msg); |
| 381 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame^] | 382 | /*! \brief Set the talloc context for \ref msgb_alloc |
| 383 | * \param[in] ctx talloc context to be used as root for msgb allocations |
| 384 | */ |
Harald Welte | 9e1f060 | 2011-06-29 18:46:10 +0200 | [diff] [blame] | 385 | void msgb_set_talloc_ctx(void *ctx); |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 386 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 387 | #endif /* _MSGB_H */ |