libosmocore  0.9.6.269-c2af
Osmocom core library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
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>
23 #include <osmocom/core/linuxlist.h>
24 #include <osmocom/core/utils.h>
25 #include <osmocom/core/bits.h>
26 #include <osmocom/core/defs.h>
27 
34 #define MSGB_DEBUG
35 
37 struct msgb {
38  struct llist_head list;
41  /* Part of which TRX logical channel we were received / transmitted */
42  /* FIXME: move them into the control buffer */
43  union {
44  void *dst;
45  struct gsm_bts_trx *trx;
46  };
47  struct gsm_lchan *lchan;
49  unsigned char *l1h;
50  unsigned char *l2h;
51  unsigned char *l3h;
52  unsigned char *l4h;
54  unsigned long cb[5];
56  uint16_t data_len;
57  uint16_t len;
59  unsigned char *head;
60  unsigned char *tail;
61  unsigned char *data;
62  unsigned char _data[0];
63 };
64 
65 extern struct msgb *msgb_alloc(uint16_t size, const char *name);
66 extern void msgb_free(struct msgb *m);
67 extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
68 extern struct msgb *msgb_dequeue(struct llist_head *queue);
69 extern void msgb_reset(struct msgb *m);
70 uint16_t msgb_length(const struct msgb *msg);
71 extern const char *msgb_hexdump(const struct msgb *msg);
72 extern int msgb_resize_area(struct msgb *msg, uint8_t *area,
73  int old_size, int new_size);
74 extern struct msgb *msgb_copy(const struct msgb *msg, const char *name);
75 static int msgb_test_invariant(const struct msgb *msg) __attribute__((pure));
76 
77 #ifdef MSGB_DEBUG
78 #include <osmocom/core/panic.h>
79 #define MSGB_ABORT(msg, fmt, args ...) do { \
80  osmo_panic("msgb(%p): " fmt, msg, ## args); \
81  } while(0)
82 #else
83 #define MSGB_ABORT(msg, fmt, args ...)
84 #endif
85 
87 #define msgb_l1(m) ((void *)(m->l1h))
88 
89 #define msgb_l2(m) ((void *)(m->l2h))
90 
91 #define msgb_l3(m) ((void *)(m->l3h))
92 
93 #define msgb_sms(m) ((void *)(m->l4h))
94 
102 static inline unsigned int msgb_l1len(const struct msgb *msgb)
103 {
104  return msgb->tail - (uint8_t *)msgb_l1(msgb);
105 }
106 
114 static inline unsigned int msgb_l2len(const struct msgb *msgb)
115 {
116  return msgb->tail - (uint8_t *)msgb_l2(msgb);
117 }
118 
126 static inline unsigned int msgb_l3len(const struct msgb *msgb)
127 {
128  return msgb->tail - (uint8_t *)msgb_l3(msgb);
129 }
130 
138 static inline unsigned int msgb_headlen(const struct msgb *msgb)
139 {
140  return msgb->len - msgb->data_len;
141 }
142 
150 static inline int msgb_tailroom(const struct msgb *msgb)
151 {
152  return (msgb->head + msgb->data_len) - msgb->tail;
153 }
154 
162 static inline int msgb_headroom(const struct msgb *msgb)
163 {
164  return (msgb->data - msgb->head);
165 }
166 
179 static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
180 {
181  unsigned char *tmp = msgb->tail;
182  if (msgb_tailroom(msgb) < (int) len)
183  MSGB_ABORT(msgb, "Not enough tailroom msgb_put (%u < %u)\n",
184  msgb_tailroom(msgb), len);
185  msgb->tail += len;
186  msgb->len += len;
187  return tmp;
188 }
189 
194 static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
195 {
196  uint8_t *space = msgb_put(msgb, 1);
197  space[0] = word & 0xFF;
198 }
199 
204 static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
205 {
206  uint8_t *space = msgb_put(msgb, 2);
207  osmo_store16be(word, space);
208 }
209 
214 static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
215 {
216  uint8_t *space = msgb_put(msgb, 4);
217  osmo_store32be(word, space);
218 }
219 
224 static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
225 {
226  unsigned char *tmp = msgb->tail - len;
227  if (msgb_length(msgb) < len)
228  MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n",
229  len, msgb_length(msgb));
230  msgb->tail -= len;
231  msgb->len -= len;
232  return tmp;
233 }
234 
239 static inline uint8_t msgb_get_u8(struct msgb *msgb)
240 {
241  uint8_t *space = msgb_get(msgb, 1);
242  return space[0];
243 }
244 
249 static inline uint16_t msgb_get_u16(struct msgb *msgb)
250 {
251  uint8_t *space = msgb_get(msgb, 2);
252  return osmo_load16be(space);
253 }
254 
259 static inline uint32_t msgb_get_u32(struct msgb *msgb)
260 {
261  uint8_t *space = msgb_get(msgb, 4);
262  return osmo_load32be(space);
263 }
264 
277 static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
278 {
279  if (msgb_headroom(msgb) < (int) len)
280  MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
281  msgb_headroom(msgb), len);
282  msgb->data -= len;
283  msgb->len += len;
284  return msgb->data;
285 }
286 
291 static inline void msgb_push_u8(struct msgb *msg, uint8_t word)
292 {
293  uint8_t *space = msgb_push(msg, 1);
294  space[0] = word;
295 }
296 
301 static inline void msgb_push_u16(struct msgb *msg, uint16_t word)
302 {
303  uint16_t *space = (uint16_t *) msgb_push(msg, 2);
304  osmo_store16be(word, space);
305 }
306 
311 static inline void msgb_push_u32(struct msgb *msg, uint32_t word)
312 {
313  uint32_t *space = (uint32_t *) msgb_push(msg, 4);
314  osmo_store32be(word, space);
315 }
316 
326 static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
327 {
328  msgb->len -= len;
329  return msgb->data += len;
330 }
331 
340 static inline unsigned char *msgb_pull_to_l3(struct msgb *msg)
341 {
342  unsigned char *ret = msgb_pull(msg, msg->l3h - msg->data);
343  msg->l1h = msg->l2h = NULL;
344  return ret;
345 }
346 
355 static inline unsigned char *msgb_pull_to_l2(struct msgb *msg)
356 {
357  unsigned char *ret = msgb_pull(msg, msg->l2h - msg->data);
358  msg->l1h = NULL;
359  return ret;
360 }
361 
366 static inline uint8_t msgb_pull_u8(struct msgb *msgb)
367 {
368  uint8_t *space = msgb_pull(msgb, 1) - 1;
369  return space[0];
370 }
371 
376 static inline uint16_t msgb_pull_u16(struct msgb *msgb)
377 {
378  uint8_t *space = msgb_pull(msgb, 2) - 2;
379  return osmo_load16be(space);
380 }
381 
386 static inline uint32_t msgb_pull_u32(struct msgb *msgb)
387 {
388  uint8_t *space = msgb_pull(msgb, 4) - 4;
389  return osmo_load32be(space);
390 }
391 
403 static inline void msgb_reserve(struct msgb *msg, int len)
404 {
405  msg->data += len;
406  msg->tail += len;
407 }
408 
414 static inline int msgb_trim(struct msgb *msg, int len)
415 {
416  if (len < 0)
417  MSGB_ABORT(msg, "Negative length is not allowed\n");
418  if (len > msg->data_len)
419  return -1;
420 
421  msg->len = len;
422  msg->tail = msg->data + len;
423 
424  return 0;
425 }
426 
432 static inline int msgb_l3trim(struct msgb *msg, int l3len)
433 {
434  return msgb_trim(msg, (msg->l3h - msg->data) + l3len);
435 }
436 
447 static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
448  const char *name)
449 {
450  osmo_static_assert(size > headroom, headroom_bigger);
451 
452  struct msgb *msg = msgb_alloc(size, name);
453  if (msg)
454  msgb_reserve(msg, headroom);
455  return msg;
456 }
457 
462 static inline int msgb_test_invariant(const struct msgb *msg)
463 {
464  const unsigned char *lbound;
465  if (!msg || !msg->data || !msg->tail ||
466  (msg->data + msg->len != msg->tail) ||
467  (msg->data < msg->head) ||
468  (msg->tail > msg->head + msg->data_len))
469  return 0;
470 
471  lbound = msg->head;
472 
473  if (msg->l1h) {
474  if (msg->l1h < lbound)
475  return 0;
476  lbound = msg->l1h;
477  }
478  if (msg->l2h) {
479  if (msg->l2h < lbound)
480  return 0;
481  lbound = msg->l2h;
482  }
483  if (msg->l3h) {
484  if (msg->l3h < lbound)
485  return 0;
486  lbound = msg->l3h;
487  }
488  if (msg->l4h) {
489  if (msg->l4h < lbound)
490  return 0;
491  lbound = msg->l4h;
492  }
493 
494  return lbound <= msg->head + msg->data_len;
495 }
496 
497 /* non inline functions to ease binding */
498 
499 uint8_t *msgb_data(const struct msgb *msg);
500 
501 void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size);
502 void msgb_set_talloc_ctx(void *ctx) OSMO_DEPRECATED("Use msgb_talloc_ctx_init() instead");
503 
static unsigned char * msgb_put(struct msgb *msgb, unsigned int len)
append data to end of message buffer
Definition: msgb.h:179
static uint16_t msgb_pull_u16(struct msgb *msgb)
remove uint16 from front of message
Definition: msgb.h:376
static unsigned int msgb_l3len(const struct msgb *msgb)
determine length of L3 message
Definition: msgb.h:126
unsigned long cb[5]
control buffer
Definition: msgb.h:54
#define OSMO_DEPRECATED(text)
Set the deprecated attribute with a message.
Definition: defs.h:41
unsigned char * l3h
pointer to Layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP
Definition: msgb.h:51
#define msgb_l3(m)
obtain L3 header of msgb
Definition: msgb.h:91
#define msgb_l1(m)
obtain L1 header of msgb
Definition: msgb.h:87
struct msgb * msgb_alloc(uint16_t size, const char *name)
Allocate a new message buffer.
Definition: msgb.c:74
unsigned char * data
start of message in buffer
Definition: msgb.h:61
Osmocom message buffer.
Definition: msgb.h:37
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:182
static int msgb_l3trim(struct msgb *msg, int l3len)
Trim the msgb to a given layer3 length.
Definition: msgb.h:432
General definitions that are meant to be included from header files.
static int msgb_trim(struct msgb *msg, int len)
Trim the msgb to a given absolute length.
Definition: msgb.h:414
uint16_t len
length of bytes used in msgb
Definition: msgb.h:57
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:340
void msgb_free(struct msgb *m)
Release given message buffer.
Definition: msgb.c:97
static int msgb_tailroom(const struct msgb *msgb)
determine how much tail room is left in msgb
Definition: msgb.h:150
unsigned char * l4h
pointer to layer 4 header
Definition: msgb.h:52
static unsigned int msgb_l1len(const struct msgb *msgb)
determine length of L1 message
Definition: msgb.h:102
static unsigned char * msgb_get(struct msgb *msgb, unsigned int len)
remove data from end of message
Definition: msgb.h:224
const char * msgb_hexdump(const struct msgb *msg)
Return a (static) buffer containing a hexdump of the msg.
Definition: msgb.c:300
static void msgb_push_u8(struct msgb *msg, uint8_t word)
prepend a uint8 value to the head of the message
Definition: msgb.h:291
struct msgb * msgb_dequeue(struct llist_head *queue)
Dequeue message buffer from head of queue.
Definition: msgb.c:121
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". If pool_size is 0, create a named const as msgb talloc context. If pool_size is nonzero, create a talloc pool, possibly for faster msgb allocations (see talloc_pool()).
Definition: msgb.c:195
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:255
static uint32_t msgb_pull_u32(struct msgb *msgb)
remove uint32 from front of message
Definition: msgb.h:386
static unsigned int msgb_l2len(const struct msgb *msgb)
determine length of L2 message
Definition: msgb.h:114
static struct msgb * msgb_alloc_headroom(int size, int headroom, const char *name)
Allocate message buffer with specified headroom.
Definition: msgb.h:447
static unsigned char * msgb_push(struct msgb *msgb, unsigned int len)
prepend (push) some data to start of message
Definition: msgb.h:277
Simple doubly linked list implementation.
(double) linked list header structure
Definition: linuxlist.h:47
unsigned char * l1h
pointer to Layer1 header (if any)
Definition: msgb.h:49
static uint8_t msgb_pull_u8(struct msgb *msgb)
remove uint8 from front of message
Definition: msgb.h:366
void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
Enqueue message buffer to tail of a queue.
Definition: msgb.c:109
struct gsm_lchan * lchan
logical channel
Definition: msgb.h:47
void msgb_reset(struct msgb *m)
Re-set all message buffer pointers.
Definition: msgb.c:144
static void msgb_push_u32(struct msgb *msg, uint32_t word)
prepend a uint32 value to the head of the message
Definition: msgb.h:311
static void msgb_reserve(struct msgb *msg, int len)
Increase headroom of empty msgb, reducing the tailroom.
Definition: msgb.h:403
unsigned char * tail
end of message in buffer
Definition: msgb.h:60
static void msgb_put_u16(struct msgb *msgb, uint16_t word)
append a uint16 value to the end of the message
Definition: msgb.h:204
static uint8_t msgb_get_u8(struct msgb *msgb)
remove uint8 from end of message
Definition: msgb.h:239
unsigned char * l2h
pointer to A-bis layer 2 header: OML, RSL(RLL), NS
Definition: msgb.h:50
struct msgb * msgb_copy(const struct msgb *msg, const char *name)
Copy an msgb.
Definition: msgb.c:213
unsigned char * head
start of underlying memory buffer
Definition: msgb.h:59
static uint32_t msgb_get_u32(struct msgb *msgb)
remove uint32 from end of message
Definition: msgb.h:259
static int msgb_headroom(const struct msgb *msgb)
determine the amount of headroom in msgb
Definition: msgb.h:162
Osmocom bit level support code.
uint16_t data_len
length of underlying data array
Definition: msgb.h:56
unsigned char _data[0]
optional immediate data array
Definition: msgb.h:62
struct llist_head list
linked list header
Definition: msgb.h:38
uint8_t * msgb_data(const struct msgb *msg)
get pointer to data section of message buffer
Definition: msgb.c:164
#define msgb_l2(m)
obtain L2 header of msgb
Definition: msgb.h:89
static uint16_t msgb_get_u16(struct msgb *msgb)
remove uint16 from end of message
Definition: msgb.h:249
void * dst
reference of origin/destination
Definition: msgb.h:44
static unsigned int msgb_headlen(const struct msgb *msgb)
determine the length of the header
Definition: msgb.h:138
uint16_t msgb_length(const struct msgb *msg)
get length of message buffer
Definition: msgb.c:173
static int msgb_test_invariant(const struct msgb *msg) __attribute__((pure))
Check a message buffer for consistency.
Definition: msgb.h:462
static void msgb_put_u32(struct msgb *msgb, uint32_t word)
append a uint32 value to the end of the message
Definition: msgb.h:214
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:326
static unsigned char * msgb_pull_to_l2(struct msgb *msg)
remove (pull) all headers in front of l2h from the message buffer.
Definition: msgb.h:355
static void msgb_push_u16(struct msgb *msg, uint16_t word)
prepend a uint16 value to the head of the message
Definition: msgb.h:301
static void msgb_put_u8(struct msgb *msgb, uint8_t word)
append a uint8 value to the end of the message
Definition: msgb.h:194