libosmocore  0.9.3.20160317
Osmocom core library
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 
39 #define MSGB_DEBUG
40 
42 struct msgb {
43  struct llist_head list;
46  /* Part of which TRX logical channel we were received / transmitted */
47  /* FIXME: move them into the control buffer */
48  union {
49  void *dst;
50  struct gsm_bts_trx *trx;
51  };
52  struct gsm_lchan *lchan;
54  unsigned char *l1h;
55  unsigned char *l2h;
56  unsigned char *l3h;
57  unsigned char *l4h;
59  unsigned long cb[5];
61  uint16_t data_len;
62  uint16_t len;
64  unsigned char *head;
65  unsigned char *tail;
66  unsigned char *data;
67  unsigned char _data[0];
68 };
69 
70 extern struct msgb *msgb_alloc(uint16_t size, const char *name);
71 extern void msgb_free(struct msgb *m);
72 extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
73 extern struct msgb *msgb_dequeue(struct llist_head *queue);
74 extern void msgb_reset(struct msgb *m);
75 uint16_t msgb_length(const struct msgb *msg);
76 extern const char *msgb_hexdump(const struct msgb *msg);
77 extern int msgb_resize_area(struct msgb *msg, uint8_t *area,
78  int old_size, int new_size);
79 extern struct msgb *msgb_copy(const struct msgb *msg, const char *name);
80 static int msgb_test_invariant(const struct msgb *msg) __attribute__((pure));
81 
82 #ifdef MSGB_DEBUG
83 #include <osmocom/core/panic.h>
84 #define MSGB_ABORT(msg, fmt, args ...) do { \
85  osmo_panic("msgb(%p): " fmt, msg, ## args); \
86  } while(0)
87 #else
88 #define MSGB_ABORT(msg, fmt, args ...)
89 #endif
90 
92 #define msgb_l1(m) ((void *)(m->l1h))
93 
94 #define msgb_l2(m) ((void *)(m->l2h))
95 
96 #define msgb_l3(m) ((void *)(m->l3h))
97 
98 #define msgb_sms(m) ((void *)(m->l4h))
99 
107 static inline unsigned int msgb_l1len(const struct msgb *msgb)
108 {
109  return msgb->tail - (uint8_t *)msgb_l1(msgb);
110 }
111 
119 static inline unsigned int msgb_l2len(const struct msgb *msgb)
120 {
121  return msgb->tail - (uint8_t *)msgb_l2(msgb);
122 }
123 
131 static inline unsigned int msgb_l3len(const struct msgb *msgb)
132 {
133  return msgb->tail - (uint8_t *)msgb_l3(msgb);
134 }
135 
143 static inline unsigned int msgb_headlen(const struct msgb *msgb)
144 {
145  return msgb->len - msgb->data_len;
146 }
147 
155 static inline int msgb_tailroom(const struct msgb *msgb)
156 {
157  return (msgb->head + msgb->data_len) - msgb->tail;
158 }
159 
167 static inline int msgb_headroom(const struct msgb *msgb)
168 {
169  return (msgb->data - msgb->head);
170 }
171 
184 static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
185 {
186  unsigned char *tmp = msgb->tail;
187  if (msgb_tailroom(msgb) < (int) len)
188  MSGB_ABORT(msgb, "Not enough tailroom msgb_push (%u < %u)\n",
189  msgb_tailroom(msgb), len);
190  msgb->tail += len;
191  msgb->len += len;
192  return tmp;
193 }
194 
199 static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
200 {
201  uint8_t *space = msgb_put(msgb, 1);
202  space[0] = word & 0xFF;
203 }
204 
209 static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
210 {
211  uint8_t *space = msgb_put(msgb, 2);
212  osmo_store16be(word, space);
213 }
214 
219 static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
220 {
221  uint8_t *space = msgb_put(msgb, 4);
222  osmo_store32be(word, space);
223 }
224 
229 static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
230 {
231  unsigned char *tmp = msgb->tail - len;
232  if (msgb_length(msgb) < len)
233  MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n",
234  len, msgb_length(msgb));
235  msgb->tail -= len;
236  msgb->len -= len;
237  return tmp;
238 }
239 
244 static inline uint8_t msgb_get_u8(struct msgb *msgb)
245 {
246  uint8_t *space = msgb_get(msgb, 1);
247  return space[0];
248 }
249 
254 static inline uint16_t msgb_get_u16(struct msgb *msgb)
255 {
256  uint8_t *space = msgb_get(msgb, 2);
257  return osmo_load16be(space);
258 }
259 
264 static inline uint32_t msgb_get_u32(struct msgb *msgb)
265 {
266  uint8_t *space = msgb_get(msgb, 4);
267  return osmo_load32be(space);
268 }
269 
282 static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
283 {
284  if (msgb_headroom(msgb) < (int) len)
285  MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
286  msgb_headroom(msgb), len);
287  msgb->data -= len;
288  msgb->len += len;
289  return msgb->data;
290 }
291 
301 static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
302 {
303  msgb->len -= len;
304  return msgb->data += len;
305 }
306 
315 static inline unsigned char *msgb_pull_to_l3(struct msgb *msg)
316 {
317  unsigned char *ret = msgb_pull(msg, msg->l3h - msg->data);
318  msg->l1h = msg->l2h = NULL;
319  return ret;
320 }
321 
326 static inline uint8_t msgb_pull_u8(struct msgb *msgb)
327 {
328  uint8_t *space = msgb_pull(msgb, 1) - 1;
329  return space[0];
330 }
331 
336 static inline uint16_t msgb_pull_u16(struct msgb *msgb)
337 {
338  uint8_t *space = msgb_pull(msgb, 2) - 2;
339  return osmo_load16be(space);
340 }
341 
346 static inline uint32_t msgb_pull_u32(struct msgb *msgb)
347 {
348  uint8_t *space = msgb_pull(msgb, 4) - 4;
349  return osmo_load32be(space);
350 }
351 
363 static inline void msgb_reserve(struct msgb *msg, int len)
364 {
365  msg->data += len;
366  msg->tail += len;
367 }
368 
374 static inline int msgb_trim(struct msgb *msg, int len)
375 {
376  if (len < 0)
377  MSGB_ABORT(msg, "Negative length is not allowed\n");
378  if (len > msg->data_len)
379  return -1;
380 
381  msg->len = len;
382  msg->tail = msg->data + len;
383 
384  return 0;
385 }
386 
392 static inline int msgb_l3trim(struct msgb *msg, int l3len)
393 {
394  return msgb_trim(msg, (msg->l3h - msg->data) + l3len);
395 }
396 
407 static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
408  const char *name)
409 {
410  osmo_static_assert(size > headroom, headroom_bigger);
411 
412  struct msgb *msg = msgb_alloc(size, name);
413  if (msg)
414  msgb_reserve(msg, headroom);
415  return msg;
416 }
417 
422 static inline int msgb_test_invariant(const struct msgb *msg)
423 {
424  const unsigned char *lbound;
425  if (!msg || !msg->data || !msg->tail ||
426  (msg->data + msg->len != msg->tail) ||
427  (msg->data < msg->head) ||
428  (msg->tail > msg->head + msg->data_len))
429  return 0;
430 
431  lbound = msg->head;
432 
433  if (msg->l1h) {
434  if (msg->l1h < lbound)
435  return 0;
436  lbound = msg->l1h;
437  }
438  if (msg->l2h) {
439  if (msg->l2h < lbound)
440  return 0;
441  lbound = msg->l2h;
442  }
443  if (msg->l3h) {
444  if (msg->l3h < lbound)
445  return 0;
446  lbound = msg->l3h;
447  }
448  if (msg->l4h) {
449  if (msg->l4h < lbound)
450  return 0;
451  lbound = msg->l4h;
452  }
453 
454  return lbound <= msg->head + msg->data_len;
455 }
456 
457 /* non inline functions to ease binding */
458 
459 uint8_t *msgb_data(const struct msgb *msg);
460 void msgb_set_talloc_ctx(void *ctx);
461 
static unsigned char * msgb_put(struct msgb *msgb, unsigned int len)
append data to end of message buffer
Definition: msgb.h:184
static uint16_t msgb_pull_u16(struct msgb *msgb)
remove uint16 from front of message
Definition: msgb.h:336
static unsigned int msgb_l3len(const struct msgb *msgb)
determine length of L3 message
Definition: msgb.h:131
unsigned long cb[5]
control buffer
Definition: msgb.h:59
unsigned char * l3h
pointer to Layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP
Definition: msgb.h:56
#define msgb_l3(m)
obtain L3 header of msgb
Definition: msgb.h:96
#define msgb_l1(m)
obtain L1 header of msgb
Definition: msgb.h:92
struct msgb * msgb_alloc(uint16_t size, const char *name)
Allocate a new message buffer.
Definition: msgb.c:48
unsigned char * data
start of message in buffer
Definition: msgb.h:66
Osmocom message buffer.
Definition: msgb.h:42
void msgb_set_talloc_ctx(void *ctx)
Set the talloc context for msgb_alloc.
Definition: msgb.c:152
static int msgb_l3trim(struct msgb *msg, int l3len)
Trim the msgb to a given layer3 length.
Definition: msgb.h:392
static int msgb_trim(struct msgb *msg, int len)
Trim the msgb to a given absolute length.
Definition: msgb.h:374
uint16_t len
length of bytes used in msgb
Definition: msgb.h:62
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:315
void msgb_free(struct msgb *m)
Release given message buffer.
Definition: msgb.c:71
static int msgb_tailroom(const struct msgb *msgb)
determine how much tail room is left in msgb
Definition: msgb.h:155
unsigned char * l4h
pointer to layer 4 header
Definition: msgb.h:57
static unsigned int msgb_l1len(const struct msgb *msgb)
determine length of L1 message
Definition: msgb.h:107
static unsigned char * msgb_get(struct msgb *msgb, unsigned int len)
remove data from end of message
Definition: msgb.h:229
const char * msgb_hexdump(const struct msgb *msg)
Return a (static) buffer containing a hexdump of the msg.
Definition: msgb.c:252
struct msgb * msgb_dequeue(struct llist_head *queue)
Dequeue message buffer from head of queue.
Definition: msgb.c:95
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:207
static uint32_t msgb_pull_u32(struct msgb *msgb)
remove uint32 from front of message
Definition: msgb.h:346
static unsigned int msgb_l2len(const struct msgb *msgb)
determine length of L2 message
Definition: msgb.h:119
static struct msgb * msgb_alloc_headroom(int size, int headroom, const char *name)
Allocate message buffer with specified headroom.
Definition: msgb.h:407
static unsigned char * msgb_push(struct msgb *msgb, unsigned int len)
prepend (push) some data to start of message
Definition: msgb.h:282
Definition: linuxlist.h:42
unsigned char * l1h
pointer to Layer1 header (if any)
Definition: msgb.h:54
static uint8_t msgb_pull_u8(struct msgb *msgb)
remove uint8 from front of message
Definition: msgb.h:326
void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
Enqueue message buffer to tail of a queue.
Definition: msgb.c:83
struct gsm_lchan * lchan
logical channel
Definition: msgb.h:52
void msgb_reset(struct msgb *m)
Re-set all message buffer pointers.
Definition: msgb.c:115
static void msgb_reserve(struct msgb *msg, int len)
Increase headroom of empty msgb, reducing the tailroom.
Definition: msgb.h:363
unsigned char * tail
end of message in buffer
Definition: msgb.h:65
static void msgb_put_u16(struct msgb *msgb, uint16_t word)
append a uint16 value to the end of the message
Definition: msgb.h:209
static uint8_t msgb_get_u8(struct msgb *msgb)
remove uint8 from end of message
Definition: msgb.h:244
unsigned char * l2h
pointer to A-bis layer 2 header: OML, RSL(RLL), NS
Definition: msgb.h:55
struct msgb * msgb_copy(const struct msgb *msg, const char *name)
Copy an msgb.
Definition: msgb.c:165
unsigned char * head
start of underlying memory buffer
Definition: msgb.h:64
static uint32_t msgb_get_u32(struct msgb *msgb)
remove uint32 from end of message
Definition: msgb.h:264
static int msgb_headroom(const struct msgb *msgb)
determine the amount of headroom in msgb
Definition: msgb.h:167
Osmocom bit level support code.
uint16_t data_len
length of underlying data array
Definition: msgb.h:61
unsigned char _data[0]
optional immediate data array
Definition: msgb.h:67
struct llist_head list
linked list header
Definition: msgb.h:43
uint8_t * msgb_data(const struct msgb *msg)
get pointer to data section of message buffer
Definition: msgb.c:135
#define msgb_l2(m)
obtain L2 header of msgb
Definition: msgb.h:94
static uint16_t msgb_get_u16(struct msgb *msgb)
remove uint16 from end of message
Definition: msgb.h:254
void * dst
reference of origin/destination
Definition: msgb.h:49
static unsigned int msgb_headlen(const struct msgb *msgb)
determine the length of the header
Definition: msgb.h:143
uint16_t msgb_length(const struct msgb *msg)
get length of message buffer
Definition: msgb.c:144
static int msgb_test_invariant(const struct msgb *msg) __attribute__((pure))
Check a message buffer for consistency.
Definition: msgb.h:422
static void msgb_put_u32(struct msgb *msgb, uint32_t word)
append a uint32 value to the end of the message
Definition: msgb.h:219
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: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:199