libosmocore 0.9.6-23.20170220git32ee5af8.fc42
Osmocom core library
Loading...
Searching...
No Matches
msgb.h
Go to the documentation of this file.
1#pragma once
2
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>
24#include <osmocom/core/utils.h>
25#include <osmocom/core/bits.h>
26#include <osmocom/core/defs.h>
27
40#define MSGB_DEBUG
41
43struct msgb {
47 /* Part of which TRX logical channel we were received / transmitted */
48 /* FIXME: move them into the control buffer */
49 union {
50 void *dst;
51 struct gsm_bts_trx *trx;
52 };
53 struct gsm_lchan *lchan;
55 unsigned char *l1h;
56 unsigned char *l2h;
57 unsigned char *l3h;
58 unsigned char *l4h;
60 unsigned long cb[5];
62 uint16_t data_len;
63 uint16_t len;
65 unsigned char *head;
66 unsigned char *tail;
67 unsigned char *data;
68 unsigned char _data[0];
69};
70
71extern struct msgb *msgb_alloc(uint16_t size, const char *name);
72extern void msgb_free(struct msgb *m);
73extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
74extern struct msgb *msgb_dequeue(struct llist_head *queue);
75extern void msgb_reset(struct msgb *m);
76uint16_t msgb_length(const struct msgb *msg);
77extern const char *msgb_hexdump(const struct msgb *msg);
78extern int msgb_resize_area(struct msgb *msg, uint8_t *area,
79 int old_size, int new_size);
80extern struct msgb *msgb_copy(const struct msgb *msg, const char *name);
81static int msgb_test_invariant(const struct msgb *msg) __attribute__((pure));
82
83#ifdef MSGB_DEBUG
84#include <osmocom/core/panic.h>
85#define MSGB_ABORT(msg, fmt, args ...) do { \
86 osmo_panic("msgb(%p): " fmt, msg, ## args); \
87 } while(0)
88#else
89#define MSGB_ABORT(msg, fmt, args ...)
90#endif
91
93#define msgb_l1(m) ((void *)(m->l1h))
95#define msgb_l2(m) ((void *)(m->l2h))
97#define msgb_l3(m) ((void *)(m->l3h))
99#define msgb_sms(m) ((void *)(m->l4h))
100
108static inline unsigned int msgb_l1len(const struct msgb *msgb)
109{
110 return msgb->tail - (uint8_t *)msgb_l1(msgb);
111}
112
120static inline unsigned int msgb_l2len(const struct msgb *msgb)
121{
122 return msgb->tail - (uint8_t *)msgb_l2(msgb);
123}
124
132static inline unsigned int msgb_l3len(const struct msgb *msgb)
133{
134 return msgb->tail - (uint8_t *)msgb_l3(msgb);
135}
136
144static inline unsigned int msgb_headlen(const struct msgb *msgb)
145{
146 return msgb->len - msgb->data_len;
147}
148
156static inline int msgb_tailroom(const struct msgb *msgb)
157{
158 return (msgb->head + msgb->data_len) - msgb->tail;
159}
160
168static inline int msgb_headroom(const struct msgb *msgb)
169{
170 return (msgb->data - msgb->head);
171}
172
185static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
186{
187 unsigned char *tmp = msgb->tail;
188 if (msgb_tailroom(msgb) < (int) len)
189 MSGB_ABORT(msgb, "Not enough tailroom msgb_put (%u < %u)\n",
191 msgb->tail += len;
192 msgb->len += len;
193 return tmp;
194}
195
200static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
201{
202 uint8_t *space = msgb_put(msgb, 1);
203 space[0] = word & 0xFF;
204}
205
210static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
211{
212 uint8_t *space = msgb_put(msgb, 2);
213 osmo_store16be(word, space);
214}
215
220static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
221{
222 uint8_t *space = msgb_put(msgb, 4);
223 osmo_store32be(word, space);
224}
225
230static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
231{
232 unsigned char *tmp = msgb->tail - len;
233 if (msgb_length(msgb) < len)
234 MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n",
236 msgb->tail -= len;
237 msgb->len -= len;
238 return tmp;
239}
240
245static inline uint8_t msgb_get_u8(struct msgb *msgb)
246{
247 uint8_t *space = msgb_get(msgb, 1);
248 return space[0];
249}
250
255static inline uint16_t msgb_get_u16(struct msgb *msgb)
256{
257 uint8_t *space = msgb_get(msgb, 2);
258 return osmo_load16be(space);
259}
260
265static inline uint32_t msgb_get_u32(struct msgb *msgb)
266{
267 uint8_t *space = msgb_get(msgb, 4);
268 return osmo_load32be(space);
269}
270
283static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
284{
285 if (msgb_headroom(msgb) < (int) len)
286 MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
288 msgb->data -= len;
289 msgb->len += len;
290 return msgb->data;
291}
292
297static inline void msgb_push_u8(struct msgb *msg, uint8_t word)
298{
299 uint8_t *space = msgb_push(msg, 1);
300 space[0] = word;
301}
302
307static inline void msgb_push_u16(struct msgb *msg, uint16_t word)
308{
309 uint16_t *space = (uint16_t *) msgb_push(msg, 2);
310 osmo_store16be(word, space);
311}
312
317static inline void msgb_push_u32(struct msgb *msg, uint32_t word)
318{
319 uint32_t *space = (uint32_t *) msgb_push(msg, 4);
320 osmo_store32be(word, space);
321}
322
332static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
333{
334 msgb->len -= len;
335 return msgb->data += len;
336}
337
346static inline unsigned char *msgb_pull_to_l3(struct msgb *msg)
347{
348 unsigned char *ret = msgb_pull(msg, msg->l3h - msg->data);
349 msg->l1h = msg->l2h = NULL;
350 return ret;
351}
352
357static inline uint8_t msgb_pull_u8(struct msgb *msgb)
358{
359 uint8_t *space = msgb_pull(msgb, 1) - 1;
360 return space[0];
361}
362
367static inline uint16_t msgb_pull_u16(struct msgb *msgb)
368{
369 uint8_t *space = msgb_pull(msgb, 2) - 2;
370 return osmo_load16be(space);
371}
372
377static inline uint32_t msgb_pull_u32(struct msgb *msgb)
378{
379 uint8_t *space = msgb_pull(msgb, 4) - 4;
380 return osmo_load32be(space);
381}
382
394static inline void msgb_reserve(struct msgb *msg, int len)
395{
396 msg->data += len;
397 msg->tail += len;
398}
399
405static inline int msgb_trim(struct msgb *msg, int len)
406{
407 if (len < 0)
408 MSGB_ABORT(msg, "Negative length is not allowed\n");
409 if (len > msg->data_len)
410 return -1;
411
412 msg->len = len;
413 msg->tail = msg->data + len;
414
415 return 0;
416}
417
423static inline int msgb_l3trim(struct msgb *msg, int l3len)
424{
425 return msgb_trim(msg, (msg->l3h - msg->data) + l3len);
426}
427
438static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
439 const char *name)
440{
441 osmo_static_assert(size > headroom, headroom_bigger);
442
443 struct msgb *msg = msgb_alloc(size, name);
444 if (msg)
445 msgb_reserve(msg, headroom);
446 return msg;
447}
448
453static inline int msgb_test_invariant(const struct msgb *msg)
454{
455 const unsigned char *lbound;
456 if (!msg || !msg->data || !msg->tail ||
457 (msg->data + msg->len != msg->tail) ||
458 (msg->data < msg->head) ||
459 (msg->tail > msg->head + msg->data_len))
460 return 0;
461
462 lbound = msg->head;
463
464 if (msg->l1h) {
465 if (msg->l1h < lbound)
466 return 0;
467 lbound = msg->l1h;
468 }
469 if (msg->l2h) {
470 if (msg->l2h < lbound)
471 return 0;
472 lbound = msg->l2h;
473 }
474 if (msg->l3h) {
475 if (msg->l3h < lbound)
476 return 0;
477 lbound = msg->l3h;
478 }
479 if (msg->l4h) {
480 if (msg->l4h < lbound)
481 return 0;
482 lbound = msg->l4h;
483 }
484
485 return lbound <= msg->head + msg->data_len;
486}
487
488/* non inline functions to ease binding */
489
490uint8_t *msgb_data(const struct msgb *msg);
491
492void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size);
493void msgb_set_talloc_ctx(void *ctx) OSMO_DEPRECATED("Use msgb_talloc_ctx_init() instead");
494
Osmocom bit level support code.
General definitions that are meant to be included from header files.
static unsigned char * msgb_pull(struct msgb *msgb, unsigned int len)
remove (pull) a header from the front of the message buffer
Definition msgb.h:332
uint16_t msgb_length(const struct msgb *msg)
get length of message buffer
Definition msgb.c:148
static uint8_t msgb_pull_u8(struct msgb *msgb)
remove uint8 from front of message
Definition msgb.h:357
static int msgb_tailroom(const struct msgb *msgb)
determine how much tail room is left in msgb
Definition msgb.h:156
void msgb_set_talloc_ctx(void *ctx) OSMO_DEPRECATED("Use msgb_talloc_ctx_init() instead")
Set the talloc context for msgb_alloc Deprecated, use msgb_talloc_ctx_init() instead.
Definition msgb.c:157
struct msgb * msgb_alloc(uint16_t size, const char *name)
Allocate a new message buffer.
Definition msgb.c:49
static void msgb_put_u32(struct msgb *msgb, uint32_t word)
append a uint32 value to the end of the message
Definition msgb.h:220
static uint16_t msgb_pull_u16(struct msgb *msgb)
remove uint16 from front of message
Definition msgb.h:367
static uint32_t msgb_get_u32(struct msgb *msgb)
remove uint32 from end of message
Definition msgb.h:265
static int msgb_test_invariant(const struct msgb *msg) __attribute__((pure))
Check a message buffer for consistency.
Definition msgb.h:453
static unsigned int msgb_l3len(const struct msgb *msgb)
determine length of L3 message
Definition msgb.h:132
void msgb_reset(struct msgb *m)
Re-set all message buffer pointers.
Definition msgb.c:119
static void msgb_push_u8(struct msgb *msg, uint8_t word)
prepend a uint8 value to the head of the message
Definition msgb.h:297
#define msgb_l1(m)
obtain L1 header of msgb
Definition msgb.h:93
struct msgb * msgb_copy(const struct msgb *msg, const char *name)
Copy an msgb.
Definition msgb.c:188
static unsigned int msgb_l2len(const struct msgb *msgb)
determine length of L2 message
Definition msgb.h:120
void * msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size)
Initialize a msgb talloc context for msgb_alloc. Create a talloc context called "msgb"....
Definition msgb.c:170
static void msgb_push_u16(struct msgb *msg, uint16_t word)
prepend a uint16 value to the head of the message
Definition msgb.h:307
static uint32_t msgb_pull_u32(struct msgb *msgb)
remove uint32 from front of message
Definition msgb.h:377
static unsigned int msgb_l1len(const struct msgb *msgb)
determine length of L1 message
Definition msgb.h:108
static unsigned char * msgb_put(struct msgb *msgb, unsigned int len)
append data to end of message buffer
Definition msgb.h:185
static void msgb_put_u16(struct msgb *msgb, uint16_t word)
append a uint16 value to the end of the message
Definition msgb.h:210
const char * msgb_hexdump(const struct msgb *msg)
Return a (static) buffer containing a hexdump of the msg.
Definition msgb.c:275
uint8_t * msgb_data(const struct msgb *msg)
get pointer to data section of message buffer
Definition msgb.c:139
static int msgb_trim(struct msgb *msg, int len)
Trim the msgb to a given absolute length.
Definition msgb.h:405
static uint8_t msgb_get_u8(struct msgb *msgb)
remove uint8 from end of message
Definition msgb.h:245
static struct msgb * msgb_alloc_headroom(int size, int headroom, const char *name)
Allocate message buffer with specified headroom.
Definition msgb.h:438
static unsigned char * msgb_pull_to_l3(struct msgb *msg)
remove (pull) all headers in front of l3h from the message buffer.
Definition msgb.h:346
static void msgb_push_u32(struct msgb *msg, uint32_t word)
prepend a uint32 value to the head of the message
Definition msgb.h:317
int msgb_resize_area(struct msgb *msg, uint8_t *area, int old_size, int new_size)
Resize an area within an msgb.
Definition msgb.c:230
static void msgb_put_u8(struct msgb *msgb, uint8_t word)
append a uint8 value to the end of the message
Definition msgb.h:200
void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
Enqueue message buffer to tail of a queue.
Definition msgb.c:84
static unsigned char * msgb_get(struct msgb *msgb, unsigned int len)
remove data from end of message
Definition msgb.h:230
static uint16_t msgb_get_u16(struct msgb *msgb)
remove uint16 from end of message
Definition msgb.h:255
static void msgb_reserve(struct msgb *msg, int len)
Increase headroom of empty msgb, reducing the tailroom.
Definition msgb.h:394
#define msgb_l3(m)
obtain L3 header of msgb
Definition msgb.h:97
static int msgb_l3trim(struct msgb *msg, int l3len)
Trim the msgb to a given layer3 length.
Definition msgb.h:423
struct msgb * msgb_dequeue(struct llist_head *queue)
Dequeue message buffer from head of queue.
Definition msgb.c:96
void msgb_free(struct msgb *m)
Release given message buffer.
Definition msgb.c:72
static unsigned int msgb_headlen(const struct msgb *msgb)
determine the length of the header
Definition msgb.h:144
#define msgb_l2(m)
obtain L2 header of msgb
Definition msgb.h:95
static unsigned char * msgb_push(struct msgb *msgb, unsigned int len)
prepend (push) some data to start of message
Definition msgb.h:283
static int msgb_headroom(const struct msgb *msgb)
determine the amount of headroom in msgb
Definition msgb.h:168
#define OSMO_DEPRECATED(text)
Set the deprecated attribute with a message.
Definition defs.h:41
Simple doubly linked list implementation.
(double) linked list header structure
Definition linuxlist.h:47
Osmocom message buffer.
Definition msgb.h:43
unsigned long cb[5]
control buffer
Definition msgb.h:60
unsigned char * l2h
pointer to A-bis layer 2 header: OML, RSL(RLL), NS
Definition msgb.h:56
uint16_t len
length of bytes used in msgb
Definition msgb.h:63
unsigned char * data
start of message in buffer
Definition msgb.h:67
void * dst
reference of origin/destination
Definition msgb.h:50
struct gsm_lchan * lchan
logical channel
Definition msgb.h:53
unsigned char _data[0]
optional immediate data array
Definition msgb.h:68
unsigned char * l3h
pointer to Layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP
Definition msgb.h:57
unsigned char * tail
end of message in buffer
Definition msgb.h:66
unsigned char * l4h
pointer to layer 4 header
Definition msgb.h:58
uint16_t data_len
length of underlying data array
Definition msgb.h:62
struct llist_head list
linked list header
Definition msgb.h:44
unsigned char * head
start of underlying memory buffer
Definition msgb.h:65
unsigned char * l1h
pointer to Layer1 header (if any)
Definition msgb.h:55