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
33extern "C" {
34#endif
35
107/* packed to be aligned correctly in the static packet buffer */
108typedef struct gnrc_pktsnip {
109 /* the first three fields *MUST* match iolist_t! */
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
151static 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 /* Removing head is a no-op. The new head is the next in the list. */
214 if (pkt == snip) {
215 return pkt->next;
216 }
217
218 /* Removing nothing is a no-op, the new head is the old one */
219 if (snip == NULL) {
220 return pkt;
221 }
222
223 /* Iterate over the list and remove the given snip from it, if found.
224 * The new head is the old head. */
225 for (gnrc_pktsnip_t *i = pkt; i != NULL; i = i->next) {
226 if (i->next == snip) {
227 i->next = snip->next;
228 return pkt;
229 }
230 }
231
232 return pkt;
233}
234
243static inline size_t gnrc_pkt_len_upto(const gnrc_pktsnip_t *pkt, gnrc_nettype_t type)
244{
245 size_t len = 0;
246
247 while (pkt != NULL) {
248 len += pkt->size;
249
250 if (pkt->type == type) {
251 break;
252 }
253
254 pkt = pkt->next;
255 }
256
257 return len;
258}
259
267static inline size_t gnrc_pkt_count(const gnrc_pktsnip_t *pkt)
268{
269 size_t count = 0;
270
271 while (pkt != NULL) {
272 ++count;
273 pkt = pkt->next;
274 }
275
276 return count;
277}
278
290
291#ifdef __cplusplus
292}
293#endif
294
295#endif /* NET_GNRC_PKT_H */
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:139
gnrc_nettype_t
Definition of protocol types in the network stack.
Definition: nettype.h:51
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
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.
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:243
static size_t gnrc_pkt_count(const gnrc_pktsnip_t *pkt)
Count the numbers of snips in the given packet.
Definition: pkt.h:267
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
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_prev_snip(gnrc_pktsnip_t *pkt, gnrc_pktsnip_t *snip)
Returns the snip before a given snip in a packet.
Definition: pkt.h:135
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: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