]> git.stg.codes - stg.git/blob - projects/traffcounter/tc_packets.h
Merge branch 'naffanya-dev'
[stg.git] / projects / traffcounter / tc_packets.h
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21  /*
22  $Revision: 1.3 $
23  $Date: 2009/04/10 14:14:49 $
24  $Author: faust $
25  */
26
27
28 #ifndef __TC_PACKETS_H__
29 #define __TC_PACKETS_H__
30
31 #include <netinet/ip.h>
32
33 #ifdef HAVE_STDINT
34     #include <stdint.h>
35 #else
36     #ifdef HAVE_INTTYPES
37         #include <inttypes.h>
38     #else
39         #error "You need either stdint.h or inttypes.h to compile this!"
40     #endif
41 #endif
42
43 namespace STG
44 {
45
46     //-----------------------------------------------------------------------------
47     /*
48      *  Session identifier
49      * A session is an amount of bytes transfered in one direction between two
50      * fixed addresses by one protocol.
51      * In case of UDP/TCP session is also identified by ports.
52      */
53     struct SESSION_ID
54     {
55     SESSION_ID()
56         : saddr(0),
57           daddr(0),
58           sport(0),
59           dport(0),
60           proto(0)
61         {
62         }
63
64     SESSION_ID(const iphdr & ipHdr, uint16_t sp, uint16_t dp)
65         : saddr(ipHdr.saddr),
66           daddr(ipHdr.daddr),
67           sport(sp),
68           dport(dp),
69           proto(ipHdr.protocol)
70         {
71         }
72
73     uint32_t    saddr;
74     uint32_t    daddr;
75     uint16_t    sport;
76     uint16_t    dport;
77     uint8_t     proto;
78
79     bool operator ==(const SESSION_ID & rval)
80         {
81         return saddr == rval.saddr &&
82                sport == rval.sport &&
83                daddr == rval.daddr &&
84                dport == rval.dport &&
85                proto == rval.proto;
86         }
87     };
88     //-----------------------------------------------------------------------------
89     /*
90      *  Ordering functor to use SESSION_ID as key-type in maps
91      */
92     struct SESSION_LESS 
93         : public std::binary_function<SESSION_ID, SESSION_ID, bool> {
94     bool operator()(const SESSION_ID & lval, const SESSION_ID & rval) const
95         {
96         if (lval.saddr > rval.saddr)
97             return false;
98         if (lval.saddr < rval.saddr)
99             return true;
100         if (lval.daddr > rval.daddr)
101             return false;
102         if (lval.daddr < rval.daddr)
103             return true;
104         if (lval.sport > rval.sport)
105             return false;
106         if (lval.sport < rval.sport)
107             return true;
108         if (lval.dport > rval.dport)
109             return false;
110         if (lval.dport < rval.dport)
111             return true;
112         if (lval.proto > rval.proto)
113             return false;
114         if (lval.proto < rval.proto)
115             return true;
116         return false;
117         };
118     };
119     //-----------------------------------------------------------------------------
120     /*
121      *  A packet in the incoming queue
122      * Can create a new session or be attached to an existing one
123      */
124     struct PENDING_PACKET : public SESSION_ID
125     {
126     PENDING_PACKET()
127         {
128         }
129     PENDING_PACKET(const iphdr & ipHdr, uint16_t sp, uint16_t dp)
130         : SESSION_ID(ipHdr, sp, dp),
131           length(ipHdr.tot_len),
132           direction(FOREIGN)
133         {
134         }
135
136     uint16_t    length;
137     enum DIRECTION
138         {
139         INCOMING = 0,   // From outer world to user
140         OUTGOING,       // From user to outer world
141         LOCAL,   // From user to user
142         FOREIGN         // From outer world to outer world
143         } direction;
144     };
145     //-----------------------------------------------------------------------------
146     /*
147      *  Session length and meta-information
148      * Used to identify data cost
149      */
150     struct SESSION_DATA
151     {
152     SESSION_DATA()
153         {
154         dir          = -1; // NULL direction
155         length       = 0;
156         };
157
158     SESSION_DATA(const SESSION_DATA & sp)
159         {
160         dir          = sp.dir;
161         length       = sp.length;
162         };
163
164     int         dir;
165     uint32_t    length;
166     };
167     //-----------------------------------------------------------------------------
168     /*
169      *  User-related types
170      */
171     typedef std::pair<SESSION_ID, SESSION_DATA> TRAFF_ITEM;
172     typedef std::list<TRAFF_ITEM> TRAFF_DATA;
173
174 }
175
176 #endif