]> mj.ucw.cz Git - netgrind.git/blob - netgrind/pkt.h
Hopefully complete TCP analyser.
[netgrind.git] / netgrind / pkt.h
1 /*
2  *      Netgrind -- Packet Buffers
3  *
4  *      (c) 2003 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU General Public License.
8  */
9
10 #include "lib/lists.h"
11
12 struct pkt {
13   node n;
14   u64 timestamp;
15   u32 seq;
16   byte *data, *stop, *ebuf;
17   byte buf[0];
18 };
19
20 static inline uns pkt_len(struct pkt *p)
21 {
22   return p->stop - p->data;
23 }
24
25 static inline void pkt_push(struct pkt *p, uns len)
26 {
27   p->data -= len;
28   ASSERT(p->data >= p->buf);
29 }
30
31 static inline void *pkt_pop(struct pkt *p, uns len)
32 {
33   byte *d = p->data;
34   if (d + len > p->stop)
35     return NULL;
36   p->data += len;
37   return d;
38 }
39
40 static inline void *pkt_peek(struct pkt *p, uns len)
41 {
42   byte *d = p->data;
43   if (d + len > p->stop)
44     return NULL;
45   return d;
46 }
47
48 static inline byte *pkt_append(struct pkt *p, uns len)
49 {
50   byte *d = p->stop;
51   p->stop = d + len;
52   ASSERT(p->stop <= p->ebuf);
53   return d;
54 }
55
56 static inline byte *pkt_unappend(struct pkt *p, uns len)
57 {
58   p->stop -= len;
59   ASSERT(p->stop >= p->data);
60   return p->stop;
61 }
62
63 struct pkt *pkt_new(uns preroom, uns postroom);
64 void pkt_free(struct pkt *pkt);
65
66 struct pkt_stats {
67   u64 packets;
68   u64 bytes;
69 };
70
71 static inline void pkt_account(struct pkt_stats *stats, struct pkt *pkt)
72 {
73   stats->packets++;
74   stats->bytes += pkt_len(pkt);
75 }