servermgmt: fixed config file parser
[netsniff-ng.git] / src / pkt_buff.h
blobb80030a292b99175a146c0406d50b768159ceaad
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_struct.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 *proto;
25 static inline struct pkt_buff *pkt_alloc(uint8_t *packet, unsigned int len)
27 struct pkt_buff *pkt = xmalloc(sizeof(*pkt));
29 pkt->head = packet;
30 pkt->data = packet;
31 pkt->tail = packet + len;
32 pkt->size = len;
33 pkt->proto = NULL;
35 return pkt;
38 static inline void pkt_free(struct pkt_buff *pkt)
40 xfree(pkt);
43 static inline unsigned int pkt_len(struct pkt_buff *pkt)
45 bug_on(!pkt || pkt->data > pkt->tail);
47 return pkt->tail - pkt->data;
50 static inline uint8_t *pkt_pull(struct pkt_buff *pkt, unsigned int len)
52 uint8_t *data = NULL;
54 bug_on(!pkt || pkt->head > pkt->data || pkt->data > pkt->tail);
56 if (pkt_len(pkt) && pkt->data + len <= pkt->tail) {
57 data = pkt->data;
58 pkt->data += len;
61 return data;
64 static inline uint8_t *pkt_peek(struct pkt_buff *pkt)
66 bug_on(!pkt || pkt->head > pkt->data || pkt->data > pkt->tail);
68 return pkt->data;
71 static inline void pkt_trim(struct pkt_buff *pkt, unsigned int len)
73 bug_on(!pkt || pkt->head > pkt->data || pkt->data > pkt->tail);
75 if (pkt_len(pkt) && pkt->tail - len >= pkt->data)
76 pkt->tail -= len;
79 static inline uint8_t *pkt_pull_tail(struct pkt_buff *pkt, unsigned int len)
81 uint8_t *tail = NULL;
83 bug_on(!pkt || pkt->head > pkt->data || pkt->data > pkt->tail);
85 if (pkt_len(pkt) && pkt->tail - len >= pkt->data) {
86 tail = pkt->tail;
87 pkt->tail -= len;
90 return tail;
93 static inline void pkt_set_proto(struct pkt_buff *pkt, struct hash_table *table,
94 unsigned int key)
96 bug_on(!pkt || !table);
98 pkt->proto = (struct protocol *) lookup_hash(key, table);
99 while (pkt->proto && key != pkt->proto->key)
100 pkt->proto = pkt->proto->next;
103 #endif /* PKT_BUFF_H */