Loading...
Searching...
No Matches
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;
113 /* end of iolist_t */
114#ifdef MODULE_GNRC_NETERR
115 kernel_pid_t err_sub;
117#endif
124 uint8_t users;
126
137 gnrc_pktsnip_t *snip)
138{
139 while ((pkt != NULL) && (pkt->next != snip)) {
140 pkt = pkt->next;
141 }
142 return pkt;
143}
144
152static inline size_t gnrc_pkt_len(const gnrc_pktsnip_t *pkt)
153{
154 size_t len = 0;
155
156 while (pkt != NULL) {
157 len += pkt->size;
158 pkt = pkt->next;
159 }
160
161 return len;
162}
163
173 gnrc_pktsnip_t *snip)
174{
175 /* find last snip in pkt */
176 gnrc_pktsnip_t *last = gnrc_pkt_prev_snip(pkt, NULL);
177
178 if (last != NULL) {
179 last->next = snip;
180 }
181 else {
182 /* last == NULL means snip */
183 pkt = snip;
184 }
185 return pkt;
186}
187
197 gnrc_pktsnip_t *snip)
198{
199 snip->next = pkt;
200 return snip;
201}
202
212 gnrc_pktsnip_t *snip)
213{
214 /* Removing head is a no-op. The new head is the next in the list. */
215 if (pkt == snip) {
216 return pkt->next;
217 }
218
219 /* Removing nothing is a no-op, the new head is the old one */
220 if (snip == NULL) {
221 return pkt;
222 }
223
224 /* Iterate over the list and remove the given snip from it, if found.
225 * The new head is the old head. */
226 for (gnrc_pktsnip_t *i = pkt; i != NULL; i = i->next) {
227 if (i->next == snip) {
228 i->next = snip->next;
229 return pkt;
230 }
231 }
232
233 return pkt;
234}
235
244static inline size_t gnrc_pkt_len_upto(const gnrc_pktsnip_t *pkt, gnrc_nettype_t type)
245{
246 size_t len = 0;
247
248 while (pkt != NULL) {
249 len += pkt->size;
250
251 if (pkt->type == type) {
252 break;
253 }
254
255 pkt = pkt->next;
256 }
257
258 return len;
259}
260
268static inline size_t gnrc_pkt_count(const gnrc_pktsnip_t *pkt)
269{
270 size_t count = 0;
271
272 while (pkt != NULL) {
273 ++count;
274 pkt = pkt->next;
275 }
276
277 return count;
278}
279
291
292#ifdef __cplusplus
293}
294#endif
295
296#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:211
static gnrc_pktsnip_t * gnrc_pkt_prepend(gnrc_pktsnip_t *pkt, gnrc_pktsnip_t *snip)
Prepends a snip to a packet.
Definition pkt.h:196
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:244
static size_t gnrc_pkt_count(const gnrc_pktsnip_t *pkt)
Count the numbers of snips in the given packet.
Definition pkt.h:268
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:152
static gnrc_pktsnip_t * gnrc_pkt_append(gnrc_pktsnip_t *pkt, gnrc_pktsnip_t *snip)
Appends a snip to a packet.
Definition pkt.h:172
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:136
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
gnrc_nettype_t type
protocol of the packet snip
Definition pkt.h:118
uint8_t users
Counter of threads currently having control over this packet.
Definition pkt.h:124
struct gnrc_pktsnip * next
next snip in the packet
Definition pkt.h:110