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 
219 
228 static inline size_t gnrc_pkt_len_upto(const gnrc_pktsnip_t *pkt, gnrc_nettype_t type)
229 {
230  size_t len = 0;
231 
232  while (pkt != NULL) {
233  len += pkt->size;
234 
235  if (pkt->type == type) {
236  break;
237  }
238 
239  pkt = pkt->next;
240  }
241 
242  return len;
243 }
244 
252 static inline size_t gnrc_pkt_count(const gnrc_pktsnip_t *pkt)
253 {
254  size_t count = 0;
255 
256  while (pkt != NULL) {
257  ++count;
258  pkt = pkt->next;
259  }
260 
261  return count;
262 }
263 
275 
276 #ifdef __cplusplus
277 }
278 #endif
279 
280 #endif /* NET_GNRC_PKT_H */
281 
gnrc_pkt_len_upto
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:228
kernel_pid_t
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:125
gnrc_pktsnip::data
void * data
pointer to the data of the snip
Definition: pkt.h:111
gnrc_pkt_prepend
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
gnrc_pkt_count
static size_t gnrc_pkt_count(const gnrc_pktsnip_t *pkt)
Count the numbers of snips in the given packet.
Definition: pkt.h:252
gnrc_pkt_prev_snip
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::type
gnrc_nettype_t type
protocol of the packet snip
Definition: pkt.h:119
gnrc_pktsnip_t
struct gnrc_pktsnip gnrc_pktsnip_t
Type to represent parts (either headers or payload) of a packet, called snips.
sched.h
Scheduler API definition.
gnrc_pkt_append
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
gnrc_pktsnip_search_type
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.
nettype.h
Protocol type definitions.
gnrc_nettype_t
gnrc_nettype_t
Definition of protocol types in the network stack.
Definition: nettype.h:50
list_node
List node structure.
Definition: list.h:40
list_node::next
struct list_node * next
pointer to next list entry
Definition: list.h:41
list_remove
static list_node_t * list_remove(list_node_t *list, list_node_t *node)
Removes the node from the list.
Definition: list.h:86
gnrc_pkt_delete
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
list.h
Intrusive linked list.
gnrc_pkt_len
static size_t gnrc_pkt_len(const gnrc_pktsnip_t *pkt)
Calculates length of a packet in byte.
Definition: pkt.h:151
gnrc_pktsnip::size
size_t size
the length of the snip in byte
Definition: pkt.h:112
gnrc_pktsnip::next
struct gnrc_pktsnip * next
next snip in the packet
Definition: pkt.h:110
inttypes.h
Adds include for missing inttype definitions.
gnrc_pktsnip::users
unsigned int users
Counter of threads currently having control over this packet.
Definition: pkt.h:118
gnrc_pktsnip
Type to represent parts (either headers or payload) of a packet, called snips.
Definition: pkt.h:108