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