pkt.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2014-2015 Martine Lenders <mlenders@inf.fu-berlin.de>
3  * SPDX-FileCopyrightText: 2015 Freie Universität Berlin
4  * SPDX-License-Identifier: LGPL-2.1-only
5  */
6 
7 #pragma once
8 
22 #include <inttypes.h>
23 #include <stdlib.h>
24 
25 #include "sched.h"
26 #include "net/gnrc/nettype.h"
27 #include "list.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
104 /* packed to be aligned correctly in the static packet buffer */
105 typedef struct gnrc_pktsnip {
106  /* the first three fields *MUST* match iolist_t! */
107  struct gnrc_pktsnip *next;
108  void *data;
109  size_t size;
110  /* end of iolist_t */
111 #ifdef MODULE_GNRC_NETERR
112  kernel_pid_t err_sub;
114 #endif
121  uint8_t users;
123 
134  gnrc_pktsnip_t *snip)
135 {
136  while ((pkt != NULL) && (pkt->next != snip)) {
137  pkt = pkt->next;
138  }
139  return pkt;
140 }
141 
149 static inline size_t gnrc_pkt_len(const gnrc_pktsnip_t *pkt)
150 {
151  size_t len = 0;
152 
153  while (pkt != NULL) {
154  len += pkt->size;
155  pkt = pkt->next;
156  }
157 
158  return len;
159 }
160 
170  gnrc_pktsnip_t *snip)
171 {
172  /* find last snip in pkt */
173  gnrc_pktsnip_t *last = gnrc_pkt_prev_snip(pkt, NULL);
174 
175  if (last != NULL) {
176  last->next = snip;
177  }
178  else {
179  /* last == NULL means snip */
180  pkt = snip;
181  }
182  return pkt;
183 }
184 
194  gnrc_pktsnip_t *snip)
195 {
196  snip->next = pkt;
197  return snip;
198 }
199 
209  gnrc_pktsnip_t *snip)
210 {
211  /* Removing head is a no-op. The new head is the next in the list. */
212  if (pkt == snip) {
213  return pkt->next;
214  }
215 
216  /* Removing nothing is a no-op, the new head is the old one */
217  if (snip == NULL) {
218  return pkt;
219  }
220 
221  /* Iterate over the list and remove the given snip from it, if found.
222  * The new head is the old head. */
223  for (gnrc_pktsnip_t *i = pkt; i != NULL; i = i->next) {
224  if (i->next == snip) {
225  i->next = snip->next;
226  return pkt;
227  }
228  }
229 
230  return pkt;
231 }
232 
241 static inline size_t gnrc_pkt_len_upto(const gnrc_pktsnip_t *pkt, gnrc_nettype_t type)
242 {
243  size_t len = 0;
244 
245  while (pkt != NULL) {
246  len += pkt->size;
247 
248  if (pkt->type == type) {
249  break;
250  }
251 
252  pkt = pkt->next;
253  }
254 
255  return len;
256 }
257 
265 static inline size_t gnrc_pkt_count(const gnrc_pktsnip_t *pkt)
266 {
267  size_t count = 0;
268 
269  while (pkt != NULL) {
270  ++count;
271  pkt = pkt->next;
272  }
273 
274  return count;
275 }
276 
288 
289 #ifdef __cplusplus
290 }
291 #endif
292 
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:135
gnrc_nettype_t
Definition of protocol types in the network stack.
Definition: nettype.h:48
static gnrc_pktsnip_t * gnrc_pkt_append(gnrc_pktsnip_t *pkt, gnrc_pktsnip_t *snip)
Appends a snip to a packet.
Definition: pkt.h:169
static gnrc_pktsnip_t * gnrc_pkt_delete(gnrc_pktsnip_t *pkt, gnrc_pktsnip_t *snip)
Deletes a snip from a packet.
Definition: pkt.h:208
static gnrc_pktsnip_t * gnrc_pkt_prepend(gnrc_pktsnip_t *pkt, gnrc_pktsnip_t *snip)
Prepends a snip to a packet.
Definition: pkt.h:193
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:241
static size_t gnrc_pkt_count(const gnrc_pktsnip_t *pkt)
Count the numbers of snips in the given packet.
Definition: pkt.h:265
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:133
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:149
Adds include for missing inttype definitions.
Intrusive linked list.
Protocol type definitions.
Scheduler API definition.
Type to represent parts (either headers or payload) of a packet, called snips.
Definition: pkt.h:105
void * data
pointer to the data of the snip
Definition: pkt.h:108
size_t size
the length of the snip in byte
Definition: pkt.h:109
gnrc_nettype_t type
protocol of the packet snip
Definition: pkt.h:115
uint8_t users
Counter of threads currently having control over this packet.
Definition: pkt.h:121
struct gnrc_pktsnip * next
next snip in the packet
Definition: pkt.h:107