pkt.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014, 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
3  * 2015 Freie Universit├Ąt Berlin
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  */
9 
22 #ifndef NET_GNRC_PKT_H
23 #define NET_GNRC_PKT_H
24 
25 #include <inttypes.h>
26 #include <stdlib.h>
27 
28 #include "sched.h"
29 #include "net/gnrc/nettype.h"
30 #include "list.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
107 /* packed to be aligned correctly in the static packet buffer */
108 typedef struct gnrc_pktsnip {
109  /* the first three fields *MUST* match iolist_t! */
110  struct gnrc_pktsnip *next;
111  void *data;
112  size_t size;
118  unsigned int users;
120 #ifdef MODULE_GNRC_NETERR
121  kernel_pid_t err_sub;
123 #endif
125 
136  gnrc_pktsnip_t *snip)
137 {
138  while ((pkt != NULL) && (pkt->next != snip)) {
139  pkt = pkt->next;
140  }
141  return pkt;
142 }
143 
151 static inline size_t gnrc_pkt_len(const gnrc_pktsnip_t *pkt)
152 {
153  size_t len = 0;
154 
155  while (pkt != NULL) {
156  len += pkt->size;
157  pkt = pkt->next;
158  }
159 
160  return len;
161 }
162 
172  gnrc_pktsnip_t *snip)
173 {
174  /* find last snip in pkt */
175  gnrc_pktsnip_t *last = gnrc_pkt_prev_snip(pkt, NULL);
176 
177  if (last != NULL) {
178  last->next = snip;
179  }
180  else {
181  /* last == NULL means snip */
182  pkt = snip;
183  }
184  return pkt;
185 }
186 
196  gnrc_pktsnip_t *snip)
197 {
198  snip->next = pkt;
199  return snip;
200 }
201 
211  gnrc_pktsnip_t *snip)
212 {
213  list_node_t list = { .next = (list_node_t *)pkt };
214 
215  list_remove(&list, (list_node_t *)snip);
216  return (gnrc_pktsnip_t *)list.next;
217 }
218 
227 static inline size_t gnrc_pkt_len_upto(const gnrc_pktsnip_t *pkt, gnrc_nettype_t type)
228 {
229  size_t len = 0;
230 
231  while (pkt != NULL) {
232  len += pkt->size;
233 
234  if (pkt->type == type) {
235  break;
236  }
237 
238  pkt = pkt->next;
239  }
240 
241  return len;
242 }
243 
251 static inline size_t gnrc_pkt_count(const gnrc_pktsnip_t *pkt)
252 {
253  size_t count = 0;
254 
255  while (pkt != NULL) {
256  ++count;
257  pkt = pkt->next;
258  }
259 
260  return count;
261 }
262 
274 
275 #ifdef __cplusplus
276 }
277 #endif
278 
279 #endif /* NET_GNRC_PKT_H */
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:134
gnrc_nettype_t
Definition of protocol types in the network stack.
Definition: nettype.h:50
static gnrc_pktsnip_t * gnrc_pkt_append(gnrc_pktsnip_t *pkt, gnrc_pktsnip_t *snip)
Appends a snip to a packet.
Definition: pkt.h:171
static gnrc_pktsnip_t * gnrc_pkt_delete(gnrc_pktsnip_t *pkt, gnrc_pktsnip_t *snip)
Deletes a snip from a packet.
Definition: pkt.h:210
static gnrc_pktsnip_t * gnrc_pkt_prepend(gnrc_pktsnip_t *pkt, gnrc_pktsnip_t *snip)
Prepends a snip to a packet.
Definition: pkt.h:195
static size_t gnrc_pkt_len_upto(const gnrc_pktsnip_t *pkt, gnrc_nettype_t type)
Calculates length of a packet in byte up to (including) a snip with the given type.
Definition: pkt.h:227
static size_t gnrc_pkt_count(const gnrc_pktsnip_t *pkt)
Count the numbers of snips in the given packet.
Definition: pkt.h:251
static gnrc_pktsnip_t * gnrc_pkt_prev_snip(gnrc_pktsnip_t *pkt, gnrc_pktsnip_t *snip)
Returns the snip before a given snip in a packet.
Definition: pkt.h:135
gnrc_pktsnip_t * gnrc_pktsnip_search_type(gnrc_pktsnip_t *pkt, gnrc_nettype_t type)
Searches the packet for a packet snip of a specific type.
struct gnrc_pktsnip gnrc_pktsnip_t
Type to represent parts (either headers or payload) of a packet, called snips.
static size_t gnrc_pkt_len(const gnrc_pktsnip_t *pkt)
Calculates length of a packet in byte.
Definition: pkt.h:151
Adds include for missing inttype definitions.
Intrusive linked list.
static list_node_t * list_remove(list_node_t *list, list_node_t *node)
Removes the node from the list.
Definition: list.h:86
Protocol type definitions.
Scheduler API definition.
Type to represent parts (either headers or payload) of a packet, called snips.
Definition: pkt.h:108
void * data
pointer to the data of the snip
Definition: pkt.h:111
size_t size
the length of the snip in byte
Definition: pkt.h:112
unsigned int users
Counter of threads currently having control over this packet.
Definition: pkt.h:118
gnrc_nettype_t type
protocol of the packet snip
Definition: pkt.h:119
struct gnrc_pktsnip * next
next snip in the packet
Definition: pkt.h:110
List node structure.
Definition: list.h:40
struct list_node * next
pointer to next list entry
Definition: list.h:41