blob: 7fad943c5f7bcc0e641a3012fed9e4cc532df881 [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +01002
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 Ayuso83419342011-03-22 16:36:13 +010023#include <osmocom/core/linuxlist.h>
24#include <osmocom/core/utils.h>
Max53777012014-06-04 19:07:41 +020025#include <osmocom/core/bits.h>
Neels Hofmeyrf45334b2016-09-16 00:15:56 +020026#include <osmocom/core/defs.h>
Harald Welteec8b4502010-02-20 20:34:29 +010027
Harald Welteba6988b2011-08-17 12:46:48 +020028/*! \defgroup msgb Message buffers
29 * @{
30 */
31
Harald Welte96e2a002017-06-12 21:44:18 +020032/*! \file msgb.h */
Harald Weltebd598e32011-08-16 23:26:52 +020033
Harald Welte652a7232010-07-22 21:55:24 +020034#define MSGB_DEBUG
35
Neels Hofmeyr87e45502017-06-20 00:17:59 +020036/*! Osmocom message buffer */
Harald Welteec8b4502010-02-20 20:34:29 +010037struct msgb {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020038 struct llist_head list; /*!< linked list header */
Harald Welteec8b4502010-02-20 20:34:29 +010039
Pablo Neira Ayuso29cbf612011-07-07 19:46:44 +020040
Harald Welteec8b4502010-02-20 20:34:29 +010041 /* Part of which TRX logical channel we were received / transmitted */
Harald Welte074c9f92010-04-30 14:29:11 +020042 /* FIXME: move them into the control buffer */
Harald Weltec5a0ded2011-07-18 16:59:27 +020043 union {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020044 void *dst; /*!< reference of origin/destination */
Harald Weltec5a0ded2011-07-18 16:59:27 +020045 struct gsm_bts_trx *trx;
46 };
Neels Hofmeyr87e45502017-06-20 00:17:59 +020047 struct gsm_lchan *lchan; /*!< logical channel */
Harald Welteec8b4502010-02-20 20:34:29 +010048
Neels Hofmeyr87e45502017-06-20 00:17:59 +020049 unsigned char *l1h; /*!< pointer to Layer1 header (if any) */
50 unsigned char *l2h; /*!< pointer to A-bis layer 2 header: OML, RSL(RLL), NS */
51 unsigned char *l3h; /*!< pointer to Layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */
52 unsigned char *l4h; /*!< pointer to layer 4 header */
Harald Welteec8b4502010-02-20 20:34:29 +010053
Neels Hofmeyr87e45502017-06-20 00:17:59 +020054 unsigned long cb[5]; /*!< control buffer */
Harald Welte074c9f92010-04-30 14:29:11 +020055
Neels Hofmeyr87e45502017-06-20 00:17:59 +020056 uint16_t data_len; /*!< length of underlying data array */
57 uint16_t len; /*!< length of bytes used in msgb */
Harald Welteec8b4502010-02-20 20:34:29 +010058
Neels Hofmeyr87e45502017-06-20 00:17:59 +020059 unsigned char *head; /*!< start of underlying memory buffer */
60 unsigned char *tail; /*!< end of message in buffer */
61 unsigned char *data; /*!< start of message in buffer */
62 unsigned char _data[0]; /*!< optional immediate data array */
Harald Welteec8b4502010-02-20 20:34:29 +010063};
64
65extern struct msgb *msgb_alloc(uint16_t size, const char *name);
66extern void msgb_free(struct msgb *m);
67extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
68extern struct msgb *msgb_dequeue(struct llist_head *queue);
69extern void msgb_reset(struct msgb *m);
Harald Welte972b5022012-09-08 19:58:59 +020070uint16_t msgb_length(const struct msgb *msg);
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +010071extern const char *msgb_hexdump(const struct msgb *msg);
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +010072extern int msgb_resize_area(struct msgb *msg, uint8_t *area,
73 int old_size, int new_size);
74extern struct msgb *msgb_copy(const struct msgb *msg, const char *name);
Jacob Erlbeck8db11342015-11-27 13:26:15 +010075static int msgb_test_invariant(const struct msgb *msg) __attribute__((pure));
Harald Welteec8b4502010-02-20 20:34:29 +010076
Harald Welte652a7232010-07-22 21:55:24 +020077#ifdef MSGB_DEBUG
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010078#include <osmocom/core/panic.h>
Harald Welte929d8872010-11-05 07:47:41 +010079#define MSGB_ABORT(msg, fmt, args ...) do { \
Harald Weltedab02872010-11-09 13:42:26 +010080 osmo_panic("msgb(%p): " fmt, msg, ## args); \
Harald Welte929d8872010-11-05 07:47:41 +010081 } while(0)
82#else
83#define MSGB_ABORT(msg, fmt, args ...)
Harald Welte652a7232010-07-22 21:55:24 +020084#endif
85
Neels Hofmeyr87e45502017-06-20 00:17:59 +020086/*! obtain L1 header of msgb */
Harald Weltefdd0a702010-03-01 22:30:51 +010087#define msgb_l1(m) ((void *)(m->l1h))
Neels Hofmeyr87e45502017-06-20 00:17:59 +020088/*! obtain L2 header of msgb */
Harald Welteec8b4502010-02-20 20:34:29 +010089#define msgb_l2(m) ((void *)(m->l2h))
Neels Hofmeyr87e45502017-06-20 00:17:59 +020090/*! obtain L3 header of msgb */
Harald Welteec8b4502010-02-20 20:34:29 +010091#define msgb_l3(m) ((void *)(m->l3h))
Neels Hofmeyr87e45502017-06-20 00:17:59 +020092/*! obtain SMS header of msgb */
Harald Weltebb77c9d2010-04-30 14:26:12 +020093#define msgb_sms(m) ((void *)(m->l4h))
Harald Welteec8b4502010-02-20 20:34:29 +010094
Neels Hofmeyr87e45502017-06-20 00:17:59 +020095/*! determine length of L1 message
Harald Weltebd598e32011-08-16 23:26:52 +020096 * \param[in] msgb message buffer
97 * \returns size of L1 message in bytes
98 *
99 * This function computes the number of bytes between the tail of the
100 * message and the layer 1 header.
101 */
Harald Weltefdd0a702010-03-01 22:30:51 +0100102static inline unsigned int msgb_l1len(const struct msgb *msgb)
103{
104 return msgb->tail - (uint8_t *)msgb_l1(msgb);
105}
106
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200107/*! determine length of L2 message
Harald Weltebd598e32011-08-16 23:26:52 +0200108 * \param[in] msgb message buffer
109 * \returns size of L2 message in bytes
110 *
111 * This function computes the number of bytes between the tail of the
112 * message and the layer 2 header.
113 */
Harald Welteec8b4502010-02-20 20:34:29 +0100114static inline unsigned int msgb_l2len(const struct msgb *msgb)
115{
116 return msgb->tail - (uint8_t *)msgb_l2(msgb);
117}
118
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200119/*! determine length of L3 message
Harald Weltebd598e32011-08-16 23:26:52 +0200120 * \param[in] msgb message buffer
121 * \returns size of L3 message in bytes
122 *
123 * This function computes the number of bytes between the tail of the
124 * message and the layer 3 header.
125 */
Harald Welteec8b4502010-02-20 20:34:29 +0100126static inline unsigned int msgb_l3len(const struct msgb *msgb)
127{
128 return msgb->tail - (uint8_t *)msgb_l3(msgb);
129}
130
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200131/*! determine the length of the header
Harald Weltebd598e32011-08-16 23:26:52 +0200132 * \param[in] msgb message buffer
133 * \returns number of bytes between start of buffer and start of msg
134 *
135 * This function computes the length difference between the underlying
136 * data buffer and the used section of the \a msgb.
137 */
Harald Welteec8b4502010-02-20 20:34:29 +0100138static inline unsigned int msgb_headlen(const struct msgb *msgb)
139{
140 return msgb->len - msgb->data_len;
141}
Harald Welte652a7232010-07-22 21:55:24 +0200142
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200143/*! determine how much tail room is left in msgb
Harald Weltebd598e32011-08-16 23:26:52 +0200144 * \param[in] msgb message buffer
145 * \returns number of bytes remaining at end of msgb
146 *
147 * This function computes the amount of octets left in the underlying
148 * data buffer after the end of the message.
149 */
Harald Welte652a7232010-07-22 21:55:24 +0200150static inline int msgb_tailroom(const struct msgb *msgb)
151{
152 return (msgb->head + msgb->data_len) - msgb->tail;
153}
154
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200155/*! determine the amount of headroom in msgb
Harald Weltebd598e32011-08-16 23:26:52 +0200156 * \param[in] msgb message buffer
157 * \returns number of bytes left ahead of message start in msgb
158 *
159 * This function computes the amount of bytes left in the underlying
160 * data buffer before the start of the actual message.
161 */
Harald Welte652a7232010-07-22 21:55:24 +0200162static inline int msgb_headroom(const struct msgb *msgb)
163{
164 return (msgb->data - msgb->head);
165}
166
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200167/*! append data to end of message buffer
Harald Weltebd598e32011-08-16 23:26:52 +0200168 * \param[in] msgb message buffer
169 * \param[in] len number of bytes to append to message
170 * \returns pointer to start of newly-appended data
171 *
172 * This function will move the \a tail pointer of the message buffer \a
173 * len bytes further, thus enlarging the message by \a len bytes.
174 *
175 * The return value is a pointer to start of the newly added section at
176 * the end of the message and can be used for actually filling/copying
177 * data into it.
178 */
Harald Welteec8b4502010-02-20 20:34:29 +0100179static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
180{
181 unsigned char *tmp = msgb->tail;
Harald Welte46a45a62010-11-09 13:41:48 +0100182 if (msgb_tailroom(msgb) < (int) len)
Neels Hofmeyr418ca582016-09-09 02:03:43 +0200183 MSGB_ABORT(msgb, "Not enough tailroom msgb_put (%u < %u)\n",
Harald Welte929d8872010-11-05 07:47:41 +0100184 msgb_tailroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100185 msgb->tail += len;
186 msgb->len += len;
187 return tmp;
188}
Harald Weltebd598e32011-08-16 23:26:52 +0200189
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200190/*! append a uint8 value to the end of the message
Harald Weltebd598e32011-08-16 23:26:52 +0200191 * \param[in] msgb message buffer
192 * \param[in] word unsigned 8bit byte to be appended
193 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100194static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
195{
196 uint8_t *space = msgb_put(msgb, 1);
197 space[0] = word & 0xFF;
198}
Harald Weltebd598e32011-08-16 23:26:52 +0200199
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200200/*! append a uint16 value to the end of the message
Harald Weltebd598e32011-08-16 23:26:52 +0200201 * \param[in] msgb message buffer
202 * \param[in] word unsigned 16bit byte to be appended
203 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100204static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
205{
206 uint8_t *space = msgb_put(msgb, 2);
Max53777012014-06-04 19:07:41 +0200207 osmo_store16be(word, space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100208}
Harald Weltebd598e32011-08-16 23:26:52 +0200209
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200210/*! append a uint32 value to the end of the message
Harald Weltebd598e32011-08-16 23:26:52 +0200211 * \param[in] msgb message buffer
212 * \param[in] word unsigned 32bit byte to be appended
213 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100214static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
215{
216 uint8_t *space = msgb_put(msgb, 4);
Max53777012014-06-04 19:07:41 +0200217 osmo_store32be(word, space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100218}
Harald Weltebd598e32011-08-16 23:26:52 +0200219
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200220/*! remove data from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200221 * \param[in] msgb message buffer
222 * \param[in] len number of bytes to remove from end
223 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100224static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
225{
Harald Weltecac3cd62012-09-10 20:58:20 +0200226 unsigned char *tmp = msgb->tail - len;
Harald Welte972b5022012-09-08 19:58:59 +0200227 if (msgb_length(msgb) < len)
228 MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n",
229 len, msgb_length(msgb));
230 msgb->tail -= len;
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100231 msgb->len -= len;
232 return tmp;
233}
Max53777012014-06-04 19:07:41 +0200234
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200235/*! remove uint8 from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200236 * \param[in] msgb message buffer
237 * \returns 8bit value taken from end of msgb
238 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100239static inline uint8_t msgb_get_u8(struct msgb *msgb)
240{
241 uint8_t *space = msgb_get(msgb, 1);
242 return space[0];
243}
Max53777012014-06-04 19:07:41 +0200244
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200245/*! remove uint16 from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200246 * \param[in] msgb message buffer
247 * \returns 16bit value taken from end of msgb
248 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100249static inline uint16_t msgb_get_u16(struct msgb *msgb)
250{
251 uint8_t *space = msgb_get(msgb, 2);
Max53777012014-06-04 19:07:41 +0200252 return osmo_load16be(space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100253}
Max53777012014-06-04 19:07:41 +0200254
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200255/*! remove uint32 from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200256 * \param[in] msgb message buffer
257 * \returns 32bit value taken from end of msgb
258 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100259static inline uint32_t msgb_get_u32(struct msgb *msgb)
260{
261 uint8_t *space = msgb_get(msgb, 4);
Max53777012014-06-04 19:07:41 +0200262 return osmo_load32be(space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100263}
Harald Weltebd598e32011-08-16 23:26:52 +0200264
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200265/*! prepend (push) some data to start of message
Harald Weltebd598e32011-08-16 23:26:52 +0200266 * \param[in] msgb message buffer
267 * \param[in] len number of bytes to pre-pend
268 * \returns pointer to newly added portion at start of \a msgb
269 *
270 * This function moves the \a data pointer of the \ref msgb further
271 * to the front (by \a len bytes), thereby enlarging the message by \a
272 * len bytes.
273 *
274 * The return value is a pointer to the newly added section in the
275 * beginning of the message. It can be used to fill/copy data into it.
276 */
Harald Welteec8b4502010-02-20 20:34:29 +0100277static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
278{
Harald Welte46a45a62010-11-09 13:41:48 +0100279 if (msgb_headroom(msgb) < (int) len)
Harald Welte929d8872010-11-05 07:47:41 +0100280 MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
281 msgb_headroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100282 msgb->data -= len;
283 msgb->len += len;
284 return msgb->data;
285}
Max53777012014-06-04 19:07:41 +0200286
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200287/*! prepend a uint8 value to the head of the message
Harald Welte8593f1f2016-11-11 19:26:51 +0100288 * \param[in] msgb message buffer
289 * \param[in] word unsigned 8bit byte to be prepended
290 */
291static inline void msgb_push_u8(struct msgb *msg, uint8_t word)
292{
293 uint8_t *space = msgb_push(msg, 1);
294 space[0] = word;
295}
296
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200297/*! prepend a uint16 value to the head of the message
Harald Welte8593f1f2016-11-11 19:26:51 +0100298 * \param[in] msgb message buffer
299 * \param[in] word unsigned 16bit byte to be prepended
300 */
301static inline void msgb_push_u16(struct msgb *msg, uint16_t word)
302{
303 uint16_t *space = (uint16_t *) msgb_push(msg, 2);
304 osmo_store16be(word, space);
305}
306
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200307/*! prepend a uint32 value to the head of the message
Harald Welte8593f1f2016-11-11 19:26:51 +0100308 * \param[in] msgb message buffer
309 * \param[in] word unsigned 32bit byte to be prepended
310 */
311static inline void msgb_push_u32(struct msgb *msg, uint32_t word)
312{
313 uint32_t *space = (uint32_t *) msgb_push(msg, 4);
314 osmo_store32be(word, space);
315}
316
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200317/*! remove (pull) a header from the front of the message buffer
Harald Weltebd598e32011-08-16 23:26:52 +0200318 * \param[in] msgb message buffer
319 * \param[in] len number of octets to be pulled
320 * \returns pointer to new start of msgb
321 *
322 * This function moves the \a data pointer of the \ref msgb further back
323 * in the message, thereby shrinking the size of the message by \a len
324 * bytes.
325 */
Harald Welteec8b4502010-02-20 20:34:29 +0100326static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
327{
328 msgb->len -= len;
329 return msgb->data += len;
330}
Harald Welteec8b4502010-02-20 20:34:29 +0100331
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200332/*! remove (pull) all headers in front of l3h from the message buffer.
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100333 * \param[in] msgb message buffer with a valid l3h
334 * \returns pointer to new start of msgb (l3h)
335 *
336 * This function moves the \a data pointer of the \ref msgb further back
337 * in the message, thereby shrinking the size of the message.
338 * l1h and l2h will be cleared.
339 */
340static inline unsigned char *msgb_pull_to_l3(struct msgb *msg)
341{
342 unsigned char *ret = msgb_pull(msg, msg->l3h - msg->data);
343 msg->l1h = msg->l2h = NULL;
344 return ret;
345}
346
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200347/*! remove (pull) all headers in front of l2h from the message buffer.
Harald Welte59a9ebf2017-04-15 19:04:34 +0200348 * \param[in] msgb message buffer with a valid l2h
349 * \returns pointer to new start of msgb (l2h)
350 *
351 * This function moves the \a data pointer of the \ref msgb further back
352 * in the message, thereby shrinking the size of the message.
353 * l1h will be cleared.
354 */
355static inline unsigned char *msgb_pull_to_l2(struct msgb *msg)
356{
357 unsigned char *ret = msgb_pull(msg, msg->l2h - msg->data);
358 msg->l1h = NULL;
359 return ret;
360}
361
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200362/*! remove uint8 from front of message
Harald Welte972b5022012-09-08 19:58:59 +0200363 * \param[in] msgb message buffer
364 * \returns 8bit value taken from end of msgb
365 */
366static inline uint8_t msgb_pull_u8(struct msgb *msgb)
367{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100368 uint8_t *space = msgb_pull(msgb, 1) - 1;
Harald Welte972b5022012-09-08 19:58:59 +0200369 return space[0];
370}
Max53777012014-06-04 19:07:41 +0200371
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200372/*! remove uint16 from front of message
Harald Welte972b5022012-09-08 19:58:59 +0200373 * \param[in] msgb message buffer
374 * \returns 16bit value taken from end of msgb
375 */
376static inline uint16_t msgb_pull_u16(struct msgb *msgb)
377{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100378 uint8_t *space = msgb_pull(msgb, 2) - 2;
Max53777012014-06-04 19:07:41 +0200379 return osmo_load16be(space);
Harald Welte972b5022012-09-08 19:58:59 +0200380}
Max53777012014-06-04 19:07:41 +0200381
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200382/*! remove uint32 from front of message
Harald Welte972b5022012-09-08 19:58:59 +0200383 * \param[in] msgb message buffer
384 * \returns 32bit value taken from end of msgb
385 */
386static inline uint32_t msgb_pull_u32(struct msgb *msgb)
387{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100388 uint8_t *space = msgb_pull(msgb, 4) - 4;
Max53777012014-06-04 19:07:41 +0200389 return osmo_load32be(space);
Harald Welte972b5022012-09-08 19:58:59 +0200390}
391
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200392/*! Increase headroom of empty msgb, reducing the tailroom
Harald Weltebd598e32011-08-16 23:26:52 +0200393 * \param[in] msg message buffer
394 * \param[in] len amount of extra octets to be reserved as headroom
395 *
396 * This function reserves some memory at the beginning of the underlying
397 * data buffer. The idea is to reserve space in case further headers
398 * have to be pushed to the \ref msgb during further processing.
399 *
400 * Calling this function leads to undefined reusults if it is called on
401 * a non-empty \ref msgb.
402 */
Harald Welteec8b4502010-02-20 20:34:29 +0100403static inline void msgb_reserve(struct msgb *msg, int len)
404{
405 msg->data += len;
406 msg->tail += len;
407}
408
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200409/*! Trim the msgb to a given absolute length
Harald Welteb0f91512012-01-14 21:53:41 +0100410 * \param[in] msg message buffer
411 * \param[in] len new total length of buffer
412 * \returns 0 in case of success, negative in case of error
413 */
414static inline int msgb_trim(struct msgb *msg, int len)
415{
Jacob Erlbeckff42b262015-11-27 13:26:19 +0100416 if (len < 0)
417 MSGB_ABORT(msg, "Negative length is not allowed\n");
Harald Welte3068d572012-01-14 22:07:59 +0100418 if (len > msg->data_len)
Harald Welteb0f91512012-01-14 21:53:41 +0100419 return -1;
420
Harald Welte3068d572012-01-14 22:07:59 +0100421 msg->len = len;
422 msg->tail = msg->data + len;
Harald Welteb0f91512012-01-14 21:53:41 +0100423
424 return 0;
425}
426
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200427/*! Trim the msgb to a given layer3 length
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100428 * \param[in] msg message buffer
Harald Welteb0f91512012-01-14 21:53:41 +0100429 * \param[in] l3len new layer3 length
430 * \returns 0 in case of success, negative in case of error
431 */
432static inline int msgb_l3trim(struct msgb *msg, int l3len)
433{
434 return msgb_trim(msg, (msg->l3h - msg->data) + l3len);
435}
436
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200437/*! Allocate message buffer with specified headroom
Harald Weltebd598e32011-08-16 23:26:52 +0200438 * \param[in] size size in bytes, including headroom
439 * \param[in] headroom headroom in bytes
440 * \param[in] name human-readable name
441 * \returns allocated message buffer with specified headroom
442 *
443 * This function is a convenience wrapper around \ref msgb_alloc
444 * followed by \ref msgb_reserve in order to create a new \ref msgb with
445 * user-specified amount of headroom.
446 */
Harald Welteec8b4502010-02-20 20:34:29 +0100447static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
448 const char *name)
449{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200450 osmo_static_assert(size > headroom, headroom_bigger);
Holger Hans Peter Freyther5853f082011-01-16 17:38:22 +0100451
Harald Welteec8b4502010-02-20 20:34:29 +0100452 struct msgb *msg = msgb_alloc(size, name);
453 if (msg)
454 msgb_reserve(msg, headroom);
455 return msg;
456}
457
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200458/*! Check a message buffer for consistency
Jacob Erlbeck8db11342015-11-27 13:26:15 +0100459 * \param[in] msg message buffer
460 * \returns 0 (false) if inconsistent, != 0 (true) otherwise
461 */
462static inline int msgb_test_invariant(const struct msgb *msg)
463{
464 const unsigned char *lbound;
465 if (!msg || !msg->data || !msg->tail ||
466 (msg->data + msg->len != msg->tail) ||
467 (msg->data < msg->head) ||
468 (msg->tail > msg->head + msg->data_len))
469 return 0;
470
471 lbound = msg->head;
472
473 if (msg->l1h) {
474 if (msg->l1h < lbound)
475 return 0;
476 lbound = msg->l1h;
477 }
478 if (msg->l2h) {
479 if (msg->l2h < lbound)
480 return 0;
481 lbound = msg->l2h;
482 }
483 if (msg->l3h) {
484 if (msg->l3h < lbound)
485 return 0;
486 lbound = msg->l3h;
487 }
488 if (msg->l4h) {
489 if (msg->l4h < lbound)
490 return 0;
491 lbound = msg->l4h;
492 }
493
494 return lbound <= msg->head + msg->data_len;
495}
496
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200497/* non inline functions to ease binding */
Harald Weltebd598e32011-08-16 23:26:52 +0200498
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200499uint8_t *msgb_data(const struct msgb *msg);
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200500
501void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size);
502void msgb_set_talloc_ctx(void *ctx) OSMO_DEPRECATED("Use msgb_talloc_ctx_init() instead");
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200503
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200504/*! @} */