2014-06-16 08:13:40 +00:00
|
|
|
#pragma once
|
2010-02-20 19:34:29 +00:00
|
|
|
|
|
|
|
/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2011-03-22 15:36:13 +00:00
|
|
|
#include <osmocom/core/linuxlist.h>
|
|
|
|
#include <osmocom/core/utils.h>
|
2014-06-04 17:07:41 +00:00
|
|
|
#include <osmocom/core/bits.h>
|
2016-09-15 22:15:56 +00:00
|
|
|
#include <osmocom/core/defs.h>
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2011-08-17 10:46:48 +00:00
|
|
|
/*! \defgroup msgb Message buffers
|
|
|
|
* @{
|
2017-06-20 02:35:06 +00:00
|
|
|
* \file msgb.h */
|
2011-08-16 21:26:52 +00:00
|
|
|
|
2010-07-22 19:55:24 +00:00
|
|
|
#define MSGB_DEBUG
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Osmocom message buffer */
|
2010-02-20 19:34:29 +00:00
|
|
|
struct msgb {
|
2017-06-19 22:17:59 +00:00
|
|
|
struct llist_head list; /*!< linked list header */
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2011-07-07 17:46:44 +00:00
|
|
|
|
2010-02-20 19:34:29 +00:00
|
|
|
/* Part of which TRX logical channel we were received / transmitted */
|
2010-04-30 12:29:11 +00:00
|
|
|
/* FIXME: move them into the control buffer */
|
2011-07-18 14:59:27 +00:00
|
|
|
union {
|
2017-06-19 22:17:59 +00:00
|
|
|
void *dst; /*!< reference of origin/destination */
|
2011-07-18 14:59:27 +00:00
|
|
|
struct gsm_bts_trx *trx;
|
|
|
|
};
|
2017-06-19 22:17:59 +00:00
|
|
|
struct gsm_lchan *lchan; /*!< logical channel */
|
2011-08-16 21:26:52 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
unsigned char *l1h; /*!< pointer to Layer1 header (if any) */
|
|
|
|
unsigned char *l2h; /*!< pointer to A-bis layer 2 header: OML, RSL(RLL), NS */
|
|
|
|
unsigned char *l3h; /*!< pointer to Layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */
|
|
|
|
unsigned char *l4h; /*!< pointer to layer 4 header */
|
2011-08-16 21:26:52 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
unsigned long cb[5]; /*!< control buffer */
|
2011-08-16 21:26:52 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
uint16_t data_len; /*!< length of underlying data array */
|
|
|
|
uint16_t len; /*!< length of bytes used in msgb */
|
2011-08-16 21:26:52 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
unsigned char *head; /*!< start of underlying memory buffer */
|
|
|
|
unsigned char *tail; /*!< end of message in buffer */
|
|
|
|
unsigned char *data; /*!< start of message in buffer */
|
|
|
|
unsigned char _data[0]; /*!< optional immediate data array */
|
2010-02-20 19:34:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern struct msgb *msgb_alloc(uint16_t size, const char *name);
|
|
|
|
extern void msgb_free(struct msgb *m);
|
|
|
|
extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
|
|
|
|
extern struct msgb *msgb_dequeue(struct llist_head *queue);
|
|
|
|
extern void msgb_reset(struct msgb *m);
|
2012-09-08 17:58:59 +00:00
|
|
|
uint16_t msgb_length(const struct msgb *msg);
|
2014-02-28 14:14:40 +00:00
|
|
|
extern const char *msgb_hexdump(const struct msgb *msg);
|
2015-11-27 12:26:13 +00:00
|
|
|
extern int msgb_resize_area(struct msgb *msg, uint8_t *area,
|
|
|
|
int old_size, int new_size);
|
|
|
|
extern struct msgb *msgb_copy(const struct msgb *msg, const char *name);
|
2015-11-27 12:26:15 +00:00
|
|
|
static int msgb_test_invariant(const struct msgb *msg) __attribute__((pure));
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2010-07-22 19:55:24 +00:00
|
|
|
#ifdef MSGB_DEBUG
|
2011-03-22 15:36:13 +00:00
|
|
|
#include <osmocom/core/panic.h>
|
2010-11-05 06:47:41 +00:00
|
|
|
#define MSGB_ABORT(msg, fmt, args ...) do { \
|
2010-11-09 12:42:26 +00:00
|
|
|
osmo_panic("msgb(%p): " fmt, msg, ## args); \
|
2010-11-05 06:47:41 +00:00
|
|
|
} while(0)
|
|
|
|
#else
|
|
|
|
#define MSGB_ABORT(msg, fmt, args ...)
|
2010-07-22 19:55:24 +00:00
|
|
|
#endif
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! obtain L1 header of msgb */
|
2010-03-01 21:30:51 +00:00
|
|
|
#define msgb_l1(m) ((void *)(m->l1h))
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! obtain L2 header of msgb */
|
2010-02-20 19:34:29 +00:00
|
|
|
#define msgb_l2(m) ((void *)(m->l2h))
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! obtain L3 header of msgb */
|
2010-02-20 19:34:29 +00:00
|
|
|
#define msgb_l3(m) ((void *)(m->l3h))
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! obtain SMS header of msgb */
|
2010-04-30 12:26:12 +00:00
|
|
|
#define msgb_sms(m) ((void *)(m->l4h))
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! determine length of L1 message
|
2011-08-16 21:26:52 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \returns size of L1 message in bytes
|
|
|
|
*
|
|
|
|
* This function computes the number of bytes between the tail of the
|
|
|
|
* message and the layer 1 header.
|
|
|
|
*/
|
2010-03-01 21:30:51 +00:00
|
|
|
static inline unsigned int msgb_l1len(const struct msgb *msgb)
|
|
|
|
{
|
|
|
|
return msgb->tail - (uint8_t *)msgb_l1(msgb);
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! determine length of L2 message
|
2011-08-16 21:26:52 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \returns size of L2 message in bytes
|
|
|
|
*
|
|
|
|
* This function computes the number of bytes between the tail of the
|
|
|
|
* message and the layer 2 header.
|
|
|
|
*/
|
2010-02-20 19:34:29 +00:00
|
|
|
static inline unsigned int msgb_l2len(const struct msgb *msgb)
|
|
|
|
{
|
|
|
|
return msgb->tail - (uint8_t *)msgb_l2(msgb);
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! determine length of L3 message
|
2011-08-16 21:26:52 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \returns size of L3 message in bytes
|
|
|
|
*
|
|
|
|
* This function computes the number of bytes between the tail of the
|
|
|
|
* message and the layer 3 header.
|
|
|
|
*/
|
2010-02-20 19:34:29 +00:00
|
|
|
static inline unsigned int msgb_l3len(const struct msgb *msgb)
|
|
|
|
{
|
|
|
|
return msgb->tail - (uint8_t *)msgb_l3(msgb);
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! determine the length of the header
|
2011-08-16 21:26:52 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \returns number of bytes between start of buffer and start of msg
|
|
|
|
*
|
|
|
|
* This function computes the length difference between the underlying
|
|
|
|
* data buffer and the used section of the \a msgb.
|
|
|
|
*/
|
2010-02-20 19:34:29 +00:00
|
|
|
static inline unsigned int msgb_headlen(const struct msgb *msgb)
|
|
|
|
{
|
|
|
|
return msgb->len - msgb->data_len;
|
|
|
|
}
|
2010-07-22 19:55:24 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! determine how much tail room is left in msgb
|
2011-08-16 21:26:52 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \returns number of bytes remaining at end of msgb
|
|
|
|
*
|
|
|
|
* This function computes the amount of octets left in the underlying
|
|
|
|
* data buffer after the end of the message.
|
|
|
|
*/
|
2010-07-22 19:55:24 +00:00
|
|
|
static inline int msgb_tailroom(const struct msgb *msgb)
|
|
|
|
{
|
|
|
|
return (msgb->head + msgb->data_len) - msgb->tail;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! determine the amount of headroom in msgb
|
2011-08-16 21:26:52 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \returns number of bytes left ahead of message start in msgb
|
|
|
|
*
|
|
|
|
* This function computes the amount of bytes left in the underlying
|
|
|
|
* data buffer before the start of the actual message.
|
|
|
|
*/
|
2010-07-22 19:55:24 +00:00
|
|
|
static inline int msgb_headroom(const struct msgb *msgb)
|
|
|
|
{
|
|
|
|
return (msgb->data - msgb->head);
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! append data to end of message buffer
|
2011-08-16 21:26:52 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \param[in] len number of bytes to append to message
|
|
|
|
* \returns pointer to start of newly-appended data
|
|
|
|
*
|
|
|
|
* This function will move the \a tail pointer of the message buffer \a
|
|
|
|
* len bytes further, thus enlarging the message by \a len bytes.
|
|
|
|
*
|
|
|
|
* The return value is a pointer to start of the newly added section at
|
|
|
|
* the end of the message and can be used for actually filling/copying
|
|
|
|
* data into it.
|
|
|
|
*/
|
2010-02-20 19:34:29 +00:00
|
|
|
static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
|
|
|
|
{
|
|
|
|
unsigned char *tmp = msgb->tail;
|
2010-11-09 12:41:48 +00:00
|
|
|
if (msgb_tailroom(msgb) < (int) len)
|
2016-09-09 00:03:43 +00:00
|
|
|
MSGB_ABORT(msgb, "Not enough tailroom msgb_put (%u < %u)\n",
|
2010-11-05 06:47:41 +00:00
|
|
|
msgb_tailroom(msgb), len);
|
2010-02-20 19:34:29 +00:00
|
|
|
msgb->tail += len;
|
|
|
|
msgb->len += len;
|
|
|
|
return tmp;
|
|
|
|
}
|
2011-08-16 21:26:52 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! append a uint8 value to the end of the message
|
2011-08-16 21:26:52 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \param[in] word unsigned 8bit byte to be appended
|
|
|
|
*/
|
2010-03-07 17:03:41 +00:00
|
|
|
static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
|
|
|
|
{
|
|
|
|
uint8_t *space = msgb_put(msgb, 1);
|
|
|
|
space[0] = word & 0xFF;
|
|
|
|
}
|
2011-08-16 21:26:52 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! append a uint16 value to the end of the message
|
2011-08-16 21:26:52 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \param[in] word unsigned 16bit byte to be appended
|
|
|
|
*/
|
2010-03-07 17:03:41 +00:00
|
|
|
static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
|
|
|
|
{
|
|
|
|
uint8_t *space = msgb_put(msgb, 2);
|
2014-06-04 17:07:41 +00:00
|
|
|
osmo_store16be(word, space);
|
2010-03-07 17:03:41 +00:00
|
|
|
}
|
2011-08-16 21:26:52 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! append a uint32 value to the end of the message
|
2011-08-16 21:26:52 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \param[in] word unsigned 32bit byte to be appended
|
|
|
|
*/
|
2010-03-07 17:03:41 +00:00
|
|
|
static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
|
|
|
|
{
|
|
|
|
uint8_t *space = msgb_put(msgb, 4);
|
2014-06-04 17:07:41 +00:00
|
|
|
osmo_store32be(word, space);
|
2010-03-07 17:03:41 +00:00
|
|
|
}
|
2011-08-16 21:26:52 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! remove data from end of message
|
2011-08-16 21:26:52 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \param[in] len number of bytes to remove from end
|
|
|
|
*/
|
2010-03-07 17:03:41 +00:00
|
|
|
static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
|
|
|
|
{
|
2012-09-10 18:58:20 +00:00
|
|
|
unsigned char *tmp = msgb->tail - len;
|
2012-09-08 17:58:59 +00:00
|
|
|
if (msgb_length(msgb) < len)
|
|
|
|
MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n",
|
|
|
|
len, msgb_length(msgb));
|
|
|
|
msgb->tail -= len;
|
2010-03-07 17:03:41 +00:00
|
|
|
msgb->len -= len;
|
|
|
|
return tmp;
|
|
|
|
}
|
2014-06-04 17:07:41 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! remove uint8 from end of message
|
2011-08-16 21:26:52 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \returns 8bit value taken from end of msgb
|
|
|
|
*/
|
2010-03-07 17:03:41 +00:00
|
|
|
static inline uint8_t msgb_get_u8(struct msgb *msgb)
|
|
|
|
{
|
|
|
|
uint8_t *space = msgb_get(msgb, 1);
|
|
|
|
return space[0];
|
|
|
|
}
|
2014-06-04 17:07:41 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! remove uint16 from end of message
|
2011-08-16 21:26:52 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \returns 16bit value taken from end of msgb
|
|
|
|
*/
|
2010-03-07 17:03:41 +00:00
|
|
|
static inline uint16_t msgb_get_u16(struct msgb *msgb)
|
|
|
|
{
|
|
|
|
uint8_t *space = msgb_get(msgb, 2);
|
2014-06-04 17:07:41 +00:00
|
|
|
return osmo_load16be(space);
|
2010-03-07 17:03:41 +00:00
|
|
|
}
|
2014-06-04 17:07:41 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! remove uint32 from end of message
|
2011-08-16 21:26:52 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \returns 32bit value taken from end of msgb
|
|
|
|
*/
|
2010-03-07 17:03:41 +00:00
|
|
|
static inline uint32_t msgb_get_u32(struct msgb *msgb)
|
|
|
|
{
|
|
|
|
uint8_t *space = msgb_get(msgb, 4);
|
2014-06-04 17:07:41 +00:00
|
|
|
return osmo_load32be(space);
|
2010-03-07 17:03:41 +00:00
|
|
|
}
|
2011-08-16 21:26:52 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! prepend (push) some data to start of message
|
2011-08-16 21:26:52 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \param[in] len number of bytes to pre-pend
|
|
|
|
* \returns pointer to newly added portion at start of \a msgb
|
|
|
|
*
|
|
|
|
* This function moves the \a data pointer of the \ref msgb further
|
|
|
|
* to the front (by \a len bytes), thereby enlarging the message by \a
|
|
|
|
* len bytes.
|
|
|
|
*
|
|
|
|
* The return value is a pointer to the newly added section in the
|
|
|
|
* beginning of the message. It can be used to fill/copy data into it.
|
|
|
|
*/
|
2010-02-20 19:34:29 +00:00
|
|
|
static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
|
|
|
|
{
|
2010-11-09 12:41:48 +00:00
|
|
|
if (msgb_headroom(msgb) < (int) len)
|
2010-11-05 06:47:41 +00:00
|
|
|
MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
|
|
|
|
msgb_headroom(msgb), len);
|
2010-02-20 19:34:29 +00:00
|
|
|
msgb->data -= len;
|
|
|
|
msgb->len += len;
|
|
|
|
return msgb->data;
|
|
|
|
}
|
2014-06-04 17:07:41 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! prepend a uint8 value to the head of the message
|
2016-11-11 18:26:51 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \param[in] word unsigned 8bit byte to be prepended
|
|
|
|
*/
|
|
|
|
static inline void msgb_push_u8(struct msgb *msg, uint8_t word)
|
|
|
|
{
|
|
|
|
uint8_t *space = msgb_push(msg, 1);
|
|
|
|
space[0] = word;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! prepend a uint16 value to the head of the message
|
2016-11-11 18:26:51 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \param[in] word unsigned 16bit byte to be prepended
|
|
|
|
*/
|
|
|
|
static inline void msgb_push_u16(struct msgb *msg, uint16_t word)
|
|
|
|
{
|
|
|
|
uint16_t *space = (uint16_t *) msgb_push(msg, 2);
|
|
|
|
osmo_store16be(word, space);
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! prepend a uint32 value to the head of the message
|
2016-11-11 18:26:51 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \param[in] word unsigned 32bit byte to be prepended
|
|
|
|
*/
|
|
|
|
static inline void msgb_push_u32(struct msgb *msg, uint32_t word)
|
|
|
|
{
|
|
|
|
uint32_t *space = (uint32_t *) msgb_push(msg, 4);
|
|
|
|
osmo_store32be(word, space);
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! remove (pull) a header from the front of the message buffer
|
2011-08-16 21:26:52 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \param[in] len number of octets to be pulled
|
|
|
|
* \returns pointer to new start of msgb
|
|
|
|
*
|
|
|
|
* This function moves the \a data pointer of the \ref msgb further back
|
|
|
|
* in the message, thereby shrinking the size of the message by \a len
|
|
|
|
* bytes.
|
|
|
|
*/
|
2010-02-20 19:34:29 +00:00
|
|
|
static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
|
|
|
|
{
|
|
|
|
msgb->len -= len;
|
|
|
|
return msgb->data += len;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! remove (pull) all headers in front of l3h from the message buffer.
|
2014-01-28 10:03:11 +00:00
|
|
|
* \param[in] msgb message buffer with a valid l3h
|
|
|
|
* \returns pointer to new start of msgb (l3h)
|
|
|
|
*
|
|
|
|
* This function moves the \a data pointer of the \ref msgb further back
|
|
|
|
* in the message, thereby shrinking the size of the message.
|
|
|
|
* l1h and l2h will be cleared.
|
|
|
|
*/
|
|
|
|
static inline unsigned char *msgb_pull_to_l3(struct msgb *msg)
|
|
|
|
{
|
|
|
|
unsigned char *ret = msgb_pull(msg, msg->l3h - msg->data);
|
|
|
|
msg->l1h = msg->l2h = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! remove (pull) all headers in front of l2h from the message buffer.
|
2017-04-15 17:04:34 +00:00
|
|
|
* \param[in] msgb message buffer with a valid l2h
|
|
|
|
* \returns pointer to new start of msgb (l2h)
|
|
|
|
*
|
|
|
|
* This function moves the \a data pointer of the \ref msgb further back
|
|
|
|
* in the message, thereby shrinking the size of the message.
|
|
|
|
* l1h will be cleared.
|
|
|
|
*/
|
|
|
|
static inline unsigned char *msgb_pull_to_l2(struct msgb *msg)
|
|
|
|
{
|
|
|
|
unsigned char *ret = msgb_pull(msg, msg->l2h - msg->data);
|
|
|
|
msg->l1h = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! remove uint8 from front of message
|
2012-09-08 17:58:59 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \returns 8bit value taken from end of msgb
|
|
|
|
*/
|
|
|
|
static inline uint8_t msgb_pull_u8(struct msgb *msgb)
|
|
|
|
{
|
2012-11-14 18:36:00 +00:00
|
|
|
uint8_t *space = msgb_pull(msgb, 1) - 1;
|
2012-09-08 17:58:59 +00:00
|
|
|
return space[0];
|
|
|
|
}
|
2014-06-04 17:07:41 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! remove uint16 from front of message
|
2012-09-08 17:58:59 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \returns 16bit value taken from end of msgb
|
|
|
|
*/
|
|
|
|
static inline uint16_t msgb_pull_u16(struct msgb *msgb)
|
|
|
|
{
|
2012-11-14 18:36:00 +00:00
|
|
|
uint8_t *space = msgb_pull(msgb, 2) - 2;
|
2014-06-04 17:07:41 +00:00
|
|
|
return osmo_load16be(space);
|
2012-09-08 17:58:59 +00:00
|
|
|
}
|
2014-06-04 17:07:41 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! remove uint32 from front of message
|
2012-09-08 17:58:59 +00:00
|
|
|
* \param[in] msgb message buffer
|
|
|
|
* \returns 32bit value taken from end of msgb
|
|
|
|
*/
|
|
|
|
static inline uint32_t msgb_pull_u32(struct msgb *msgb)
|
|
|
|
{
|
2012-11-14 18:36:00 +00:00
|
|
|
uint8_t *space = msgb_pull(msgb, 4) - 4;
|
2014-06-04 17:07:41 +00:00
|
|
|
return osmo_load32be(space);
|
2012-09-08 17:58:59 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Increase headroom of empty msgb, reducing the tailroom
|
2011-08-16 21:26:52 +00:00
|
|
|
* \param[in] msg message buffer
|
|
|
|
* \param[in] len amount of extra octets to be reserved as headroom
|
|
|
|
*
|
|
|
|
* This function reserves some memory at the beginning of the underlying
|
|
|
|
* data buffer. The idea is to reserve space in case further headers
|
|
|
|
* have to be pushed to the \ref msgb during further processing.
|
|
|
|
*
|
|
|
|
* Calling this function leads to undefined reusults if it is called on
|
|
|
|
* a non-empty \ref msgb.
|
|
|
|
*/
|
2010-02-20 19:34:29 +00:00
|
|
|
static inline void msgb_reserve(struct msgb *msg, int len)
|
|
|
|
{
|
|
|
|
msg->data += len;
|
|
|
|
msg->tail += len;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Trim the msgb to a given absolute length
|
2012-01-14 20:53:41 +00:00
|
|
|
* \param[in] msg message buffer
|
|
|
|
* \param[in] len new total length of buffer
|
|
|
|
* \returns 0 in case of success, negative in case of error
|
|
|
|
*/
|
|
|
|
static inline int msgb_trim(struct msgb *msg, int len)
|
|
|
|
{
|
2015-11-27 12:26:19 +00:00
|
|
|
if (len < 0)
|
|
|
|
MSGB_ABORT(msg, "Negative length is not allowed\n");
|
2012-01-14 21:07:59 +00:00
|
|
|
if (len > msg->data_len)
|
2012-01-14 20:53:41 +00:00
|
|
|
return -1;
|
|
|
|
|
2012-01-14 21:07:59 +00:00
|
|
|
msg->len = len;
|
|
|
|
msg->tail = msg->data + len;
|
2012-01-14 20:53:41 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Trim the msgb to a given layer3 length
|
2013-02-15 12:27:59 +00:00
|
|
|
* \param[in] msg message buffer
|
2012-01-14 20:53:41 +00:00
|
|
|
* \param[in] l3len new layer3 length
|
|
|
|
* \returns 0 in case of success, negative in case of error
|
|
|
|
*/
|
|
|
|
static inline int msgb_l3trim(struct msgb *msg, int l3len)
|
|
|
|
{
|
|
|
|
return msgb_trim(msg, (msg->l3h - msg->data) + l3len);
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Allocate message buffer with specified headroom
|
2011-08-16 21:26:52 +00:00
|
|
|
* \param[in] size size in bytes, including headroom
|
|
|
|
* \param[in] headroom headroom in bytes
|
|
|
|
* \param[in] name human-readable name
|
|
|
|
* \returns allocated message buffer with specified headroom
|
|
|
|
*
|
|
|
|
* This function is a convenience wrapper around \ref msgb_alloc
|
|
|
|
* followed by \ref msgb_reserve in order to create a new \ref msgb with
|
|
|
|
* user-specified amount of headroom.
|
|
|
|
*/
|
2010-02-20 19:34:29 +00:00
|
|
|
static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
|
|
|
|
const char *name)
|
|
|
|
{
|
2011-05-07 10:43:08 +00:00
|
|
|
osmo_static_assert(size > headroom, headroom_bigger);
|
2011-01-16 16:38:22 +00:00
|
|
|
|
2010-02-20 19:34:29 +00:00
|
|
|
struct msgb *msg = msgb_alloc(size, name);
|
|
|
|
if (msg)
|
|
|
|
msgb_reserve(msg, headroom);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Check a message buffer for consistency
|
2015-11-27 12:26:15 +00:00
|
|
|
* \param[in] msg message buffer
|
|
|
|
* \returns 0 (false) if inconsistent, != 0 (true) otherwise
|
|
|
|
*/
|
|
|
|
static inline int msgb_test_invariant(const struct msgb *msg)
|
|
|
|
{
|
|
|
|
const unsigned char *lbound;
|
|
|
|
if (!msg || !msg->data || !msg->tail ||
|
|
|
|
(msg->data + msg->len != msg->tail) ||
|
|
|
|
(msg->data < msg->head) ||
|
|
|
|
(msg->tail > msg->head + msg->data_len))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
lbound = msg->head;
|
|
|
|
|
|
|
|
if (msg->l1h) {
|
|
|
|
if (msg->l1h < lbound)
|
|
|
|
return 0;
|
|
|
|
lbound = msg->l1h;
|
|
|
|
}
|
|
|
|
if (msg->l2h) {
|
|
|
|
if (msg->l2h < lbound)
|
|
|
|
return 0;
|
|
|
|
lbound = msg->l2h;
|
|
|
|
}
|
|
|
|
if (msg->l3h) {
|
|
|
|
if (msg->l3h < lbound)
|
|
|
|
return 0;
|
|
|
|
lbound = msg->l3h;
|
|
|
|
}
|
|
|
|
if (msg->l4h) {
|
|
|
|
if (msg->l4h < lbound)
|
|
|
|
return 0;
|
|
|
|
lbound = msg->l4h;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lbound <= msg->head + msg->data_len;
|
|
|
|
}
|
|
|
|
|
2010-10-18 16:22:31 +00:00
|
|
|
/* non inline functions to ease binding */
|
2011-08-16 21:26:52 +00:00
|
|
|
|
2010-10-18 16:22:31 +00:00
|
|
|
uint8_t *msgb_data(const struct msgb *msg);
|
2016-09-15 22:15:56 +00:00
|
|
|
|
|
|
|
void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size);
|
|
|
|
void msgb_set_talloc_ctx(void *ctx) OSMO_DEPRECATED("Use msgb_talloc_ctx_init() instead");
|
2017-10-10 14:53:21 +00:00
|
|
|
int msgb_printf(struct msgb *msgb, const char *format, ...);
|
2010-10-18 16:22:31 +00:00
|
|
|
|
2012-04-18 19:53:23 +00:00
|
|
|
/*! @} */
|