blob: b7c842213e35e6b9342bbe2bbe1be2d2358c1b1e [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))
Maxcb02a392018-12-03 14:00:49 +0100133/*! obtain L4 header of msgb */
134#define msgb_l4(m) ((void *)(m->l4h))
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200135/*! obtain SMS header of msgb */
Maxcb02a392018-12-03 14:00:49 +0100136#define msgb_sms(m) msgb_l4(m)
Harald Welteec8b4502010-02-20 20:34:29 +0100137
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200138/*! determine length of L1 message
Harald Weltebd598e32011-08-16 23:26:52 +0200139 * \param[in] msgb message buffer
140 * \returns size of L1 message in bytes
141 *
142 * This function computes the number of bytes between the tail of the
143 * message and the layer 1 header.
144 */
Harald Weltefdd0a702010-03-01 22:30:51 +0100145static inline unsigned int msgb_l1len(const struct msgb *msgb)
146{
147 return msgb->tail - (uint8_t *)msgb_l1(msgb);
148}
149
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200150/*! determine length of L2 message
Harald Weltebd598e32011-08-16 23:26:52 +0200151 * \param[in] msgb message buffer
152 * \returns size of L2 message in bytes
153 *
154 * This function computes the number of bytes between the tail of the
155 * message and the layer 2 header.
156 */
Harald Welteec8b4502010-02-20 20:34:29 +0100157static inline unsigned int msgb_l2len(const struct msgb *msgb)
158{
159 return msgb->tail - (uint8_t *)msgb_l2(msgb);
160}
161
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200162/*! determine length of L3 message
Harald Weltebd598e32011-08-16 23:26:52 +0200163 * \param[in] msgb message buffer
164 * \returns size of L3 message in bytes
165 *
166 * This function computes the number of bytes between the tail of the
167 * message and the layer 3 header.
168 */
Harald Welteec8b4502010-02-20 20:34:29 +0100169static inline unsigned int msgb_l3len(const struct msgb *msgb)
170{
171 return msgb->tail - (uint8_t *)msgb_l3(msgb);
172}
173
Vadim Yanitskiye521ede2018-11-07 03:53:03 +0700174/*! determine length of L4 message
175 * \param[in] msgb message buffer
176 * \returns size of L4 message in bytes
177 *
178 * This function computes the number of bytes between the tail of the
179 * message and the layer 4 header.
180 */
181static inline unsigned int msgb_l4len(const struct msgb *msgb)
182{
183 return msgb->tail - (uint8_t *)msgb_sms(msgb);
184}
185
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200186/*! determine the length of the header
Harald Weltebd598e32011-08-16 23:26:52 +0200187 * \param[in] msgb message buffer
188 * \returns number of bytes between start of buffer and start of msg
189 *
190 * This function computes the length difference between the underlying
191 * data buffer and the used section of the \a msgb.
192 */
Harald Welteec8b4502010-02-20 20:34:29 +0100193static inline unsigned int msgb_headlen(const struct msgb *msgb)
194{
195 return msgb->len - msgb->data_len;
196}
Harald Welte652a7232010-07-22 21:55:24 +0200197
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200198/*! determine how much tail room is left in msgb
Harald Weltebd598e32011-08-16 23:26:52 +0200199 * \param[in] msgb message buffer
200 * \returns number of bytes remaining at end of msgb
201 *
202 * This function computes the amount of octets left in the underlying
203 * data buffer after the end of the message.
204 */
Harald Welte652a7232010-07-22 21:55:24 +0200205static inline int msgb_tailroom(const struct msgb *msgb)
206{
207 return (msgb->head + msgb->data_len) - msgb->tail;
208}
209
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200210/*! determine the amount of headroom in msgb
Harald Weltebd598e32011-08-16 23:26:52 +0200211 * \param[in] msgb message buffer
212 * \returns number of bytes left ahead of message start in msgb
213 *
214 * This function computes the amount of bytes left in the underlying
215 * data buffer before the start of the actual message.
216 */
Harald Welte652a7232010-07-22 21:55:24 +0200217static inline int msgb_headroom(const struct msgb *msgb)
218{
219 return (msgb->data - msgb->head);
220}
221
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200222/*! append data to end of message buffer
Harald Weltebd598e32011-08-16 23:26:52 +0200223 * \param[in] msgb message buffer
224 * \param[in] len number of bytes to append to message
225 * \returns pointer to start of newly-appended data
226 *
227 * This function will move the \a tail pointer of the message buffer \a
228 * len bytes further, thus enlarging the message by \a len bytes.
229 *
230 * The return value is a pointer to start of the newly added section at
231 * the end of the message and can be used for actually filling/copying
232 * data into it.
233 */
Harald Welteec8b4502010-02-20 20:34:29 +0100234static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
235{
236 unsigned char *tmp = msgb->tail;
Harald Welte46a45a62010-11-09 13:41:48 +0100237 if (msgb_tailroom(msgb) < (int) len)
Neels Hofmeyr418ca582016-09-09 02:03:43 +0200238 MSGB_ABORT(msgb, "Not enough tailroom msgb_put (%u < %u)\n",
Harald Welte929d8872010-11-05 07:47:41 +0100239 msgb_tailroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100240 msgb->tail += len;
241 msgb->len += len;
242 return tmp;
243}
Harald Weltebd598e32011-08-16 23:26:52 +0200244
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200245/*! append a uint8 value to the end of the message
Harald Weltebd598e32011-08-16 23:26:52 +0200246 * \param[in] msgb message buffer
247 * \param[in] word unsigned 8bit byte to be appended
248 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100249static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
250{
251 uint8_t *space = msgb_put(msgb, 1);
252 space[0] = word & 0xFF;
253}
Harald Weltebd598e32011-08-16 23:26:52 +0200254
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200255/*! append a uint16 value to the end of the message
Harald Weltebd598e32011-08-16 23:26:52 +0200256 * \param[in] msgb message buffer
257 * \param[in] word unsigned 16bit byte to be appended
258 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100259static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
260{
261 uint8_t *space = msgb_put(msgb, 2);
Max53777012014-06-04 19:07:41 +0200262 osmo_store16be(word, 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/*! append a uint32 value to the end of the message
Harald Weltebd598e32011-08-16 23:26:52 +0200266 * \param[in] msgb message buffer
267 * \param[in] word unsigned 32bit byte to be appended
268 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100269static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
270{
271 uint8_t *space = msgb_put(msgb, 4);
Max53777012014-06-04 19:07:41 +0200272 osmo_store32be(word, space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100273}
Harald Weltebd598e32011-08-16 23:26:52 +0200274
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200275/*! remove data from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200276 * \param[in] msgb message buffer
277 * \param[in] len number of bytes to remove from end
278 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100279static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
280{
Harald Welte972b5022012-09-08 19:58:59 +0200281 if (msgb_length(msgb) < len)
282 MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n",
283 len, msgb_length(msgb));
284 msgb->tail -= len;
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100285 msgb->len -= len;
Pau Espin Pedrol9fa09122018-04-11 20:09:13 +0200286 return msgb->tail;
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100287}
Max53777012014-06-04 19:07:41 +0200288
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200289/*! remove uint8 from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200290 * \param[in] msgb message buffer
291 * \returns 8bit value taken from end of msgb
292 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100293static inline uint8_t msgb_get_u8(struct msgb *msgb)
294{
295 uint8_t *space = msgb_get(msgb, 1);
296 return space[0];
297}
Max53777012014-06-04 19:07:41 +0200298
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200299/*! remove uint16 from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200300 * \param[in] msgb message buffer
301 * \returns 16bit value taken from end of msgb
302 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100303static inline uint16_t msgb_get_u16(struct msgb *msgb)
304{
305 uint8_t *space = msgb_get(msgb, 2);
Max53777012014-06-04 19:07:41 +0200306 return osmo_load16be(space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100307}
Max53777012014-06-04 19:07:41 +0200308
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200309/*! remove uint32 from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200310 * \param[in] msgb message buffer
311 * \returns 32bit value taken from end of msgb
312 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100313static inline uint32_t msgb_get_u32(struct msgb *msgb)
314{
315 uint8_t *space = msgb_get(msgb, 4);
Max53777012014-06-04 19:07:41 +0200316 return osmo_load32be(space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100317}
Harald Weltebd598e32011-08-16 23:26:52 +0200318
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200319/*! prepend (push) some data to start of message
Harald Weltebd598e32011-08-16 23:26:52 +0200320 * \param[in] msgb message buffer
321 * \param[in] len number of bytes to pre-pend
322 * \returns pointer to newly added portion at start of \a msgb
323 *
324 * This function moves the \a data pointer of the \ref msgb further
325 * to the front (by \a len bytes), thereby enlarging the message by \a
326 * len bytes.
327 *
328 * The return value is a pointer to the newly added section in the
329 * beginning of the message. It can be used to fill/copy data into it.
330 */
Harald Welteec8b4502010-02-20 20:34:29 +0100331static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
332{
Harald Welte46a45a62010-11-09 13:41:48 +0100333 if (msgb_headroom(msgb) < (int) len)
Harald Welte929d8872010-11-05 07:47:41 +0100334 MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
335 msgb_headroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100336 msgb->data -= len;
337 msgb->len += len;
338 return msgb->data;
339}
Max53777012014-06-04 19:07:41 +0200340
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200341/*! prepend a uint8 value to the head of the message
Harald Welte8593f1f2016-11-11 19:26:51 +0100342 * \param[in] msgb message buffer
343 * \param[in] word unsigned 8bit byte to be prepended
344 */
345static inline void msgb_push_u8(struct msgb *msg, uint8_t word)
346{
347 uint8_t *space = msgb_push(msg, 1);
348 space[0] = word;
349}
350
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200351/*! prepend a uint16 value to the head of the message
Harald Welte8593f1f2016-11-11 19:26:51 +0100352 * \param[in] msgb message buffer
353 * \param[in] word unsigned 16bit byte to be prepended
354 */
355static inline void msgb_push_u16(struct msgb *msg, uint16_t word)
356{
357 uint16_t *space = (uint16_t *) msgb_push(msg, 2);
358 osmo_store16be(word, space);
359}
360
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200361/*! prepend a uint32 value to the head of the message
Harald Welte8593f1f2016-11-11 19:26:51 +0100362 * \param[in] msgb message buffer
363 * \param[in] word unsigned 32bit byte to be prepended
364 */
365static inline void msgb_push_u32(struct msgb *msg, uint32_t word)
366{
367 uint32_t *space = (uint32_t *) msgb_push(msg, 4);
368 osmo_store32be(word, space);
369}
370
Max84fb5bb2018-11-08 13:23:59 +0100371static inline unsigned char *msgb_wrap_with_TL(struct msgb *msgb, uint8_t tag)
372{
373 uint8_t *data = msgb_push(msgb, 2);
374
375 data[0] = tag;
376 data[1] = msgb->len - 2;
377 return data;
378}
379
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200380/*! remove (pull) a header from the front of the message buffer
Harald Weltebd598e32011-08-16 23:26:52 +0200381 * \param[in] msgb message buffer
382 * \param[in] len number of octets to be pulled
383 * \returns pointer to new start of msgb
384 *
385 * This function moves the \a data pointer of the \ref msgb further back
386 * in the message, thereby shrinking the size of the message by \a len
387 * bytes.
388 */
Harald Welteec8b4502010-02-20 20:34:29 +0100389static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
390{
Pau Espin Pedrolc62fc2d2018-04-11 20:14:08 +0200391 if (msgb_length(msgb) < len)
392 MSGB_ABORT(msgb, "msgb too small to pull %u (len %u)\n",
393 len, msgb_length(msgb));
Harald Welteec8b4502010-02-20 20:34:29 +0100394 msgb->len -= len;
395 return msgb->data += len;
396}
Harald Welteec8b4502010-02-20 20:34:29 +0100397
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200398/*! remove (pull) all headers in front of l3h from the message buffer.
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100399 * \param[in] msgb message buffer with a valid l3h
400 * \returns pointer to new start of msgb (l3h)
401 *
402 * This function moves the \a data pointer of the \ref msgb further back
403 * in the message, thereby shrinking the size of the message.
404 * l1h and l2h will be cleared.
405 */
406static inline unsigned char *msgb_pull_to_l3(struct msgb *msg)
407{
408 unsigned char *ret = msgb_pull(msg, msg->l3h - msg->data);
409 msg->l1h = msg->l2h = NULL;
410 return ret;
411}
412
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200413/*! remove (pull) all headers in front of l2h from the message buffer.
Harald Welte59a9ebf2017-04-15 19:04:34 +0200414 * \param[in] msgb message buffer with a valid l2h
415 * \returns pointer to new start of msgb (l2h)
416 *
417 * This function moves the \a data pointer of the \ref msgb further back
418 * in the message, thereby shrinking the size of the message.
419 * l1h will be cleared.
420 */
421static inline unsigned char *msgb_pull_to_l2(struct msgb *msg)
422{
423 unsigned char *ret = msgb_pull(msg, msg->l2h - msg->data);
424 msg->l1h = NULL;
425 return ret;
426}
427
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200428/*! remove uint8 from front of message
Harald Welte972b5022012-09-08 19:58:59 +0200429 * \param[in] msgb message buffer
430 * \returns 8bit value taken from end of msgb
431 */
432static inline uint8_t msgb_pull_u8(struct msgb *msgb)
433{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100434 uint8_t *space = msgb_pull(msgb, 1) - 1;
Harald Welte972b5022012-09-08 19:58:59 +0200435 return space[0];
436}
Max53777012014-06-04 19:07:41 +0200437
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200438/*! remove uint16 from front of message
Harald Welte972b5022012-09-08 19:58:59 +0200439 * \param[in] msgb message buffer
440 * \returns 16bit value taken from end of msgb
441 */
442static inline uint16_t msgb_pull_u16(struct msgb *msgb)
443{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100444 uint8_t *space = msgb_pull(msgb, 2) - 2;
Max53777012014-06-04 19:07:41 +0200445 return osmo_load16be(space);
Harald Welte972b5022012-09-08 19:58:59 +0200446}
Max53777012014-06-04 19:07:41 +0200447
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200448/*! remove uint32 from front of message
Harald Welte972b5022012-09-08 19:58:59 +0200449 * \param[in] msgb message buffer
450 * \returns 32bit value taken from end of msgb
451 */
452static inline uint32_t msgb_pull_u32(struct msgb *msgb)
453{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100454 uint8_t *space = msgb_pull(msgb, 4) - 4;
Max53777012014-06-04 19:07:41 +0200455 return osmo_load32be(space);
Harald Welte972b5022012-09-08 19:58:59 +0200456}
457
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200458/*! Increase headroom of empty msgb, reducing the tailroom
Harald Weltebd598e32011-08-16 23:26:52 +0200459 * \param[in] msg message buffer
460 * \param[in] len amount of extra octets to be reserved as headroom
461 *
462 * This function reserves some memory at the beginning of the underlying
463 * data buffer. The idea is to reserve space in case further headers
464 * have to be pushed to the \ref msgb during further processing.
465 *
466 * Calling this function leads to undefined reusults if it is called on
467 * a non-empty \ref msgb.
468 */
Harald Welteec8b4502010-02-20 20:34:29 +0100469static inline void msgb_reserve(struct msgb *msg, int len)
470{
471 msg->data += len;
472 msg->tail += len;
473}
474
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200475/*! Trim the msgb to a given absolute length
Harald Welteb0f91512012-01-14 21:53:41 +0100476 * \param[in] msg message buffer
477 * \param[in] len new total length of buffer
478 * \returns 0 in case of success, negative in case of error
479 */
480static inline int msgb_trim(struct msgb *msg, int len)
481{
Jacob Erlbeckff42b262015-11-27 13:26:19 +0100482 if (len < 0)
483 MSGB_ABORT(msg, "Negative length is not allowed\n");
Harald Welte3068d572012-01-14 22:07:59 +0100484 if (len > msg->data_len)
Harald Welteb0f91512012-01-14 21:53:41 +0100485 return -1;
486
Harald Welte3068d572012-01-14 22:07:59 +0100487 msg->len = len;
488 msg->tail = msg->data + len;
Harald Welteb0f91512012-01-14 21:53:41 +0100489
490 return 0;
491}
492
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200493/*! Trim the msgb to a given layer3 length
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100494 * \param[in] msg message buffer
Harald Welteb0f91512012-01-14 21:53:41 +0100495 * \param[in] l3len new layer3 length
496 * \returns 0 in case of success, negative in case of error
497 */
498static inline int msgb_l3trim(struct msgb *msg, int l3len)
499{
500 return msgb_trim(msg, (msg->l3h - msg->data) + l3len);
501}
502
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200503/*! Allocate message buffer with specified headroom
Harald Weltebd598e32011-08-16 23:26:52 +0200504 * \param[in] size size in bytes, including headroom
505 * \param[in] headroom headroom in bytes
506 * \param[in] name human-readable name
507 * \returns allocated message buffer with specified headroom
508 *
509 * This function is a convenience wrapper around \ref msgb_alloc
510 * followed by \ref msgb_reserve in order to create a new \ref msgb with
511 * user-specified amount of headroom.
512 */
Harald Welteec8b4502010-02-20 20:34:29 +0100513static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
514 const char *name)
515{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200516 osmo_static_assert(size > headroom, headroom_bigger);
Holger Hans Peter Freyther5853f082011-01-16 17:38:22 +0100517
Harald Welteec8b4502010-02-20 20:34:29 +0100518 struct msgb *msg = msgb_alloc(size, name);
519 if (msg)
520 msgb_reserve(msg, headroom);
521 return msg;
522}
523
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200524/*! Check a message buffer for consistency
Jacob Erlbeck8db11342015-11-27 13:26:15 +0100525 * \param[in] msg message buffer
526 * \returns 0 (false) if inconsistent, != 0 (true) otherwise
527 */
528static inline int msgb_test_invariant(const struct msgb *msg)
529{
530 const unsigned char *lbound;
531 if (!msg || !msg->data || !msg->tail ||
532 (msg->data + msg->len != msg->tail) ||
533 (msg->data < msg->head) ||
534 (msg->tail > msg->head + msg->data_len))
535 return 0;
536
537 lbound = msg->head;
538
539 if (msg->l1h) {
540 if (msg->l1h < lbound)
541 return 0;
542 lbound = msg->l1h;
543 }
544 if (msg->l2h) {
545 if (msg->l2h < lbound)
546 return 0;
547 lbound = msg->l2h;
548 }
549 if (msg->l3h) {
550 if (msg->l3h < lbound)
551 return 0;
552 lbound = msg->l3h;
553 }
554 if (msg->l4h) {
555 if (msg->l4h < lbound)
556 return 0;
557 lbound = msg->l4h;
558 }
559
560 return lbound <= msg->head + msg->data_len;
561}
562
Maxdb038252018-12-03 14:14:44 +0100563
564/* msgb data comparison helpers */
565
566/*! Compare: check data in msgb against given data
567 * \param[in] msg message buffer
568 * \param[in] data expected data
569 * \param[in] len length of data
570 * \returns boolean indicating whether msgb content is equal to the given data
571 */
572#define msgb_eq_data(msg, data, len) \
573 _msgb_eq(__FILE__, __LINE__, __func__, 0, msg, data, len, false)
574
575/*! Compare: check L1 data in msgb against given data
576 * \param[in] msg message buffer
577 * \param[in] data expected L1 data
578 * \param[in] len length of data
579 * \returns boolean indicating whether msgb L1 content is equal to the given data
580 */
581#define msgb_eq_l1_data(msg, data, len) \
582 _msgb_eq(__FILE__, __LINE__, __func__, 1, msg, data, len, false)
583
584/*! Compare: check L2 data in msgb against given data
585 * \param[in] msg message buffer
586 * \param[in] data expected L2 data
587 * \param[in] len length of data
588 * \returns boolean indicating whether msgb L2 content is equal to the given data
589 */
590#define msgb_eq_l2_data(msg, data, len) \
591 _msgb_eq(__FILE__, __LINE__, __func__, 2, msg, data, len, false)
592
593/*! Compare: check L3 data in msgb against given data
594 * \param[in] msg message buffer
595 * \param[in] data expected L3 data
596 * \param[in] len length of data
597 * \returns boolean indicating whether msgb L3 content is equal to the given data
598 */
599#define msgb_eq_l3_data(msg, data, len) \
600 _msgb_eq(__FILE__, __LINE__, __func__, 3, msg, data, len, false)
601
602/*! Compare: check L4 data in msgb against given data
603 * \param[in] msg message buffer
604 * \param[in] data expected L4 data
605 * \param[in] len length of data
606 * \returns boolean indicating whether msgb L4 content is equal to the given data
607 */
608#define msgb_eq_l4_data(msg, data, len) \
609 _msgb_eq(__FILE__, __LINE__, __func__, 4, msg, data, len, false)
610
611
612/* msgb test/debug helpers */
613
614/*! Compare and print: check data in msgb against given data and print errors if any
615 * \param[in] msg message buffer
616 * \param[in] data expected data
617 * \param[in] len length of data
618 * \returns boolean indicating whether msgb content is equal to the given data
619 */
620#define msgb_eq_data_print(msg, data, len) \
621 _msgb_eq(__FILE__, __LINE__, __func__, 0, msg, data, len, true)
622
623/*! Compare and print: check L1 data in msgb against given data and print errors if any
624 * \param[in] msg message buffer
625 * \param[in] data expected L1 data
626 * \param[in] len length of data
627 * \returns boolean indicating whether msgb L1 content is equal to the given data
628 */
629#define msgb_eq_l1_data_print(msg, data, len) \
630 _msgb_eq(__FILE__, __LINE__, __func__, 1, msg, data, len, true)
631
632/*! Compare and print: check L2 data in msgb against given data and print errors if any
633 * \param[in] msg message buffer
634 * \param[in] data expected L2 data
635 * \param[in] len length of data
636 * \returns boolean indicating whether msgb L2 content is equal to the given data
637 */
638#define msgb_eq_l2_data_print(msg, data, len) \
639 _msgb_eq(__FILE__, __LINE__, __func__, 2, msg, data, len, true)
640
641/*! Compare and print: check L3 data in msgb against given data and print errors if any
642 * \param[in] msg message buffer
643 * \param[in] data expected L3 data
644 * \param[in] len length of data
645 * \returns boolean indicating whether msgb L3 content is equal to the given data
646 */
647#define msgb_eq_l3_data_print(msg, data, len) \
648 _msgb_eq(__FILE__, __LINE__, __func__, 3, msg, data, len, true)
649
650
651/*! Compare and print: check L4 data in msgb against given data and print errors if any
652 * \param[in] msg message buffer
653 * \param[in] data expected L4 data
654 * \param[in] len length of data
655 * \returns boolean indicating whether msgb L4 content is equal to the given data
656 */
657#define msgb_eq_l4_data_print(msg, data, len) \
658 _msgb_eq(__FILE__, __LINE__, __func__, 4, msg, data, len, true)
659
660bool _msgb_eq(const char *file, size_t line, const char *func, uint8_t level,
661 const struct msgb *msg, const uint8_t *data, size_t len, bool print);
662
663
664/* msgb data comparison */
665
666/*! Compare msgbs
667 * \param[in] msg1 message buffer
668 * \param[in] msg2 reference message buffer
669 * \returns boolean indicating whether msgb content is equal
670 */
671#define msgb_eq(msg1, msgb2, len) msgb_eq_data(msg1, msgb_data(msg2), msgb_length(msg2))
672
673/*! Compare msgbs L1 content
674 * \param[in] msg1 message buffer
675 * \param[in] msg2 reference message buffer
676 * \returns boolean indicating whether msgb L1 content is equal
677 */
678#define msgb_eq_l1(msg1, msgb2, len) msgb_eq_l1_data(msg1, msgb_l1(msg2), msgb_l1len(msg2))
679
680/*! Compare msgbs L2 content
681 * \param[in] msg1 message buffer
682 * \param[in] msg2 reference message buffer
683 * \returns boolean indicating whether msgb L2 content is equal
684 */
685#define msgb_eq_l2(msg1, msgb2, len) msgb_eq_l2_data(msg1, msgb_l2(msg2), msgb_l2len(msg2))
686
687/*! Compare msgbs L3 content
688 * \param[in] msg1 message buffer
689 * \param[in] msg2 reference message buffer
690 * \returns boolean indicating whether msgb L3 content is equal
691 */
692#define msgb_eq_l3(msg1, msgb2, len) msgb_eq_l3_data(msg1, msgb_l3(msg2), msgb_l3len(msg2))
693
694/*! Compare msgbs L4 content
695 * \param[in] msg1 message buffer
696 * \param[in] msg2 reference message buffer
697 * \returns boolean indicating whether msgb L4 content is equal
698 */
699#define msgb_eq_l4(msg1, msgb2, len) msgb_eq_l4_data(msg1, msgb_l4(msg2), msgb_l4len(msg2))
700
701
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200702/* non inline functions to ease binding */
Harald Weltebd598e32011-08-16 23:26:52 +0200703
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200704uint8_t *msgb_data(const struct msgb *msg);
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200705
706void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size);
707void msgb_set_talloc_ctx(void *ctx) OSMO_DEPRECATED("Use msgb_talloc_ctx_init() instead");
Philipp Maierc5b47cc2017-10-10 16:53:21 +0200708int msgb_printf(struct msgb *msgb, const char *format, ...);
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200709
Maxcb02a392018-12-03 14:00:49 +0100710static inline const char *msgb_hexdump_l1(const struct msgb *msg)
711{
712 if (!msgb_l1(msg) || !(msgb_l1len(msg)))
713 return "[]";
714 return osmo_hexdump((const unsigned char *) msgb_l1(msg), msgb_l1len(msg));
715}
716
Harald Welte57217702018-02-09 02:01:48 +0100717static inline const char *msgb_hexdump_l2(const struct msgb *msg)
718{
719 if (!msgb_l2(msg) || !(msgb_l2len(msg)))
720 return "[]";
Harald Welteebd2b0f2018-02-09 10:32:03 +0100721 return osmo_hexdump((const unsigned char *) msgb_l2(msg), msgb_l2len(msg));
Harald Welte57217702018-02-09 02:01:48 +0100722}
723
724static inline const char *msgb_hexdump_l3(const struct msgb *msg)
725{
726 if (!msgb_l3(msg) || !(msgb_l3len(msg)))
727 return "[]";
Harald Welteebd2b0f2018-02-09 10:32:03 +0100728 return osmo_hexdump((const unsigned char*) msgb_l3(msg), msgb_l3len(msg));
Harald Welte57217702018-02-09 02:01:48 +0100729}
730
Maxcb02a392018-12-03 14:00:49 +0100731static inline const char *msgb_hexdump_l4(const struct msgb *msg)
732{
733 if (!msgb_l4(msg) || !(msgb_l4len(msg)))
734 return "[]";
735 return osmo_hexdump((const unsigned char*) msgb_l4(msg), msgb_l4len(msg));
736}
737
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200738/*! @} */