blob: b1cb6ec70101291c465b08887a83fbfadb289a13 [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 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020030 * \file msgb.h */
Harald Weltebd598e32011-08-16 23:26:52 +020031
Harald Welte652a7232010-07-22 21:55:24 +020032#define MSGB_DEBUG
33
Neels Hofmeyr87e45502017-06-20 00:17:59 +020034/*! Osmocom message buffer */
Harald Welteec8b4502010-02-20 20:34:29 +010035struct msgb {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020036 struct llist_head list; /*!< linked list header */
Harald Welteec8b4502010-02-20 20:34:29 +010037
Pablo Neira Ayuso29cbf612011-07-07 19:46:44 +020038
Harald Welteec8b4502010-02-20 20:34:29 +010039 /* Part of which TRX logical channel we were received / transmitted */
Harald Welte074c9f92010-04-30 14:29:11 +020040 /* FIXME: move them into the control buffer */
Harald Weltec5a0ded2011-07-18 16:59:27 +020041 union {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020042 void *dst; /*!< reference of origin/destination */
Harald Weltec5a0ded2011-07-18 16:59:27 +020043 struct gsm_bts_trx *trx;
44 };
Neels Hofmeyr87e45502017-06-20 00:17:59 +020045 struct gsm_lchan *lchan; /*!< logical channel */
Harald Welteec8b4502010-02-20 20:34:29 +010046
Neels Hofmeyr87e45502017-06-20 00:17:59 +020047 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 Welteec8b4502010-02-20 20:34:29 +010051
Neels Hofmeyr87e45502017-06-20 00:17:59 +020052 unsigned long cb[5]; /*!< control buffer */
Harald Welte074c9f92010-04-30 14:29:11 +020053
Neels Hofmeyr87e45502017-06-20 00:17:59 +020054 uint16_t data_len; /*!< length of underlying data array */
55 uint16_t len; /*!< length of bytes used in msgb */
Harald Welteec8b4502010-02-20 20:34:29 +010056
Neels Hofmeyr87e45502017-06-20 00:17:59 +020057 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 Welteec8b4502010-02-20 20:34:29 +010061};
62
63extern struct msgb *msgb_alloc(uint16_t size, const char *name);
64extern void msgb_free(struct msgb *m);
65extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
66extern struct msgb *msgb_dequeue(struct llist_head *queue);
67extern void msgb_reset(struct msgb *m);
Harald Welte972b5022012-09-08 19:58:59 +020068uint16_t msgb_length(const struct msgb *msg);
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +010069extern const char *msgb_hexdump(const struct msgb *msg);
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +010070extern int msgb_resize_area(struct msgb *msg, uint8_t *area,
71 int old_size, int new_size);
72extern struct msgb *msgb_copy(const struct msgb *msg, const char *name);
Jacob Erlbeck8db11342015-11-27 13:26:15 +010073static int msgb_test_invariant(const struct msgb *msg) __attribute__((pure));
Harald Welteec8b4502010-02-20 20:34:29 +010074
Neels Hofmeyr3fad5d72017-11-20 14:15:03 +010075/*! Free all msgbs from a queue built with msgb_enqueue().
76 * \param[in] queue list head of a msgb queue.
77 */
78static inline void msgb_queue_free(struct llist_head *queue)
79{
80 struct msgb *msg;
81 while ((msg = msgb_dequeue(queue))) msgb_free(msg);
82}
83
Pau Espin Pedrol8ce6f482018-08-17 10:33:57 +020084/*! Enqueue message buffer to tail of a queue and increment queue size counter
85 * \param[in] queue linked list header of queue
86 * \param[in] msg message buffer to be added to the queue
87 * \param[in] count pointer to variable holding size of the queue
88 *
89 * The function will append the specified message buffer \a msg to the queue
90 * implemented by \ref llist_head \a queue using function \ref msgb_enqueue_count,
91 * then increment \a count
92 */
93static inline void msgb_enqueue_count(struct llist_head *queue, struct msgb *msg,
94 unsigned int *count)
95{
96 msgb_enqueue(queue, msg);
97 (*count)++;
98}
99
100/*! Dequeue message buffer from head of queue and decrement queue size counter
101 * \param[in] queue linked list header of queue
102 * \param[in] count pointer to variable holding size of the queue
103 * \returns message buffer (if any) or NULL if queue empty
104 *
105 * The function will remove the first message buffer from the queue
106 * implemented by \ref llist_head \a queue using function \ref msgb_enqueue_count,
107 * and decrement \a count, all if queue is not empty.
108 */
109static inline struct msgb *msgb_dequeue_count(struct llist_head *queue,
110 unsigned int *count)
111{
112 struct msgb *msg = msgb_dequeue(queue);
113 if (msg)
114 (*count)--;
115 return msg;
116}
117
Harald Welte652a7232010-07-22 21:55:24 +0200118#ifdef MSGB_DEBUG
Pablo Neira Ayuso83419342011-03-22 16:36:13 +0100119#include <osmocom/core/panic.h>
Harald Welte929d8872010-11-05 07:47:41 +0100120#define MSGB_ABORT(msg, fmt, args ...) do { \
Harald Weltedab02872010-11-09 13:42:26 +0100121 osmo_panic("msgb(%p): " fmt, msg, ## args); \
Harald Welte929d8872010-11-05 07:47:41 +0100122 } while(0)
123#else
124#define MSGB_ABORT(msg, fmt, args ...)
Harald Welte652a7232010-07-22 21:55:24 +0200125#endif
126
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200127/*! obtain L1 header of msgb */
Harald Weltefdd0a702010-03-01 22:30:51 +0100128#define msgb_l1(m) ((void *)(m->l1h))
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200129/*! obtain L2 header of msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100130#define msgb_l2(m) ((void *)(m->l2h))
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200131/*! obtain L3 header of msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100132#define msgb_l3(m) ((void *)(m->l3h))
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200133/*! obtain SMS header of msgb */
Harald Weltebb77c9d2010-04-30 14:26:12 +0200134#define msgb_sms(m) ((void *)(m->l4h))
Harald Welteec8b4502010-02-20 20:34:29 +0100135
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200136/*! determine length of L1 message
Harald Weltebd598e32011-08-16 23:26:52 +0200137 * \param[in] msgb message buffer
138 * \returns size of L1 message in bytes
139 *
140 * This function computes the number of bytes between the tail of the
141 * message and the layer 1 header.
142 */
Harald Weltefdd0a702010-03-01 22:30:51 +0100143static inline unsigned int msgb_l1len(const struct msgb *msgb)
144{
145 return msgb->tail - (uint8_t *)msgb_l1(msgb);
146}
147
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200148/*! determine length of L2 message
Harald Weltebd598e32011-08-16 23:26:52 +0200149 * \param[in] msgb message buffer
150 * \returns size of L2 message in bytes
151 *
152 * This function computes the number of bytes between the tail of the
153 * message and the layer 2 header.
154 */
Harald Welteec8b4502010-02-20 20:34:29 +0100155static inline unsigned int msgb_l2len(const struct msgb *msgb)
156{
157 return msgb->tail - (uint8_t *)msgb_l2(msgb);
158}
159
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200160/*! determine length of L3 message
Harald Weltebd598e32011-08-16 23:26:52 +0200161 * \param[in] msgb message buffer
162 * \returns size of L3 message in bytes
163 *
164 * This function computes the number of bytes between the tail of the
165 * message and the layer 3 header.
166 */
Harald Welteec8b4502010-02-20 20:34:29 +0100167static inline unsigned int msgb_l3len(const struct msgb *msgb)
168{
169 return msgb->tail - (uint8_t *)msgb_l3(msgb);
170}
171
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200172/*! determine the length of the header
Harald Weltebd598e32011-08-16 23:26:52 +0200173 * \param[in] msgb message buffer
174 * \returns number of bytes between start of buffer and start of msg
175 *
176 * This function computes the length difference between the underlying
177 * data buffer and the used section of the \a msgb.
178 */
Harald Welteec8b4502010-02-20 20:34:29 +0100179static inline unsigned int msgb_headlen(const struct msgb *msgb)
180{
181 return msgb->len - msgb->data_len;
182}
Harald Welte652a7232010-07-22 21:55:24 +0200183
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200184/*! determine how much tail room is left in msgb
Harald Weltebd598e32011-08-16 23:26:52 +0200185 * \param[in] msgb message buffer
186 * \returns number of bytes remaining at end of msgb
187 *
188 * This function computes the amount of octets left in the underlying
189 * data buffer after the end of the message.
190 */
Harald Welte652a7232010-07-22 21:55:24 +0200191static inline int msgb_tailroom(const struct msgb *msgb)
192{
193 return (msgb->head + msgb->data_len) - msgb->tail;
194}
195
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200196/*! determine the amount of headroom in msgb
Harald Weltebd598e32011-08-16 23:26:52 +0200197 * \param[in] msgb message buffer
198 * \returns number of bytes left ahead of message start in msgb
199 *
200 * This function computes the amount of bytes left in the underlying
201 * data buffer before the start of the actual message.
202 */
Harald Welte652a7232010-07-22 21:55:24 +0200203static inline int msgb_headroom(const struct msgb *msgb)
204{
205 return (msgb->data - msgb->head);
206}
207
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200208/*! append data to end of message buffer
Harald Weltebd598e32011-08-16 23:26:52 +0200209 * \param[in] msgb message buffer
210 * \param[in] len number of bytes to append to message
211 * \returns pointer to start of newly-appended data
212 *
213 * This function will move the \a tail pointer of the message buffer \a
214 * len bytes further, thus enlarging the message by \a len bytes.
215 *
216 * The return value is a pointer to start of the newly added section at
217 * the end of the message and can be used for actually filling/copying
218 * data into it.
219 */
Harald Welteec8b4502010-02-20 20:34:29 +0100220static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
221{
222 unsigned char *tmp = msgb->tail;
Harald Welte46a45a62010-11-09 13:41:48 +0100223 if (msgb_tailroom(msgb) < (int) len)
Neels Hofmeyr418ca582016-09-09 02:03:43 +0200224 MSGB_ABORT(msgb, "Not enough tailroom msgb_put (%u < %u)\n",
Harald Welte929d8872010-11-05 07:47:41 +0100225 msgb_tailroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100226 msgb->tail += len;
227 msgb->len += len;
228 return tmp;
229}
Harald Weltebd598e32011-08-16 23:26:52 +0200230
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200231/*! append a uint8 value to the end of the message
Harald Weltebd598e32011-08-16 23:26:52 +0200232 * \param[in] msgb message buffer
233 * \param[in] word unsigned 8bit byte to be appended
234 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100235static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
236{
237 uint8_t *space = msgb_put(msgb, 1);
238 space[0] = word & 0xFF;
239}
Harald Weltebd598e32011-08-16 23:26:52 +0200240
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200241/*! append a uint16 value to the end of the message
Harald Weltebd598e32011-08-16 23:26:52 +0200242 * \param[in] msgb message buffer
243 * \param[in] word unsigned 16bit byte to be appended
244 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100245static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
246{
247 uint8_t *space = msgb_put(msgb, 2);
Max53777012014-06-04 19:07:41 +0200248 osmo_store16be(word, space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100249}
Harald Weltebd598e32011-08-16 23:26:52 +0200250
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200251/*! append a uint32 value to the end of the message
Harald Weltebd598e32011-08-16 23:26:52 +0200252 * \param[in] msgb message buffer
253 * \param[in] word unsigned 32bit byte to be appended
254 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100255static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
256{
257 uint8_t *space = msgb_put(msgb, 4);
Max53777012014-06-04 19:07:41 +0200258 osmo_store32be(word, space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100259}
Harald Weltebd598e32011-08-16 23:26:52 +0200260
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200261/*! remove data from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200262 * \param[in] msgb message buffer
263 * \param[in] len number of bytes to remove from end
264 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100265static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
266{
Harald Welte972b5022012-09-08 19:58:59 +0200267 if (msgb_length(msgb) < len)
268 MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n",
269 len, msgb_length(msgb));
270 msgb->tail -= len;
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100271 msgb->len -= len;
Pau Espin Pedrol9fa09122018-04-11 20:09:13 +0200272 return msgb->tail;
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100273}
Max53777012014-06-04 19:07:41 +0200274
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200275/*! remove uint8 from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200276 * \param[in] msgb message buffer
277 * \returns 8bit value taken from end of msgb
278 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100279static inline uint8_t msgb_get_u8(struct msgb *msgb)
280{
281 uint8_t *space = msgb_get(msgb, 1);
282 return space[0];
283}
Max53777012014-06-04 19:07:41 +0200284
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200285/*! remove uint16 from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200286 * \param[in] msgb message buffer
287 * \returns 16bit value taken from end of msgb
288 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100289static inline uint16_t msgb_get_u16(struct msgb *msgb)
290{
291 uint8_t *space = msgb_get(msgb, 2);
Max53777012014-06-04 19:07:41 +0200292 return osmo_load16be(space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100293}
Max53777012014-06-04 19:07:41 +0200294
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200295/*! remove uint32 from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200296 * \param[in] msgb message buffer
297 * \returns 32bit value taken from end of msgb
298 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100299static inline uint32_t msgb_get_u32(struct msgb *msgb)
300{
301 uint8_t *space = msgb_get(msgb, 4);
Max53777012014-06-04 19:07:41 +0200302 return osmo_load32be(space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100303}
Harald Weltebd598e32011-08-16 23:26:52 +0200304
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200305/*! prepend (push) some data to start of message
Harald Weltebd598e32011-08-16 23:26:52 +0200306 * \param[in] msgb message buffer
307 * \param[in] len number of bytes to pre-pend
308 * \returns pointer to newly added portion at start of \a msgb
309 *
310 * This function moves the \a data pointer of the \ref msgb further
311 * to the front (by \a len bytes), thereby enlarging the message by \a
312 * len bytes.
313 *
314 * The return value is a pointer to the newly added section in the
315 * beginning of the message. It can be used to fill/copy data into it.
316 */
Harald Welteec8b4502010-02-20 20:34:29 +0100317static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
318{
Harald Welte46a45a62010-11-09 13:41:48 +0100319 if (msgb_headroom(msgb) < (int) len)
Harald Welte929d8872010-11-05 07:47:41 +0100320 MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
321 msgb_headroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100322 msgb->data -= len;
323 msgb->len += len;
324 return msgb->data;
325}
Max53777012014-06-04 19:07:41 +0200326
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200327/*! prepend a uint8 value to the head of the message
Harald Welte8593f1f2016-11-11 19:26:51 +0100328 * \param[in] msgb message buffer
329 * \param[in] word unsigned 8bit byte to be prepended
330 */
331static inline void msgb_push_u8(struct msgb *msg, uint8_t word)
332{
333 uint8_t *space = msgb_push(msg, 1);
334 space[0] = word;
335}
336
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200337/*! prepend a uint16 value to the head of the message
Harald Welte8593f1f2016-11-11 19:26:51 +0100338 * \param[in] msgb message buffer
339 * \param[in] word unsigned 16bit byte to be prepended
340 */
341static inline void msgb_push_u16(struct msgb *msg, uint16_t word)
342{
343 uint16_t *space = (uint16_t *) msgb_push(msg, 2);
344 osmo_store16be(word, space);
345}
346
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200347/*! prepend a uint32 value to the head of the message
Harald Welte8593f1f2016-11-11 19:26:51 +0100348 * \param[in] msgb message buffer
349 * \param[in] word unsigned 32bit byte to be prepended
350 */
351static inline void msgb_push_u32(struct msgb *msg, uint32_t word)
352{
353 uint32_t *space = (uint32_t *) msgb_push(msg, 4);
354 osmo_store32be(word, space);
355}
356
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200357/*! remove (pull) a header from the front of the message buffer
Harald Weltebd598e32011-08-16 23:26:52 +0200358 * \param[in] msgb message buffer
359 * \param[in] len number of octets to be pulled
360 * \returns pointer to new start of msgb
361 *
362 * This function moves the \a data pointer of the \ref msgb further back
363 * in the message, thereby shrinking the size of the message by \a len
364 * bytes.
365 */
Harald Welteec8b4502010-02-20 20:34:29 +0100366static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
367{
Pau Espin Pedrolc62fc2d2018-04-11 20:14:08 +0200368 if (msgb_length(msgb) < len)
369 MSGB_ABORT(msgb, "msgb too small to pull %u (len %u)\n",
370 len, msgb_length(msgb));
Harald Welteec8b4502010-02-20 20:34:29 +0100371 msgb->len -= len;
372 return msgb->data += len;
373}
Harald Welteec8b4502010-02-20 20:34:29 +0100374
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200375/*! remove (pull) all headers in front of l3h from the message buffer.
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100376 * \param[in] msgb message buffer with a valid l3h
377 * \returns pointer to new start of msgb (l3h)
378 *
379 * This function moves the \a data pointer of the \ref msgb further back
380 * in the message, thereby shrinking the size of the message.
381 * l1h and l2h will be cleared.
382 */
383static inline unsigned char *msgb_pull_to_l3(struct msgb *msg)
384{
385 unsigned char *ret = msgb_pull(msg, msg->l3h - msg->data);
386 msg->l1h = msg->l2h = NULL;
387 return ret;
388}
389
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200390/*! remove (pull) all headers in front of l2h from the message buffer.
Harald Welte59a9ebf2017-04-15 19:04:34 +0200391 * \param[in] msgb message buffer with a valid l2h
392 * \returns pointer to new start of msgb (l2h)
393 *
394 * This function moves the \a data pointer of the \ref msgb further back
395 * in the message, thereby shrinking the size of the message.
396 * l1h will be cleared.
397 */
398static inline unsigned char *msgb_pull_to_l2(struct msgb *msg)
399{
400 unsigned char *ret = msgb_pull(msg, msg->l2h - msg->data);
401 msg->l1h = NULL;
402 return ret;
403}
404
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200405/*! remove uint8 from front of message
Harald Welte972b5022012-09-08 19:58:59 +0200406 * \param[in] msgb message buffer
407 * \returns 8bit value taken from end of msgb
408 */
409static inline uint8_t msgb_pull_u8(struct msgb *msgb)
410{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100411 uint8_t *space = msgb_pull(msgb, 1) - 1;
Harald Welte972b5022012-09-08 19:58:59 +0200412 return space[0];
413}
Max53777012014-06-04 19:07:41 +0200414
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200415/*! remove uint16 from front of message
Harald Welte972b5022012-09-08 19:58:59 +0200416 * \param[in] msgb message buffer
417 * \returns 16bit value taken from end of msgb
418 */
419static inline uint16_t msgb_pull_u16(struct msgb *msgb)
420{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100421 uint8_t *space = msgb_pull(msgb, 2) - 2;
Max53777012014-06-04 19:07:41 +0200422 return osmo_load16be(space);
Harald Welte972b5022012-09-08 19:58:59 +0200423}
Max53777012014-06-04 19:07:41 +0200424
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200425/*! remove uint32 from front of message
Harald Welte972b5022012-09-08 19:58:59 +0200426 * \param[in] msgb message buffer
427 * \returns 32bit value taken from end of msgb
428 */
429static inline uint32_t msgb_pull_u32(struct msgb *msgb)
430{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100431 uint8_t *space = msgb_pull(msgb, 4) - 4;
Max53777012014-06-04 19:07:41 +0200432 return osmo_load32be(space);
Harald Welte972b5022012-09-08 19:58:59 +0200433}
434
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200435/*! Increase headroom of empty msgb, reducing the tailroom
Harald Weltebd598e32011-08-16 23:26:52 +0200436 * \param[in] msg message buffer
437 * \param[in] len amount of extra octets to be reserved as headroom
438 *
439 * This function reserves some memory at the beginning of the underlying
440 * data buffer. The idea is to reserve space in case further headers
441 * have to be pushed to the \ref msgb during further processing.
442 *
443 * Calling this function leads to undefined reusults if it is called on
444 * a non-empty \ref msgb.
445 */
Harald Welteec8b4502010-02-20 20:34:29 +0100446static inline void msgb_reserve(struct msgb *msg, int len)
447{
448 msg->data += len;
449 msg->tail += len;
450}
451
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200452/*! Trim the msgb to a given absolute length
Harald Welteb0f91512012-01-14 21:53:41 +0100453 * \param[in] msg message buffer
454 * \param[in] len new total length of buffer
455 * \returns 0 in case of success, negative in case of error
456 */
457static inline int msgb_trim(struct msgb *msg, int len)
458{
Jacob Erlbeckff42b262015-11-27 13:26:19 +0100459 if (len < 0)
460 MSGB_ABORT(msg, "Negative length is not allowed\n");
Harald Welte3068d572012-01-14 22:07:59 +0100461 if (len > msg->data_len)
Harald Welteb0f91512012-01-14 21:53:41 +0100462 return -1;
463
Harald Welte3068d572012-01-14 22:07:59 +0100464 msg->len = len;
465 msg->tail = msg->data + len;
Harald Welteb0f91512012-01-14 21:53:41 +0100466
467 return 0;
468}
469
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200470/*! Trim the msgb to a given layer3 length
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100471 * \param[in] msg message buffer
Harald Welteb0f91512012-01-14 21:53:41 +0100472 * \param[in] l3len new layer3 length
473 * \returns 0 in case of success, negative in case of error
474 */
475static inline int msgb_l3trim(struct msgb *msg, int l3len)
476{
477 return msgb_trim(msg, (msg->l3h - msg->data) + l3len);
478}
479
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200480/*! Allocate message buffer with specified headroom
Harald Weltebd598e32011-08-16 23:26:52 +0200481 * \param[in] size size in bytes, including headroom
482 * \param[in] headroom headroom in bytes
483 * \param[in] name human-readable name
484 * \returns allocated message buffer with specified headroom
485 *
486 * This function is a convenience wrapper around \ref msgb_alloc
487 * followed by \ref msgb_reserve in order to create a new \ref msgb with
488 * user-specified amount of headroom.
489 */
Harald Welteec8b4502010-02-20 20:34:29 +0100490static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
491 const char *name)
492{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200493 osmo_static_assert(size > headroom, headroom_bigger);
Holger Hans Peter Freyther5853f082011-01-16 17:38:22 +0100494
Harald Welteec8b4502010-02-20 20:34:29 +0100495 struct msgb *msg = msgb_alloc(size, name);
496 if (msg)
497 msgb_reserve(msg, headroom);
498 return msg;
499}
500
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200501/*! Check a message buffer for consistency
Jacob Erlbeck8db11342015-11-27 13:26:15 +0100502 * \param[in] msg message buffer
503 * \returns 0 (false) if inconsistent, != 0 (true) otherwise
504 */
505static inline int msgb_test_invariant(const struct msgb *msg)
506{
507 const unsigned char *lbound;
508 if (!msg || !msg->data || !msg->tail ||
509 (msg->data + msg->len != msg->tail) ||
510 (msg->data < msg->head) ||
511 (msg->tail > msg->head + msg->data_len))
512 return 0;
513
514 lbound = msg->head;
515
516 if (msg->l1h) {
517 if (msg->l1h < lbound)
518 return 0;
519 lbound = msg->l1h;
520 }
521 if (msg->l2h) {
522 if (msg->l2h < lbound)
523 return 0;
524 lbound = msg->l2h;
525 }
526 if (msg->l3h) {
527 if (msg->l3h < lbound)
528 return 0;
529 lbound = msg->l3h;
530 }
531 if (msg->l4h) {
532 if (msg->l4h < lbound)
533 return 0;
534 lbound = msg->l4h;
535 }
536
537 return lbound <= msg->head + msg->data_len;
538}
539
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200540/* non inline functions to ease binding */
Harald Weltebd598e32011-08-16 23:26:52 +0200541
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200542uint8_t *msgb_data(const struct msgb *msg);
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200543
544void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size);
545void msgb_set_talloc_ctx(void *ctx) OSMO_DEPRECATED("Use msgb_talloc_ctx_init() instead");
Philipp Maierc5b47cc2017-10-10 16:53:21 +0200546int msgb_printf(struct msgb *msgb, const char *format, ...);
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200547
Harald Welte57217702018-02-09 02:01:48 +0100548static inline const char *msgb_hexdump_l2(const struct msgb *msg)
549{
550 if (!msgb_l2(msg) || !(msgb_l2len(msg)))
551 return "[]";
Harald Welteebd2b0f2018-02-09 10:32:03 +0100552 return osmo_hexdump((const unsigned char *) msgb_l2(msg), msgb_l2len(msg));
Harald Welte57217702018-02-09 02:01:48 +0100553}
554
555static inline const char *msgb_hexdump_l3(const struct msgb *msg)
556{
557 if (!msgb_l3(msg) || !(msgb_l3len(msg)))
558 return "[]";
Harald Welteebd2b0f2018-02-09 10:32:03 +0100559 return osmo_hexdump((const unsigned char*) msgb_l3(msg), msgb_l3len(msg));
Harald Welte57217702018-02-09 02:01:48 +0100560}
561
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200562/*! @} */