blob: 1e2b02376d5b1f2fa32e6b5931c5a02976dc6a65 [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 *
Harald Welteec8b4502010-02-20 20:34:29 +010016 */
17
18#include <stdint.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010019#include <osmocom/core/linuxlist.h>
20#include <osmocom/core/utils.h>
Max53777012014-06-04 19:07:41 +020021#include <osmocom/core/bits.h>
Neels Hofmeyrf45334b2016-09-16 00:15:56 +020022#include <osmocom/core/defs.h>
Harald Welteec8b4502010-02-20 20:34:29 +010023
Harald Welteba6988b2011-08-17 12:46:48 +020024/*! \defgroup msgb Message buffers
25 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020026 * \file msgb.h */
Harald Weltebd598e32011-08-16 23:26:52 +020027
Harald Welte652a7232010-07-22 21:55:24 +020028#define MSGB_DEBUG
29
Neels Hofmeyr87e45502017-06-20 00:17:59 +020030/*! Osmocom message buffer */
Harald Welteec8b4502010-02-20 20:34:29 +010031struct msgb {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020032 struct llist_head list; /*!< linked list header */
Harald Welteec8b4502010-02-20 20:34:29 +010033
Pablo Neira Ayuso29cbf612011-07-07 19:46:44 +020034
Harald Welteec8b4502010-02-20 20:34:29 +010035 /* Part of which TRX logical channel we were received / transmitted */
Harald Welte074c9f92010-04-30 14:29:11 +020036 /* FIXME: move them into the control buffer */
Harald Weltec5a0ded2011-07-18 16:59:27 +020037 union {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020038 void *dst; /*!< reference of origin/destination */
Harald Weltec5a0ded2011-07-18 16:59:27 +020039 struct gsm_bts_trx *trx;
40 };
Neels Hofmeyr87e45502017-06-20 00:17:59 +020041 struct gsm_lchan *lchan; /*!< logical channel */
Harald Welteec8b4502010-02-20 20:34:29 +010042
Neels Hofmeyr87e45502017-06-20 00:17:59 +020043 unsigned char *l1h; /*!< pointer to Layer1 header (if any) */
44 unsigned char *l2h; /*!< pointer to A-bis layer 2 header: OML, RSL(RLL), NS */
45 unsigned char *l3h; /*!< pointer to Layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */
46 unsigned char *l4h; /*!< pointer to layer 4 header */
Harald Welteec8b4502010-02-20 20:34:29 +010047
Neels Hofmeyr87e45502017-06-20 00:17:59 +020048 unsigned long cb[5]; /*!< control buffer */
Harald Welte074c9f92010-04-30 14:29:11 +020049
Neels Hofmeyr87e45502017-06-20 00:17:59 +020050 uint16_t data_len; /*!< length of underlying data array */
51 uint16_t len; /*!< length of bytes used in msgb */
Harald Welteec8b4502010-02-20 20:34:29 +010052
Neels Hofmeyr87e45502017-06-20 00:17:59 +020053 unsigned char *head; /*!< start of underlying memory buffer */
54 unsigned char *tail; /*!< end of message in buffer */
55 unsigned char *data; /*!< start of message in buffer */
56 unsigned char _data[0]; /*!< optional immediate data array */
Harald Welteec8b4502010-02-20 20:34:29 +010057};
58
Harald Welte179f3572019-03-18 18:38:47 +010059extern struct msgb *msgb_alloc_c(const void *ctx, uint16_t size, const char *name);
Harald Welteec8b4502010-02-20 20:34:29 +010060extern struct msgb *msgb_alloc(uint16_t size, const char *name);
61extern void msgb_free(struct msgb *m);
62extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
63extern struct msgb *msgb_dequeue(struct llist_head *queue);
64extern void msgb_reset(struct msgb *m);
Harald Welte972b5022012-09-08 19:58:59 +020065uint16_t msgb_length(const struct msgb *msg);
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +010066extern const char *msgb_hexdump(const struct msgb *msg);
Harald Welte4a62eda2019-03-18 18:27:00 +010067char *msgb_hexdump_buf(char *buf, size_t buf_len, const struct msgb *msg);
Harald Welte179f3572019-03-18 18:38:47 +010068char *msgb_hexdump_c(const void *ctx, const struct msgb *msg);
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +010069extern int msgb_resize_area(struct msgb *msg, uint8_t *area,
70 int old_size, int new_size);
71extern struct msgb *msgb_copy(const struct msgb *msg, const char *name);
Harald Welte179f3572019-03-18 18:38:47 +010072extern struct msgb *msgb_copy_c(const void *ctx, 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{
Philipp Maierd9d62102022-10-04 14:43:25 +0200147 OSMO_ASSERT(msgb->l1h);
Harald Weltefdd0a702010-03-01 22:30:51 +0100148 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{
Philipp Maierd9d62102022-10-04 14:43:25 +0200160 OSMO_ASSERT(msgb->l2h);
Harald Welteec8b4502010-02-20 20:34:29 +0100161 return msgb->tail - (uint8_t *)msgb_l2(msgb);
162}
163
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200164/*! determine length of L3 message
Harald Weltebd598e32011-08-16 23:26:52 +0200165 * \param[in] msgb message buffer
166 * \returns size of L3 message in bytes
167 *
168 * This function computes the number of bytes between the tail of the
169 * message and the layer 3 header.
170 */
Harald Welteec8b4502010-02-20 20:34:29 +0100171static inline unsigned int msgb_l3len(const struct msgb *msgb)
172{
Philipp Maierd9d62102022-10-04 14:43:25 +0200173 OSMO_ASSERT(msgb->l3h);
Harald Welteec8b4502010-02-20 20:34:29 +0100174 return msgb->tail - (uint8_t *)msgb_l3(msgb);
175}
176
Vadim Yanitskiye521ede2018-11-07 03:53:03 +0700177/*! determine length of L4 message
178 * \param[in] msgb message buffer
179 * \returns size of L4 message in bytes
180 *
181 * This function computes the number of bytes between the tail of the
182 * message and the layer 4 header.
183 */
184static inline unsigned int msgb_l4len(const struct msgb *msgb)
185{
Philipp Maierd9d62102022-10-04 14:43:25 +0200186 OSMO_ASSERT(msgb->l4h);
Philipp Maier724c1622022-10-05 10:12:49 +0200187 return msgb->tail - (uint8_t *)msgb_l4(msgb);
Vadim Yanitskiye521ede2018-11-07 03:53:03 +0700188}
189
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200190/*! determine the length of the header
Harald Weltebd598e32011-08-16 23:26:52 +0200191 * \param[in] msgb message buffer
192 * \returns number of bytes between start of buffer and start of msg
193 *
194 * This function computes the length difference between the underlying
195 * data buffer and the used section of the \a msgb.
196 */
Harald Welteec8b4502010-02-20 20:34:29 +0100197static inline unsigned int msgb_headlen(const struct msgb *msgb)
198{
199 return msgb->len - msgb->data_len;
200}
Harald Welte652a7232010-07-22 21:55:24 +0200201
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200202/*! determine how much tail room is left in msgb
Harald Weltebd598e32011-08-16 23:26:52 +0200203 * \param[in] msgb message buffer
204 * \returns number of bytes remaining at end of msgb
205 *
206 * This function computes the amount of octets left in the underlying
207 * data buffer after the end of the message.
208 */
Harald Welte652a7232010-07-22 21:55:24 +0200209static inline int msgb_tailroom(const struct msgb *msgb)
210{
211 return (msgb->head + msgb->data_len) - msgb->tail;
212}
213
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200214/*! determine the amount of headroom in msgb
Harald Weltebd598e32011-08-16 23:26:52 +0200215 * \param[in] msgb message buffer
216 * \returns number of bytes left ahead of message start in msgb
217 *
218 * This function computes the amount of bytes left in the underlying
219 * data buffer before the start of the actual message.
220 */
Harald Welte652a7232010-07-22 21:55:24 +0200221static inline int msgb_headroom(const struct msgb *msgb)
222{
223 return (msgb->data - msgb->head);
224}
225
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200226/*! append data to end of message buffer
Harald Weltebd598e32011-08-16 23:26:52 +0200227 * \param[in] msgb message buffer
228 * \param[in] len number of bytes to append to message
229 * \returns pointer to start of newly-appended data
230 *
231 * This function will move the \a tail pointer of the message buffer \a
232 * len bytes further, thus enlarging the message by \a len bytes.
233 *
234 * The return value is a pointer to start of the newly added section at
235 * the end of the message and can be used for actually filling/copying
236 * data into it.
237 */
Harald Welteec8b4502010-02-20 20:34:29 +0100238static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
239{
240 unsigned char *tmp = msgb->tail;
Vadim Yanitskiy85c78312022-02-02 01:34:30 +0600241 if (OSMO_UNLIKELY(msgb_tailroom(msgb) < (int) len))
Neels Hofmeyr63cb9492019-11-11 05:51:43 +0100242 MSGB_ABORT(msgb, "Not enough tailroom msgb_put"
243 " (allocated %u, head at %u, len %u, tailroom %u < want tailroom %u)\n",
244 msgb->data_len - sizeof(struct msgb),
245 msgb->head - msgb->_data,
246 msgb->len,
Harald Welte929d8872010-11-05 07:47:41 +0100247 msgb_tailroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100248 msgb->tail += len;
249 msgb->len += len;
250 return tmp;
251}
Harald Weltebd598e32011-08-16 23:26:52 +0200252
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200253/*! append a uint8 value to the end of the message
Harald Weltebd598e32011-08-16 23:26:52 +0200254 * \param[in] msgb message buffer
255 * \param[in] word unsigned 8bit byte to be appended
256 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100257static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
258{
259 uint8_t *space = msgb_put(msgb, 1);
260 space[0] = word & 0xFF;
261}
Harald Weltebd598e32011-08-16 23:26:52 +0200262
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200263/*! append a uint16 value to the end of the message
Harald Weltebd598e32011-08-16 23:26:52 +0200264 * \param[in] msgb message buffer
265 * \param[in] word unsigned 16bit byte to be appended
266 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100267static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
268{
269 uint8_t *space = msgb_put(msgb, 2);
Max53777012014-06-04 19:07:41 +0200270 osmo_store16be(word, space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100271}
Harald Weltebd598e32011-08-16 23:26:52 +0200272
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200273/*! append a uint32 value to the end of the message
Harald Weltebd598e32011-08-16 23:26:52 +0200274 * \param[in] msgb message buffer
275 * \param[in] word unsigned 32bit byte to be appended
276 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100277static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
278{
279 uint8_t *space = msgb_put(msgb, 4);
Max53777012014-06-04 19:07:41 +0200280 osmo_store32be(word, space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100281}
Harald Weltebd598e32011-08-16 23:26:52 +0200282
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200283/*! remove data from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200284 * \param[in] msgb message buffer
285 * \param[in] len number of bytes to remove from end
286 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100287static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
288{
Vadim Yanitskiy85c78312022-02-02 01:34:30 +0600289 if (OSMO_UNLIKELY(msgb_length(msgb) < len))
Harald Welte972b5022012-09-08 19:58:59 +0200290 MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n",
291 len, msgb_length(msgb));
292 msgb->tail -= len;
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100293 msgb->len -= len;
Pau Espin Pedrol9fa09122018-04-11 20:09:13 +0200294 return msgb->tail;
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100295}
Max53777012014-06-04 19:07:41 +0200296
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200297/*! remove uint8 from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200298 * \param[in] msgb message buffer
299 * \returns 8bit value taken from end of msgb
300 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100301static inline uint8_t msgb_get_u8(struct msgb *msgb)
302{
303 uint8_t *space = msgb_get(msgb, 1);
304 return space[0];
305}
Max53777012014-06-04 19:07:41 +0200306
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200307/*! remove uint16 from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200308 * \param[in] msgb message buffer
309 * \returns 16bit value taken from end of msgb
310 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100311static inline uint16_t msgb_get_u16(struct msgb *msgb)
312{
313 uint8_t *space = msgb_get(msgb, 2);
Max53777012014-06-04 19:07:41 +0200314 return osmo_load16be(space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100315}
Max53777012014-06-04 19:07:41 +0200316
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200317/*! remove uint32 from end of message
Harald Weltebd598e32011-08-16 23:26:52 +0200318 * \param[in] msgb message buffer
319 * \returns 32bit value taken from end of msgb
320 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100321static inline uint32_t msgb_get_u32(struct msgb *msgb)
322{
323 uint8_t *space = msgb_get(msgb, 4);
Max53777012014-06-04 19:07:41 +0200324 return osmo_load32be(space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100325}
Harald Weltebd598e32011-08-16 23:26:52 +0200326
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200327/*! prepend (push) some data to start of message
Harald Weltebd598e32011-08-16 23:26:52 +0200328 * \param[in] msgb message buffer
329 * \param[in] len number of bytes to pre-pend
330 * \returns pointer to newly added portion at start of \a msgb
331 *
332 * This function moves the \a data pointer of the \ref msgb further
333 * to the front (by \a len bytes), thereby enlarging the message by \a
334 * len bytes.
335 *
336 * The return value is a pointer to the newly added section in the
337 * beginning of the message. It can be used to fill/copy data into it.
338 */
Harald Welteec8b4502010-02-20 20:34:29 +0100339static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
340{
Vadim Yanitskiy85c78312022-02-02 01:34:30 +0600341 if (OSMO_UNLIKELY(msgb_headroom(msgb) < (int) len))
Neels Hofmeyr63cb9492019-11-11 05:51:43 +0100342 MSGB_ABORT(msgb, "Not enough headroom msgb_push"
343 " (allocated %u, head at %u < want headroom %u, len %u, tailroom %u)\n",
344 msgb->data_len - sizeof(struct msgb),
345 msgb->head - msgb->_data,
346 len,
347 msgb->len,
348 msgb_tailroom(msgb));
Harald Welteec8b4502010-02-20 20:34:29 +0100349 msgb->data -= len;
350 msgb->len += len;
351 return msgb->data;
352}
Max53777012014-06-04 19:07:41 +0200353
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200354/*! prepend a uint8 value to the head of the message
Vadim Yanitskiy914a8ec2019-03-26 00:20:35 +0700355 * \param[in] msg message buffer
Harald Welte8593f1f2016-11-11 19:26:51 +0100356 * \param[in] word unsigned 8bit byte to be prepended
357 */
358static inline void msgb_push_u8(struct msgb *msg, uint8_t word)
359{
360 uint8_t *space = msgb_push(msg, 1);
361 space[0] = word;
362}
363
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200364/*! prepend a uint16 value to the head of the message
Vadim Yanitskiy914a8ec2019-03-26 00:20:35 +0700365 * \param[in] msg message buffer
Harald Welte8593f1f2016-11-11 19:26:51 +0100366 * \param[in] word unsigned 16bit byte to be prepended
367 */
368static inline void msgb_push_u16(struct msgb *msg, uint16_t word)
369{
370 uint16_t *space = (uint16_t *) msgb_push(msg, 2);
371 osmo_store16be(word, space);
372}
373
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200374/*! prepend a uint32 value to the head of the message
Vadim Yanitskiy914a8ec2019-03-26 00:20:35 +0700375 * \param[in] msg message buffer
Harald Welte8593f1f2016-11-11 19:26:51 +0100376 * \param[in] word unsigned 32bit byte to be prepended
377 */
378static inline void msgb_push_u32(struct msgb *msg, uint32_t word)
379{
380 uint32_t *space = (uint32_t *) msgb_push(msg, 4);
381 osmo_store32be(word, space);
382}
383
Maxb27e6fe2019-01-21 14:19:59 +0100384static inline unsigned char *msgb_push_tl(struct msgb *msgb, uint8_t tag)
Max84fb5bb2018-11-08 13:23:59 +0100385{
386 uint8_t *data = msgb_push(msgb, 2);
387
388 data[0] = tag;
389 data[1] = msgb->len - 2;
390 return data;
391}
392
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200393/*! remove (pull) a header from the front of the message buffer
Harald Weltebd598e32011-08-16 23:26:52 +0200394 * \param[in] msgb message buffer
395 * \param[in] len number of octets to be pulled
396 * \returns pointer to new start of msgb
397 *
398 * This function moves the \a data pointer of the \ref msgb further back
399 * in the message, thereby shrinking the size of the message by \a len
400 * bytes.
401 */
Harald Welteec8b4502010-02-20 20:34:29 +0100402static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
403{
Vadim Yanitskiy85c78312022-02-02 01:34:30 +0600404 if (OSMO_UNLIKELY(msgb_length(msgb) < len))
Pau Espin Pedrolc62fc2d2018-04-11 20:14:08 +0200405 MSGB_ABORT(msgb, "msgb too small to pull %u (len %u)\n",
406 len, msgb_length(msgb));
Harald Welteec8b4502010-02-20 20:34:29 +0100407 msgb->len -= len;
408 return msgb->data += len;
409}
Harald Welteec8b4502010-02-20 20:34:29 +0100410
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200411/*! remove (pull) all headers in front of l3h from the message buffer.
Vadim Yanitskiy914a8ec2019-03-26 00:20:35 +0700412 * \param[in] msg message buffer with a valid l3h
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100413 * \returns pointer to new start of msgb (l3h)
414 *
415 * This function moves the \a data pointer of the \ref msgb further back
416 * in the message, thereby shrinking the size of the message.
417 * l1h and l2h will be cleared.
418 */
419static inline unsigned char *msgb_pull_to_l3(struct msgb *msg)
420{
421 unsigned char *ret = msgb_pull(msg, msg->l3h - msg->data);
422 msg->l1h = msg->l2h = NULL;
423 return ret;
424}
425
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200426/*! remove (pull) all headers in front of l2h from the message buffer.
Vadim Yanitskiy914a8ec2019-03-26 00:20:35 +0700427 * \param[in] msg message buffer with a valid l2h
Harald Welte59a9ebf2017-04-15 19:04:34 +0200428 * \returns pointer to new start of msgb (l2h)
429 *
430 * This function moves the \a data pointer of the \ref msgb further back
431 * in the message, thereby shrinking the size of the message.
432 * l1h will be cleared.
433 */
434static inline unsigned char *msgb_pull_to_l2(struct msgb *msg)
435{
436 unsigned char *ret = msgb_pull(msg, msg->l2h - msg->data);
437 msg->l1h = NULL;
438 return ret;
439}
440
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200441/*! remove uint8 from front of message
Harald Welte972b5022012-09-08 19:58:59 +0200442 * \param[in] msgb message buffer
443 * \returns 8bit value taken from end of msgb
444 */
445static inline uint8_t msgb_pull_u8(struct msgb *msgb)
446{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100447 uint8_t *space = msgb_pull(msgb, 1) - 1;
Harald Welte972b5022012-09-08 19:58:59 +0200448 return space[0];
449}
Max53777012014-06-04 19:07:41 +0200450
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200451/*! remove uint16 from front of message
Harald Welte972b5022012-09-08 19:58:59 +0200452 * \param[in] msgb message buffer
453 * \returns 16bit value taken from end of msgb
454 */
455static inline uint16_t msgb_pull_u16(struct msgb *msgb)
456{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100457 uint8_t *space = msgb_pull(msgb, 2) - 2;
Max53777012014-06-04 19:07:41 +0200458 return osmo_load16be(space);
Harald Welte972b5022012-09-08 19:58:59 +0200459}
Max53777012014-06-04 19:07:41 +0200460
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200461/*! remove uint32 from front of message
Harald Welte972b5022012-09-08 19:58:59 +0200462 * \param[in] msgb message buffer
463 * \returns 32bit value taken from end of msgb
464 */
465static inline uint32_t msgb_pull_u32(struct msgb *msgb)
466{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100467 uint8_t *space = msgb_pull(msgb, 4) - 4;
Max53777012014-06-04 19:07:41 +0200468 return osmo_load32be(space);
Harald Welte972b5022012-09-08 19:58:59 +0200469}
470
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200471/*! Increase headroom of empty msgb, reducing the tailroom
Harald Weltebd598e32011-08-16 23:26:52 +0200472 * \param[in] msg message buffer
473 * \param[in] len amount of extra octets to be reserved as headroom
474 *
475 * This function reserves some memory at the beginning of the underlying
476 * data buffer. The idea is to reserve space in case further headers
477 * have to be pushed to the \ref msgb during further processing.
478 *
479 * Calling this function leads to undefined reusults if it is called on
480 * a non-empty \ref msgb.
481 */
Harald Welteec8b4502010-02-20 20:34:29 +0100482static inline void msgb_reserve(struct msgb *msg, int len)
483{
484 msg->data += len;
485 msg->tail += len;
486}
487
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200488/*! Trim the msgb to a given absolute length
Harald Welteb0f91512012-01-14 21:53:41 +0100489 * \param[in] msg message buffer
490 * \param[in] len new total length of buffer
491 * \returns 0 in case of success, negative in case of error
492 */
493static inline int msgb_trim(struct msgb *msg, int len)
494{
Vadim Yanitskiy85c78312022-02-02 01:34:30 +0600495 if (OSMO_UNLIKELY(len < 0))
Jacob Erlbeckff42b262015-11-27 13:26:19 +0100496 MSGB_ABORT(msg, "Negative length is not allowed\n");
Vadim Yanitskiy85c78312022-02-02 01:34:30 +0600497 if (OSMO_UNLIKELY(len > msg->data_len))
Harald Welteb0f91512012-01-14 21:53:41 +0100498 return -1;
499
Harald Welte3068d572012-01-14 22:07:59 +0100500 msg->len = len;
501 msg->tail = msg->data + len;
Harald Welteb0f91512012-01-14 21:53:41 +0100502
503 return 0;
504}
505
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200506/*! Trim the msgb to a given layer3 length
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100507 * \param[in] msg message buffer
Harald Welteb0f91512012-01-14 21:53:41 +0100508 * \param[in] l3len new layer3 length
509 * \returns 0 in case of success, negative in case of error
510 */
511static inline int msgb_l3trim(struct msgb *msg, int l3len)
512{
513 return msgb_trim(msg, (msg->l3h - msg->data) + l3len);
514}
515
Harald Welte179f3572019-03-18 18:38:47 +0100516/*! Allocate message buffer with specified headroom from specified talloc context.
517 * \param[in] ctx talloc context from which to allocate
518 * \param[in] size size in bytes, including headroom
519 * \param[in] headroom headroom in bytes
520 * \param[in] name human-readable name
521 * \returns allocated message buffer with specified headroom
522 *
523 * This function is a convenience wrapper around \ref msgb_alloc
524 * followed by \ref msgb_reserve in order to create a new \ref msgb with
525 * user-specified amount of headroom.
526 */
Pau Espin Pedrolaf40b0b2021-06-10 14:33:46 +0200527static inline struct msgb *msgb_alloc_headroom_c(const void *ctx, uint16_t size, uint16_t headroom,
Harald Welte179f3572019-03-18 18:38:47 +0100528 const char *name)
529{
Pau Espin Pedrolec4b6272019-09-26 17:46:27 +0200530 osmo_static_assert(size >= headroom, headroom_bigger);
Harald Welte179f3572019-03-18 18:38:47 +0100531
532 struct msgb *msg = msgb_alloc_c(ctx, size, name);
Vadim Yanitskiy85c78312022-02-02 01:34:30 +0600533 if (OSMO_LIKELY(msg))
Harald Welte179f3572019-03-18 18:38:47 +0100534 msgb_reserve(msg, headroom);
535 return msg;
536}
537
538
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200539/*! Allocate message buffer with specified headroom
Harald Weltebd598e32011-08-16 23:26:52 +0200540 * \param[in] size size in bytes, including headroom
541 * \param[in] headroom headroom in bytes
542 * \param[in] name human-readable name
543 * \returns allocated message buffer with specified headroom
544 *
545 * This function is a convenience wrapper around \ref msgb_alloc
546 * followed by \ref msgb_reserve in order to create a new \ref msgb with
547 * user-specified amount of headroom.
548 */
Pau Espin Pedrolaf40b0b2021-06-10 14:33:46 +0200549static inline struct msgb *msgb_alloc_headroom(uint16_t size, uint16_t headroom,
Harald Welteec8b4502010-02-20 20:34:29 +0100550 const char *name)
551{
Pau Espin Pedrolec4b6272019-09-26 17:46:27 +0200552 osmo_static_assert(size >= headroom, headroom_bigger);
Holger Hans Peter Freyther5853f082011-01-16 17:38:22 +0100553
Harald Welteec8b4502010-02-20 20:34:29 +0100554 struct msgb *msg = msgb_alloc(size, name);
Vadim Yanitskiy85c78312022-02-02 01:34:30 +0600555 if (OSMO_LIKELY(msg))
Harald Welteec8b4502010-02-20 20:34:29 +0100556 msgb_reserve(msg, headroom);
557 return msg;
558}
559
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200560/*! Check a message buffer for consistency
Jacob Erlbeck8db11342015-11-27 13:26:15 +0100561 * \param[in] msg message buffer
562 * \returns 0 (false) if inconsistent, != 0 (true) otherwise
563 */
564static inline int msgb_test_invariant(const struct msgb *msg)
565{
566 const unsigned char *lbound;
567 if (!msg || !msg->data || !msg->tail ||
568 (msg->data + msg->len != msg->tail) ||
569 (msg->data < msg->head) ||
570 (msg->tail > msg->head + msg->data_len))
571 return 0;
572
573 lbound = msg->head;
574
575 if (msg->l1h) {
576 if (msg->l1h < lbound)
577 return 0;
578 lbound = msg->l1h;
579 }
580 if (msg->l2h) {
581 if (msg->l2h < lbound)
582 return 0;
583 lbound = msg->l2h;
584 }
585 if (msg->l3h) {
586 if (msg->l3h < lbound)
587 return 0;
588 lbound = msg->l3h;
589 }
590 if (msg->l4h) {
591 if (msg->l4h < lbound)
592 return 0;
593 lbound = msg->l4h;
594 }
595
596 return lbound <= msg->head + msg->data_len;
597}
598
Maxdb038252018-12-03 14:14:44 +0100599
600/* msgb data comparison helpers */
601
602/*! Compare: check data in msgb against given data
603 * \param[in] msg message buffer
604 * \param[in] data expected data
605 * \param[in] len length of data
606 * \returns boolean indicating whether msgb content is equal to the given data
607 */
608#define msgb_eq_data(msg, data, len) \
609 _msgb_eq(__FILE__, __LINE__, __func__, 0, msg, data, len, false)
610
611/*! Compare: check L1 data in msgb against given data
612 * \param[in] msg message buffer
613 * \param[in] data expected L1 data
614 * \param[in] len length of data
615 * \returns boolean indicating whether msgb L1 content is equal to the given data
616 */
617#define msgb_eq_l1_data(msg, data, len) \
618 _msgb_eq(__FILE__, __LINE__, __func__, 1, msg, data, len, false)
619
620/*! Compare: check L2 data in msgb against given data
621 * \param[in] msg message buffer
622 * \param[in] data expected L2 data
623 * \param[in] len length of data
624 * \returns boolean indicating whether msgb L2 content is equal to the given data
625 */
626#define msgb_eq_l2_data(msg, data, len) \
627 _msgb_eq(__FILE__, __LINE__, __func__, 2, msg, data, len, false)
628
629/*! Compare: check L3 data in msgb against given data
630 * \param[in] msg message buffer
631 * \param[in] data expected L3 data
632 * \param[in] len length of data
633 * \returns boolean indicating whether msgb L3 content is equal to the given data
634 */
635#define msgb_eq_l3_data(msg, data, len) \
636 _msgb_eq(__FILE__, __LINE__, __func__, 3, msg, data, len, false)
637
638/*! Compare: check L4 data in msgb against given data
639 * \param[in] msg message buffer
640 * \param[in] data expected L4 data
641 * \param[in] len length of data
642 * \returns boolean indicating whether msgb L4 content is equal to the given data
643 */
644#define msgb_eq_l4_data(msg, data, len) \
645 _msgb_eq(__FILE__, __LINE__, __func__, 4, msg, data, len, false)
646
647
648/* msgb test/debug helpers */
649
650/*! Compare and print: check data in msgb against given data and print errors if any
651 * \param[in] msg message buffer
652 * \param[in] data expected data
653 * \param[in] len length of data
654 * \returns boolean indicating whether msgb content is equal to the given data
655 */
656#define msgb_eq_data_print(msg, data, len) \
657 _msgb_eq(__FILE__, __LINE__, __func__, 0, msg, data, len, true)
658
659/*! Compare and print: check L1 data in msgb against given data and print errors if any
660 * \param[in] msg message buffer
661 * \param[in] data expected L1 data
662 * \param[in] len length of data
663 * \returns boolean indicating whether msgb L1 content is equal to the given data
664 */
665#define msgb_eq_l1_data_print(msg, data, len) \
666 _msgb_eq(__FILE__, __LINE__, __func__, 1, msg, data, len, true)
667
668/*! Compare and print: check L2 data in msgb against given data and print errors if any
669 * \param[in] msg message buffer
670 * \param[in] data expected L2 data
671 * \param[in] len length of data
672 * \returns boolean indicating whether msgb L2 content is equal to the given data
673 */
674#define msgb_eq_l2_data_print(msg, data, len) \
675 _msgb_eq(__FILE__, __LINE__, __func__, 2, msg, data, len, true)
676
677/*! Compare and print: check L3 data in msgb against given data and print errors if any
678 * \param[in] msg message buffer
679 * \param[in] data expected L3 data
680 * \param[in] len length of data
681 * \returns boolean indicating whether msgb L3 content is equal to the given data
682 */
683#define msgb_eq_l3_data_print(msg, data, len) \
684 _msgb_eq(__FILE__, __LINE__, __func__, 3, msg, data, len, true)
685
686
687/*! Compare and print: check L4 data in msgb against given data and print errors if any
688 * \param[in] msg message buffer
689 * \param[in] data expected L4 data
690 * \param[in] len length of data
691 * \returns boolean indicating whether msgb L4 content is equal to the given data
692 */
693#define msgb_eq_l4_data_print(msg, data, len) \
694 _msgb_eq(__FILE__, __LINE__, __func__, 4, msg, data, len, true)
695
696bool _msgb_eq(const char *file, size_t line, const char *func, uint8_t level,
697 const struct msgb *msg, const uint8_t *data, size_t len, bool print);
698
699
700/* msgb data comparison */
701
702/*! Compare msgbs
703 * \param[in] msg1 message buffer
704 * \param[in] msg2 reference message buffer
705 * \returns boolean indicating whether msgb content is equal
706 */
Vadim Yanitskiyba426e32019-03-25 23:41:07 +0700707#define msgb_eq(msg1, msg2) msgb_eq_data(msg1, msgb_data(msg2), msgb_length(msg2))
Maxdb038252018-12-03 14:14:44 +0100708
709/*! Compare msgbs L1 content
710 * \param[in] msg1 message buffer
711 * \param[in] msg2 reference message buffer
712 * \returns boolean indicating whether msgb L1 content is equal
713 */
Vadim Yanitskiyba426e32019-03-25 23:41:07 +0700714#define msgb_eq_l1(msg1, msg2) msgb_eq_l1_data(msg1, msgb_l1(msg2), msgb_l1len(msg2))
Maxdb038252018-12-03 14:14:44 +0100715
716/*! Compare msgbs L2 content
717 * \param[in] msg1 message buffer
718 * \param[in] msg2 reference message buffer
719 * \returns boolean indicating whether msgb L2 content is equal
720 */
Vadim Yanitskiyba426e32019-03-25 23:41:07 +0700721#define msgb_eq_l2(msg1, msg2) msgb_eq_l2_data(msg1, msgb_l2(msg2), msgb_l2len(msg2))
Maxdb038252018-12-03 14:14:44 +0100722
723/*! Compare msgbs L3 content
724 * \param[in] msg1 message buffer
725 * \param[in] msg2 reference message buffer
726 * \returns boolean indicating whether msgb L3 content is equal
727 */
Vadim Yanitskiyba426e32019-03-25 23:41:07 +0700728#define msgb_eq_l3(msg1, msg2) msgb_eq_l3_data(msg1, msgb_l3(msg2), msgb_l3len(msg2))
Maxdb038252018-12-03 14:14:44 +0100729
730/*! Compare msgbs L4 content
731 * \param[in] msg1 message buffer
732 * \param[in] msg2 reference message buffer
733 * \returns boolean indicating whether msgb L4 content is equal
734 */
Vadim Yanitskiyba426e32019-03-25 23:41:07 +0700735#define msgb_eq_l4(msg1, msg2) msgb_eq_l4_data(msg1, msgb_l4(msg2), msgb_l4len(msg2))
Maxdb038252018-12-03 14:14:44 +0100736
737
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200738/* non inline functions to ease binding */
Harald Weltebd598e32011-08-16 23:26:52 +0200739
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200740uint8_t *msgb_data(const struct msgb *msg);
Neels Hofmeyrf45334b2016-09-16 00:15:56 +0200741
742void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size);
743void msgb_set_talloc_ctx(void *ctx) OSMO_DEPRECATED("Use msgb_talloc_ctx_init() instead");
Philipp Maierc5b47cc2017-10-10 16:53:21 +0200744int msgb_printf(struct msgb *msgb, const char *format, ...);
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200745
Maxcb02a392018-12-03 14:00:49 +0100746static inline const char *msgb_hexdump_l1(const struct msgb *msg)
747{
748 if (!msgb_l1(msg) || !(msgb_l1len(msg)))
749 return "[]";
750 return osmo_hexdump((const unsigned char *) msgb_l1(msg), msgb_l1len(msg));
751}
752
Harald Welte57217702018-02-09 02:01:48 +0100753static inline const char *msgb_hexdump_l2(const struct msgb *msg)
754{
755 if (!msgb_l2(msg) || !(msgb_l2len(msg)))
756 return "[]";
Harald Welteebd2b0f2018-02-09 10:32:03 +0100757 return osmo_hexdump((const unsigned char *) msgb_l2(msg), msgb_l2len(msg));
Harald Welte57217702018-02-09 02:01:48 +0100758}
759
760static inline const char *msgb_hexdump_l3(const struct msgb *msg)
761{
762 if (!msgb_l3(msg) || !(msgb_l3len(msg)))
763 return "[]";
Harald Welteebd2b0f2018-02-09 10:32:03 +0100764 return osmo_hexdump((const unsigned char*) msgb_l3(msg), msgb_l3len(msg));
Harald Welte57217702018-02-09 02:01:48 +0100765}
766
Maxcb02a392018-12-03 14:00:49 +0100767static inline const char *msgb_hexdump_l4(const struct msgb *msg)
768{
769 if (!msgb_l4(msg) || !(msgb_l4len(msg)))
770 return "[]";
771 return osmo_hexdump((const unsigned char*) msgb_l4(msg), msgb_l4len(msg));
772}
773
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200774/*! @} */