blob: 0c51ce26dbe7b6e3abc0932272833c9ab9e339ad [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);
Harald Welte4a62eda2019-03-18 18:27:00 +010070char *msgb_hexdump_buf(char *buf, size_t buf_len, const struct msgb *msg);
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +010071extern int msgb_resize_area(struct msgb *msg, uint8_t *area,
72 int old_size, int new_size);
73extern struct msgb *msgb_copy(const struct msgb *msg, const char *name);
Jacob Erlbeck8db11342015-11-27 13:26:15 +010074static int msgb_test_invariant(const struct msgb *msg) __attribute__((pure));
Harald Welteec8b4502010-02-20 20:34:29 +010075
Neels Hofmeyr3fad5d72017-11-20 14:15:03 +010076/*! Free all msgbs from a queue built with msgb_enqueue().
77 * \param[in] queue list head of a msgb queue.
78 */
79static inline void msgb_queue_free(struct llist_head *queue)
80{
81 struct msgb *msg;
82 while ((msg = msgb_dequeue(queue))) msgb_free(msg);
83}
84
Pau Espin Pedrol8ce6f482018-08-17 10:33:57 +020085/*! Enqueue message buffer to tail of a queue and increment queue size counter
86 * \param[in] queue linked list header of queue
87 * \param[in] msg message buffer to be added to the queue
88 * \param[in] count pointer to variable holding size of the queue
89 *
90 * The function will append the specified message buffer \a msg to the queue
91 * implemented by \ref llist_head \a queue using function \ref msgb_enqueue_count,
92 * then increment \a count
93 */
94static inline void msgb_enqueue_count(struct llist_head *queue, struct msgb *msg,
95 unsigned int *count)
96{
97 msgb_enqueue(queue, msg);
98 (*count)++;
99}
100
101/*! Dequeue message buffer from head of queue and decrement queue size counter
102 * \param[in] queue linked list header of queue
103 * \param[in] count pointer to variable holding size of the queue
104 * \returns message buffer (if any) or NULL if queue empty
105 *
106 * The function will remove the first message buffer from the queue
107 * implemented by \ref llist_head \a queue using function \ref msgb_enqueue_count,
108 * and decrement \a count, all if queue is not empty.
109 */
110static inline struct msgb *msgb_dequeue_count(struct llist_head *queue,
111 unsigned int *count)
112{
113 struct msgb *msg = msgb_dequeue(queue);
114 if (msg)
115 (*count)--;
116 return msg;
117}
118
Harald Welte652a7232010-07-22 21:55:24 +0200119#ifdef MSGB_DEBUG
Pablo Neira Ayuso83419342011-03-22 16:36:13 +0100120#include <osmocom/core/panic.h>
Harald Welte929d8872010-11-05 07:47:41 +0100121#define MSGB_ABORT(msg, fmt, args ...) do { \
Harald Weltedab02872010-11-09 13:42:26 +0100122 osmo_panic("msgb(%p): " fmt, msg, ## args); \
Harald Welte929d8872010-11-05 07:47:41 +0100123 } while(0)
124#else
125#define MSGB_ABORT(msg, fmt, args ...)
Harald Welte652a7232010-07-22 21:55:24 +0200126#endif
127
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200128/*! obtain L1 header of msgb */
Harald Weltefdd0a702010-03-01 22:30:51 +0100129#define msgb_l1(m) ((void *)(m->l1h))
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200130/*! obtain L2 header of msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100131#define msgb_l2(m) ((void *)(m->l2h))
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200132/*! obtain L3 header of msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100133#define msgb_l3(m) ((void *)(m->l3h))
Maxcb02a392018-12-03 14:00:49 +0100134/*! obtain L4 header of msgb */
135#define msgb_l4(m) ((void *)(m->l4h))
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200136/*! obtain SMS header of msgb */
Maxcb02a392018-12-03 14:00:49 +0100137#define msgb_sms(m) msgb_l4(m)
Harald Welteec8b4502010-02-20 20:34:29 +0100138
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200139/*! determine length of L1 message
Harald Weltebd598e32011-08-16 23:26:52 +0200140 * \param[in] msgb message buffer
141 * \returns size of L1 message in bytes
142 *
143 * This function computes the number of bytes between the tail of the
144 * message and the layer 1 header.
145 */
Harald Weltefdd0a702010-03-01 22:30:51 +0100146static inline unsigned int msgb_l1len(const struct msgb *msgb)
147{
148 return msgb->tail - (uint8_t *)msgb_l1(msgb);
149}
150
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200151/*! determine length of L2 message
Harald Weltebd598e32011-08-16 23:26:52 +0200152 * \param[in] msgb message buffer
153 * \returns size of L2 message in bytes
154 *
155 * This function computes the number of bytes between the tail of the
156 * message and the layer 2 header.
157 */
Harald Welteec8b4502010-02-20 20:34:29 +0100158static inline unsigned int msgb_l2len(const struct msgb *msgb)
159{
160 return msgb->tail - (uint8_t *)msgb_l2(msgb);
161}
162
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200163/*! determine length of L3 message
Harald Weltebd598e32011-08-16 23:26:52 +0200164 * \param[in] msgb message buffer
165 * \returns size of L3 message in bytes
166 *
167 * This function computes the number of bytes between the tail of the
168 * message and the layer 3 header.
169 */
Harald Welteec8b4502010-02-20 20:34:29 +0100170static inline unsigned int msgb_l3len(const struct msgb *msgb)
171{
172 return msgb->tail - (uint8_t *)msgb_l3(msgb);
173}
174
Vadim Yanitskiye521ede2018-11-07 03:53:03 +0700175/*! determine length of L4 message
176 * \param[in] msgb message buffer
177 * \returns size of L4 message in bytes
178 *
179 * This function computes the number of bytes between the tail of the
180 * message and the layer 4 header.
181 */
182static inline unsigned int msgb_l4len(const struct msgb *msgb)
183{
184 return msgb->tail - (uint8_t *)msgb_sms(msgb);
185}
186
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200187/*! determine the length of the header
Harald Weltebd598e32011-08-16 23:26:52 +0200188 * \param[in] msgb message buffer
189 * \returns number of bytes between start of buffer and start of msg
190 *
191 * This function computes the length difference between the underlying
192 * data buffer and the used section of the \a msgb.
193 */
Harald Welteec8b4502010-02-20 20:34:29 +0100194static inline unsigned int msgb_headlen(const struct msgb *msgb)
195{
196 return msgb->len - msgb->data_len;
197}
Harald Welte652a7232010-07-22 21:55:24 +0200198
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200199/*! determine how much tail room is left in msgb
Harald Weltebd598e32011-08-16 23:26:52 +0200200 * \param[in] msgb message buffer
201 * \returns number of bytes remaining at end of msgb
202 *
203 * This function computes the amount of octets left in the underlying
204 * data buffer after the end of the message.
205 */
Harald Welte652a7232010-07-22 21:55:24 +0200206static inline int msgb_tailroom(const struct msgb *msgb)
207{
208 return (msgb->head + msgb->data_len) - msgb->tail;
209}
210
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200211/*! determine the amount of headroom in msgb
Harald Weltebd598e32011-08-16 23:26:52 +0200212 * \param[in] msgb message buffer
213 * \returns number of bytes left ahead of message start in msgb
214 *
215 * This function computes the amount of bytes left in the underlying
216 * data buffer before the start of the actual message.
217 */
Harald Welte652a7232010-07-22 21:55:24 +0200218static inline int msgb_headroom(const struct msgb *msgb)
219{
220 return (msgb->data - msgb->head);
221}
222
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200223/*! append data to end of message buffer
Harald Weltebd598e32011-08-16 23:26:52 +0200224 * \param[in] msgb message buffer
225 * \param[in] len number of bytes to append to message
226 * \returns pointer to start of newly-appended data
227 *
228 * This function will move the \a tail pointer of the message buffer \a
229 * len bytes further, thus enlarging the message by \a len bytes.
230 *
231 * The return value is a pointer to start of the newly added section at
232 * the end of the message and can be used for actually filling/copying
233 * data into it.
234 */
Harald Welteec8b4502010-02-20 20:34:29 +0100235static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
236{
237 unsigned char *tmp = msgb->tail;
Harald Welte46a45a62010-11-09 13:41:48 +0100238 if (msgb_tailroom(msgb) < (int) len)
Neels Hofmeyr418ca582016-09-09 02:03:43 +0200239 MSGB_ABORT(msgb, "Not enough tailroom msgb_put (%u < %u)\n",
Harald Welte929d8872010-11-05 07:47:41 +0100240 msgb_tailroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100241 msgb->tail += len;
242 msgb->len += len;
243 return tmp;
244}
Harald Weltebd598e32011-08-16 23:26:52 +0200245
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200246/*! append a uint8 value to the end of the message
Harald Weltebd598e32011-08-16 23:26:52 +0200247 * \param[in] msgb message buffer
248 * \param[in] word unsigned 8bit byte to be appended
249 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100250static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
251{
252 uint8_t *space = msgb_put(msgb, 1);
253 space[0] = word & 0xFF;
254}
Harald Weltebd598e32011-08-16 23:26:52 +0200255
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200256/*! append a uint16 value to the end of the message
Harald Weltebd598e32011-08-16 23:26:52 +0200257 * \param[in] msgb message buffer
258 * \param[in] word unsigned 16bit byte to be appended
259 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100260static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
261{
262 uint8_t *space = msgb_put(msgb, 2);
Max53777012014-06-04 19:07:41 +0200263 osmo_store16be(word, space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100264}
Harald Weltebd598e32011-08-16 23:26:52 +0200265
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200266/*! append a uint32 value to the end of the message
Harald Weltebd598e32011-08-16 23:26:52 +0200267 * \param[in] msgb message buffer
268 * \param[in] word unsigned 32bit byte to be appended
269 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100270static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
271{
272 uint8_t *space = msgb_put(msgb, 4);
Max53777012014-06-04 19:07:41 +0200273 osmo_store32be(word, space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100274}
Harald Weltebd598e32011-08-16 23:26:52 +0200275
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200276/*! remove data from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200277 * \param[in] msgb message buffer
278 * \param[in] len number of bytes to remove from end
279 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100280static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
281{
Harald Welte972b5022012-09-08 19:58:59 +0200282 if (msgb_length(msgb) < len)
283 MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n",
284 len, msgb_length(msgb));
285 msgb->tail -= len;
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100286 msgb->len -= len;
Pau Espin Pedrol9fa09122018-04-11 20:09:13 +0200287 return msgb->tail;
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100288}
Max53777012014-06-04 19:07:41 +0200289
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200290/*! remove uint8 from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200291 * \param[in] msgb message buffer
292 * \returns 8bit value taken from end of msgb
293 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100294static inline uint8_t msgb_get_u8(struct msgb *msgb)
295{
296 uint8_t *space = msgb_get(msgb, 1);
297 return space[0];
298}
Max53777012014-06-04 19:07:41 +0200299
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200300/*! remove uint16 from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200301 * \param[in] msgb message buffer
302 * \returns 16bit value taken from end of msgb
303 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100304static inline uint16_t msgb_get_u16(struct msgb *msgb)
305{
306 uint8_t *space = msgb_get(msgb, 2);
Max53777012014-06-04 19:07:41 +0200307 return osmo_load16be(space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100308}
Max53777012014-06-04 19:07:41 +0200309
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200310/*! remove uint32 from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200311 * \param[in] msgb message buffer
312 * \returns 32bit value taken from end of msgb
313 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100314static inline uint32_t msgb_get_u32(struct msgb *msgb)
315{
316 uint8_t *space = msgb_get(msgb, 4);
Max53777012014-06-04 19:07:41 +0200317 return osmo_load32be(space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100318}
Harald Weltebd598e32011-08-16 23:26:52 +0200319
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200320/*! prepend (push) some data to start of message
Harald Weltebd598e32011-08-16 23:26:52 +0200321 * \param[in] msgb message buffer
322 * \param[in] len number of bytes to pre-pend
323 * \returns pointer to newly added portion at start of \a msgb
324 *
325 * This function moves the \a data pointer of the \ref msgb further
326 * to the front (by \a len bytes), thereby enlarging the message by \a
327 * len bytes.
328 *
329 * The return value is a pointer to the newly added section in the
330 * beginning of the message. It can be used to fill/copy data into it.
331 */
Harald Welteec8b4502010-02-20 20:34:29 +0100332static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
333{
Harald Welte46a45a62010-11-09 13:41:48 +0100334 if (msgb_headroom(msgb) < (int) len)
Harald Welte929d8872010-11-05 07:47:41 +0100335 MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
336 msgb_headroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100337 msgb->data -= len;
338 msgb->len += len;
339 return msgb->data;
340}
Max53777012014-06-04 19:07:41 +0200341
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200342/*! prepend a uint8 value to the head of the message
Vadim Yanitskiy914a8ec2019-03-26 00:20:35 +0700343 * \param[in] msg message buffer
Harald Welte8593f1f2016-11-11 19:26:51 +0100344 * \param[in] word unsigned 8bit byte to be prepended
345 */
346static inline void msgb_push_u8(struct msgb *msg, uint8_t word)
347{
348 uint8_t *space = msgb_push(msg, 1);
349 space[0] = word;
350}
351
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200352/*! prepend a uint16 value to the head of the message
Vadim Yanitskiy914a8ec2019-03-26 00:20:35 +0700353 * \param[in] msg message buffer
Harald Welte8593f1f2016-11-11 19:26:51 +0100354 * \param[in] word unsigned 16bit byte to be prepended
355 */
356static inline void msgb_push_u16(struct msgb *msg, uint16_t word)
357{
358 uint16_t *space = (uint16_t *) msgb_push(msg, 2);
359 osmo_store16be(word, space);
360}
361
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200362/*! prepend a uint32 value to the head of the message
Vadim Yanitskiy914a8ec2019-03-26 00:20:35 +0700363 * \param[in] msg message buffer
Harald Welte8593f1f2016-11-11 19:26:51 +0100364 * \param[in] word unsigned 32bit byte to be prepended
365 */
366static inline void msgb_push_u32(struct msgb *msg, uint32_t word)
367{
368 uint32_t *space = (uint32_t *) msgb_push(msg, 4);
369 osmo_store32be(word, space);
370}
371
Maxb27e6fe2019-01-21 14:19:59 +0100372static inline unsigned char *msgb_push_tl(struct msgb *msgb, uint8_t tag)
Max84fb5bb2018-11-08 13:23:59 +0100373{
374 uint8_t *data = msgb_push(msgb, 2);
375
376 data[0] = tag;
377 data[1] = msgb->len - 2;
378 return data;
379}
380
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200381/*! remove (pull) a header from the front of the message buffer
Harald Weltebd598e32011-08-16 23:26:52 +0200382 * \param[in] msgb message buffer
383 * \param[in] len number of octets to be pulled
384 * \returns pointer to new start of msgb
385 *
386 * This function moves the \a data pointer of the \ref msgb further back
387 * in the message, thereby shrinking the size of the message by \a len
388 * bytes.
389 */
Harald Welteec8b4502010-02-20 20:34:29 +0100390static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
391{
Pau Espin Pedrolc62fc2d2018-04-11 20:14:08 +0200392 if (msgb_length(msgb) < len)
393 MSGB_ABORT(msgb, "msgb too small to pull %u (len %u)\n",
394 len, msgb_length(msgb));
Harald Welteec8b4502010-02-20 20:34:29 +0100395 msgb->len -= len;
396 return msgb->data += len;
397}
Harald Welteec8b4502010-02-20 20:34:29 +0100398
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200399/*! remove (pull) all headers in front of l3h from the message buffer.
Vadim Yanitskiy914a8ec2019-03-26 00:20:35 +0700400 * \param[in] msg message buffer with a valid l3h
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100401 * \returns pointer to new start of msgb (l3h)
402 *
403 * This function moves the \a data pointer of the \ref msgb further back
404 * in the message, thereby shrinking the size of the message.
405 * l1h and l2h will be cleared.
406 */
407static inline unsigned char *msgb_pull_to_l3(struct msgb *msg)
408{
409 unsigned char *ret = msgb_pull(msg, msg->l3h - msg->data);
410 msg->l1h = msg->l2h = NULL;
411 return ret;
412}
413
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200414/*! remove (pull) all headers in front of l2h from the message buffer.
Vadim Yanitskiy914a8ec2019-03-26 00:20:35 +0700415 * \param[in] msg message buffer with a valid l2h
Harald Welte59a9ebf2017-04-15 19:04:34 +0200416 * \returns pointer to new start of msgb (l2h)
417 *
418 * This function moves the \a data pointer of the \ref msgb further back
419 * in the message, thereby shrinking the size of the message.
420 * l1h will be cleared.
421 */
422static inline unsigned char *msgb_pull_to_l2(struct msgb *msg)
423{
424 unsigned char *ret = msgb_pull(msg, msg->l2h - msg->data);
425 msg->l1h = NULL;
426 return ret;
427}
428
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200429/*! remove uint8 from front of message
Harald Welte972b5022012-09-08 19:58:59 +0200430 * \param[in] msgb message buffer
431 * \returns 8bit value taken from end of msgb
432 */
433static inline uint8_t msgb_pull_u8(struct msgb *msgb)
434{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100435 uint8_t *space = msgb_pull(msgb, 1) - 1;
Harald Welte972b5022012-09-08 19:58:59 +0200436 return space[0];
437}
Max53777012014-06-04 19:07:41 +0200438
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200439/*! remove uint16 from front of message
Harald Welte972b5022012-09-08 19:58:59 +0200440 * \param[in] msgb message buffer
441 * \returns 16bit value taken from end of msgb
442 */
443static inline uint16_t msgb_pull_u16(struct msgb *msgb)
444{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100445 uint8_t *space = msgb_pull(msgb, 2) - 2;
Max53777012014-06-04 19:07:41 +0200446 return osmo_load16be(space);
Harald Welte972b5022012-09-08 19:58:59 +0200447}
Max53777012014-06-04 19:07:41 +0200448
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200449/*! remove uint32 from front of message
Harald Welte972b5022012-09-08 19:58:59 +0200450 * \param[in] msgb message buffer
451 * \returns 32bit value taken from end of msgb
452 */
453static inline uint32_t msgb_pull_u32(struct msgb *msgb)
454{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100455 uint8_t *space = msgb_pull(msgb, 4) - 4;
Max53777012014-06-04 19:07:41 +0200456 return osmo_load32be(space);
Harald Welte972b5022012-09-08 19:58:59 +0200457}
458
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200459/*! Increase headroom of empty msgb, reducing the tailroom
Harald Weltebd598e32011-08-16 23:26:52 +0200460 * \param[in] msg message buffer
461 * \param[in] len amount of extra octets to be reserved as headroom
462 *
463 * This function reserves some memory at the beginning of the underlying
464 * data buffer. The idea is to reserve space in case further headers
465 * have to be pushed to the \ref msgb during further processing.
466 *
467 * Calling this function leads to undefined reusults if it is called on
468 * a non-empty \ref msgb.
469 */
Harald Welteec8b4502010-02-20 20:34:29 +0100470static inline void msgb_reserve(struct msgb *msg, int len)
471{
472 msg->data += len;
473 msg->tail += len;
474}
475
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200476/*! Trim the msgb to a given absolute length
Harald Welteb0f91512012-01-14 21:53:41 +0100477 * \param[in] msg message buffer
478 * \param[in] len new total length of buffer
479 * \returns 0 in case of success, negative in case of error
480 */
481static inline int msgb_trim(struct msgb *msg, int len)
482{
Jacob Erlbeckff42b262015-11-27 13:26:19 +0100483 if (len < 0)
484 MSGB_ABORT(msg, "Negative length is not allowed\n");
Harald Welte3068d572012-01-14 22:07:59 +0100485 if (len > msg->data_len)
Harald Welteb0f91512012-01-14 21:53:41 +0100486 return -1;
487
Harald Welte3068d572012-01-14 22:07:59 +0100488 msg->len = len;
489 msg->tail = msg->data + len;
Harald Welteb0f91512012-01-14 21:53:41 +0100490
491 return 0;
492}
493
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200494/*! Trim the msgb to a given layer3 length
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100495 * \param[in] msg message buffer
Harald Welteb0f91512012-01-14 21:53:41 +0100496 * \param[in] l3len new layer3 length
497 * \returns 0 in case of success, negative in case of error
498 */
499static inline int msgb_l3trim(struct msgb *msg, int l3len)
500{
501 return msgb_trim(msg, (msg->l3h - msg->data) + l3len);
502}
503
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200504/*! Allocate message buffer with specified headroom
Harald Weltebd598e32011-08-16 23:26:52 +0200505 * \param[in] size size in bytes, including headroom
506 * \param[in] headroom headroom in bytes
507 * \param[in] name human-readable name
508 * \returns allocated message buffer with specified headroom
509 *
510 * This function is a convenience wrapper around \ref msgb_alloc
511 * followed by \ref msgb_reserve in order to create a new \ref msgb with
512 * user-specified amount of headroom.
513 */
Harald Welteec8b4502010-02-20 20:34:29 +0100514static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
515 const char *name)
516{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200517 osmo_static_assert(size > headroom, headroom_bigger);
Holger Hans Peter Freyther5853f082011-01-16 17:38:22 +0100518
Harald Welteec8b4502010-02-20 20:34:29 +0100519 struct msgb *msg = msgb_alloc(size, name);
520 if (msg)
521 msgb_reserve(msg, headroom);
522 return msg;
523}
524
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200525/*! Check a message buffer for consistency
Jacob Erlbeck8db11342015-11-27 13:26:15 +0100526 * \param[in] msg message buffer
527 * \returns 0 (false) if inconsistent, != 0 (true) otherwise
528 */
529static inline int msgb_test_invariant(const struct msgb *msg)
530{
531 const unsigned char *lbound;
532 if (!msg || !msg->data || !msg->tail ||
533 (msg->data + msg->len != msg->tail) ||
534 (msg->data < msg->head) ||
535 (msg->tail > msg->head + msg->data_len))
536 return 0;
537
538 lbound = msg->head;
539
540 if (msg->l1h) {
541 if (msg->l1h < lbound)
542 return 0;
543 lbound = msg->l1h;
544 }
545 if (msg->l2h) {
546 if (msg->l2h < lbound)
547 return 0;
548 lbound = msg->l2h;
549 }
550 if (msg->l3h) {
551 if (msg->l3h < lbound)
552 return 0;
553 lbound = msg->l3h;
554 }
555 if (msg->l4h) {
556 if (msg->l4h < lbound)
557 return 0;
558 lbound = msg->l4h;
559 }
560
561 return lbound <= msg->head + msg->data_len;
562}
563
Maxdb038252018-12-03 14:14:44 +0100564
565/* msgb data comparison helpers */
566
567/*! Compare: check data in msgb against given data
568 * \param[in] msg message buffer
569 * \param[in] data expected data
570 * \param[in] len length of data
571 * \returns boolean indicating whether msgb content is equal to the given data
572 */
573#define msgb_eq_data(msg, data, len) \
574 _msgb_eq(__FILE__, __LINE__, __func__, 0, msg, data, len, false)
575
576/*! Compare: check L1 data in msgb against given data
577 * \param[in] msg message buffer
578 * \param[in] data expected L1 data
579 * \param[in] len length of data
580 * \returns boolean indicating whether msgb L1 content is equal to the given data
581 */
582#define msgb_eq_l1_data(msg, data, len) \
583 _msgb_eq(__FILE__, __LINE__, __func__, 1, msg, data, len, false)
584
585/*! Compare: check L2 data in msgb against given data
586 * \param[in] msg message buffer
587 * \param[in] data expected L2 data
588 * \param[in] len length of data
589 * \returns boolean indicating whether msgb L2 content is equal to the given data
590 */
591#define msgb_eq_l2_data(msg, data, len) \
592 _msgb_eq(__FILE__, __LINE__, __func__, 2, msg, data, len, false)
593
594/*! Compare: check L3 data in msgb against given data
595 * \param[in] msg message buffer
596 * \param[in] data expected L3 data
597 * \param[in] len length of data
598 * \returns boolean indicating whether msgb L3 content is equal to the given data
599 */
600#define msgb_eq_l3_data(msg, data, len) \
601 _msgb_eq(__FILE__, __LINE__, __func__, 3, msg, data, len, false)
602
603/*! Compare: check L4 data in msgb against given data
604 * \param[in] msg message buffer
605 * \param[in] data expected L4 data
606 * \param[in] len length of data
607 * \returns boolean indicating whether msgb L4 content is equal to the given data
608 */
609#define msgb_eq_l4_data(msg, data, len) \
610 _msgb_eq(__FILE__, __LINE__, __func__, 4, msg, data, len, false)
611
612
613/* msgb test/debug helpers */
614
615/*! Compare and print: check data in msgb against given data and print errors if any
616 * \param[in] msg message buffer
617 * \param[in] data expected data
618 * \param[in] len length of data
619 * \returns boolean indicating whether msgb content is equal to the given data
620 */
621#define msgb_eq_data_print(msg, data, len) \
622 _msgb_eq(__FILE__, __LINE__, __func__, 0, msg, data, len, true)
623
624/*! Compare and print: check L1 data in msgb against given data and print errors if any
625 * \param[in] msg message buffer
626 * \param[in] data expected L1 data
627 * \param[in] len length of data
628 * \returns boolean indicating whether msgb L1 content is equal to the given data
629 */
630#define msgb_eq_l1_data_print(msg, data, len) \
631 _msgb_eq(__FILE__, __LINE__, __func__, 1, msg, data, len, true)
632
633/*! Compare and print: check L2 data in msgb against given data and print errors if any
634 * \param[in] msg message buffer
635 * \param[in] data expected L2 data
636 * \param[in] len length of data
637 * \returns boolean indicating whether msgb L2 content is equal to the given data
638 */
639#define msgb_eq_l2_data_print(msg, data, len) \
640 _msgb_eq(__FILE__, __LINE__, __func__, 2, msg, data, len, true)
641
642/*! Compare and print: check L3 data in msgb against given data and print errors if any
643 * \param[in] msg message buffer
644 * \param[in] data expected L3 data
645 * \param[in] len length of data
646 * \returns boolean indicating whether msgb L3 content is equal to the given data
647 */
648#define msgb_eq_l3_data_print(msg, data, len) \
649 _msgb_eq(__FILE__, __LINE__, __func__, 3, msg, data, len, true)
650
651
652/*! Compare and print: check L4 data in msgb against given data and print errors if any
653 * \param[in] msg message buffer
654 * \param[in] data expected L4 data
655 * \param[in] len length of data
656 * \returns boolean indicating whether msgb L4 content is equal to the given data
657 */
658#define msgb_eq_l4_data_print(msg, data, len) \
659 _msgb_eq(__FILE__, __LINE__, __func__, 4, msg, data, len, true)
660
661bool _msgb_eq(const char *file, size_t line, const char *func, uint8_t level,
662 const struct msgb *msg, const uint8_t *data, size_t len, bool print);
663
664
665/* msgb data comparison */
666
667/*! Compare msgbs
668 * \param[in] msg1 message buffer
669 * \param[in] msg2 reference message buffer
670 * \returns boolean indicating whether msgb content is equal
671 */
Vadim Yanitskiyba426e32019-03-25 23:41:07 +0700672#define msgb_eq(msg1, msg2) msgb_eq_data(msg1, msgb_data(msg2), msgb_length(msg2))
Maxdb038252018-12-03 14:14:44 +0100673
674/*! Compare msgbs L1 content
675 * \param[in] msg1 message buffer
676 * \param[in] msg2 reference message buffer
677 * \returns boolean indicating whether msgb L1 content is equal
678 */
Vadim Yanitskiyba426e32019-03-25 23:41:07 +0700679#define msgb_eq_l1(msg1, msg2) msgb_eq_l1_data(msg1, msgb_l1(msg2), msgb_l1len(msg2))
Maxdb038252018-12-03 14:14:44 +0100680
681/*! Compare msgbs L2 content
682 * \param[in] msg1 message buffer
683 * \param[in] msg2 reference message buffer
684 * \returns boolean indicating whether msgb L2 content is equal
685 */
Vadim Yanitskiyba426e32019-03-25 23:41:07 +0700686#define msgb_eq_l2(msg1, msg2) msgb_eq_l2_data(msg1, msgb_l2(msg2), msgb_l2len(msg2))
Maxdb038252018-12-03 14:14:44 +0100687
688/*! Compare msgbs L3 content
689 * \param[in] msg1 message buffer
690 * \param[in] msg2 reference message buffer
691 * \returns boolean indicating whether msgb L3 content is equal
692 */
Vadim Yanitskiyba426e32019-03-25 23:41:07 +0700693#define msgb_eq_l3(msg1, msg2) msgb_eq_l3_data(msg1, msgb_l3(msg2), msgb_l3len(msg2))
Maxdb038252018-12-03 14:14:44 +0100694
695/*! Compare msgbs L4 content
696 * \param[in] msg1 message buffer
697 * \param[in] msg2 reference message buffer
698 * \returns boolean indicating whether msgb L4 content is equal
699 */
Vadim Yanitskiyba426e32019-03-25 23:41:07 +0700700#define msgb_eq_l4(msg1, msg2) msgb_eq_l4_data(msg1, msgb_l4(msg2), msgb_l4len(msg2))
Maxdb038252018-12-03 14:14:44 +0100701
702
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200703/* non inline functions to ease binding */
Harald Weltebd598e32011-08-16 23:26:52 +0200704
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200705uint8_t *msgb_data(const struct msgb *msg);
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200706
707void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size);
708void msgb_set_talloc_ctx(void *ctx) OSMO_DEPRECATED("Use msgb_talloc_ctx_init() instead");
Philipp Maierc5b47cc2017-10-10 16:53:21 +0200709int msgb_printf(struct msgb *msgb, const char *format, ...);
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200710
Maxcb02a392018-12-03 14:00:49 +0100711static inline const char *msgb_hexdump_l1(const struct msgb *msg)
712{
713 if (!msgb_l1(msg) || !(msgb_l1len(msg)))
714 return "[]";
715 return osmo_hexdump((const unsigned char *) msgb_l1(msg), msgb_l1len(msg));
716}
717
Harald Welte57217702018-02-09 02:01:48 +0100718static inline const char *msgb_hexdump_l2(const struct msgb *msg)
719{
720 if (!msgb_l2(msg) || !(msgb_l2len(msg)))
721 return "[]";
Harald Welteebd2b0f2018-02-09 10:32:03 +0100722 return osmo_hexdump((const unsigned char *) msgb_l2(msg), msgb_l2len(msg));
Harald Welte57217702018-02-09 02:01:48 +0100723}
724
725static inline const char *msgb_hexdump_l3(const struct msgb *msg)
726{
727 if (!msgb_l3(msg) || !(msgb_l3len(msg)))
728 return "[]";
Harald Welteebd2b0f2018-02-09 10:32:03 +0100729 return osmo_hexdump((const unsigned char*) msgb_l3(msg), msgb_l3len(msg));
Harald Welte57217702018-02-09 02:01:48 +0100730}
731
Maxcb02a392018-12-03 14:00:49 +0100732static inline const char *msgb_hexdump_l4(const struct msgb *msg)
733{
734 if (!msgb_l4(msg) || !(msgb_l4len(msg)))
735 return "[]";
736 return osmo_hexdump((const unsigned char*) msgb_l4(msg), msgb_l4len(msg));
737}
738
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200739/*! @} */