netsniff-ng: ring: Fix build if tp_vlan_tpid is not available in kernel header
[netsniff-ng.git] / pkt_buff.h
blob30b999eb0213dc63f7f996682e41c75539f7c2c8
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright (C) 2012 Christoph Jaeger <christoph@netsniff-ng.org>
4 * Subject to the GPL, version 2.
5 */
7 #ifndef PKT_BUFF_H
8 #define PKT_BUFF_H
10 #include "hash.h"
11 #include "built_in.h"
12 #include "proto.h"
13 #include "xmalloc.h"
15 struct pkt_buff {
16 /* invariant: head <= data <= tail */
17 uint8_t *head;
18 uint8_t *data;
19 uint8_t *tail;
20 unsigned int size;
22 struct protocol *dissector;
23 uint32_t link_type;
24 struct sockaddr_ll *sll;
27 static inline struct pkt_buff *pkt_alloc(uint8_t *packet, unsigned int len)
29 struct pkt_buff *pkt = xmalloc(sizeof(*pkt));
31 pkt->head = packet;
32 pkt->data = packet;
33 pkt->tail = packet + len;
34 pkt->size = len;
35 pkt->dissector = NULL;
37 return pkt;
40 static inline void pkt_free(struct pkt_buff *pkt)
42 xfree(pkt);
45 static inline unsigned int pkt_len(struct pkt_buff *pkt)
47 bug_on(!pkt || pkt->data > pkt->tail);
49 return pkt->tail - pkt->data;
52 static inline uint8_t *pkt_pull(struct pkt_buff *pkt, unsigned int len)
54 uint8_t *data = NULL;
56 bug_on(!pkt || pkt->head > pkt->data || pkt->data > pkt->tail);
58 if (len <= pkt_len(pkt)) {
59 data = pkt->data;
60 pkt->data += len;
63 bug_on(!pkt || pkt->head > pkt->data || pkt->data > pkt->tail);
65 return data;
68 static inline uint8_t *pkt_peek(struct pkt_buff *pkt)
70 bug_on(!pkt || pkt->head > pkt->data || pkt->data > pkt->tail);
72 return pkt->data;
75 static inline unsigned int pkt_trim(struct pkt_buff *pkt, unsigned int len)
77 unsigned int ret = 0;
79 bug_on(!pkt || pkt->head > pkt->data || pkt->data > pkt->tail);
81 if (len <= pkt_len(pkt))
82 ret = len;
84 pkt->tail -= ret;
85 bug_on(!pkt || pkt->head > pkt->data || pkt->data > pkt->tail);
87 return ret;
90 static inline uint8_t *pkt_pull_tail(struct pkt_buff *pkt, unsigned int len)
92 uint8_t *tail = NULL;
94 bug_on(!pkt || pkt->head > pkt->data || pkt->data > pkt->tail);
96 if (len <= pkt_len(pkt)) {
97 tail = pkt->tail;
98 pkt->tail -= len;
101 return tail;
104 static inline void pkt_set_dissector(struct pkt_buff *pkt, struct hash_table *table,
105 unsigned int key)
107 bug_on(!pkt || !table);
109 pkt->dissector = lookup_hash(key, table);
110 while (pkt->dissector && key != pkt->dissector->key)
111 pkt->dissector = pkt->dissector->next;
114 #endif /* PKT_BUFF_H */